Search code examples
androidandroid-layoutonclicklistenerandroid-custom-viewbuttonclick

How do I respond to click events from a button in a custom view?


I'm trying to adapt this tutorial for use with a TextView and Button. I've created my ArrayList and ListView:

   private void displayListView() {

    //putting two Country objects into the ArrayList
    //these countries have email addresses for some reason, just go with it
    ArrayList<Country> countryList = new ArrayList<Country>();
    country = new Country("[email protected]","DummyName1",false);
    countryList.add(country);
    country = new Country("[email protected]","DummyName2",false);
    countryList.add(country);

    //create an ArrayAdaptar from the String Array
    dataAdapter = new MyCustomAdapter(this,
            R.layout.pending_invite, countryList);

    ListView listView = (ListView) findViewById(R.id.listViewPendingRequests);
    // Assign adapter to ListView
    listView.setAdapter(dataAdapter);

}

Here is my main XML:

 <?xml version="1.0" encoding="utf-8"?>

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:orientation="vertical" android:layout_width="match_parent"
     android:layout_height="match_parent">  

         <ListView
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:id="@+id/listViewPendingRequests" />

  </LinearLayout>

Here is my pending_invite.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout" >

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Pending Invitation 1"
            android:id="@+id/textView7"
            android:layout_weight="1" />

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Cancel"
            android:id="@+id/button5"
            android:layout_weight="1"
            android:onClick="onClickCancel"/>
    </LinearLayout>
</LinearLayout>

I think I'm having trouble making MyCustomAdapter given that I'm no longer looking for checkboxs and stuff. Anyway, this is what I've got so far:

 private class MyCustomAdapter extends ArrayAdapter<Country> {

    private ArrayList<Country> countryList;

    public MyCustomAdapter(Context context, int textViewResourceId,
                           ArrayList<Country> countryList) {
        super(context, textViewResourceId, countryList);
        this.countryList = new ArrayList<Country>();
        this.countryList.addAll(countryList);
    }

    private class ViewHolder {
        TextView code;
        Button name;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        Log.v("ConvertView", String.valueOf(position));

        if (convertView == null) {
            LayoutInflater vi = (LayoutInflater)getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(R.layout.pending_invite, null);

            holder = new ViewHolder();
            holder.code = (TextView) convertView.findViewById(R.id.textView7);
            holder.name = (Button) convertView.findViewById(R.id.button5);
            convertView.setTag(holder);

        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        Country country = countryList.get(position);
        holder.name.setText(country.getName());
        holder.name.setTag(country);

        return convertView;

    }

}

So that works (surprisingly) to construct the views, but how do I tell which one was pressed in the onClick?:

 public void onClickCancel(View view){
    Log.i(LOG_TAG, "OnClickCancel view: " + view.getId()); //getId returns the same thing for all clicks. How do I get which button clicked?

}

The view.getId returns the same ID for all any of the buttons. How do I attach an onClick Listener to inflated buttons within a custom view?


Solution

  • You can do something like this

    private class MyCustomAdapter extends ArrayAdapter<Country> {
    
        private ArrayList<Country> countryList;
    
        public MyCustomAdapter(Context context, int textViewResourceId,
                               ArrayList<Country> countryList) {
            super(context, textViewResourceId, countryList);
            this.countryList = new ArrayList<Country>();
            this.countryList.addAll(countryList);
        }
    
        private class ViewHolder {
            TextView code;
            Button name;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
    
            ViewHolder holder = null;
            Log.v("ConvertView", String.valueOf(position));
    
            if (convertView == null) {
                LayoutInflater vi = (LayoutInflater)getSystemService(
                        Context.LAYOUT_INFLATER_SERVICE);
                convertView = vi.inflate(R.layout.pending_invite, null);
    
                holder = new ViewHolder();
                holder.code = (TextView) convertView.findViewById(R.id.textView7);
                holder.name = (Button) convertView.findViewById(R.id.button5);
                convertView.setTag(holder);
    
            }else {
                holder = (ViewHolder) convertView.getTag();
            }
    
            Country country = countryList.get(position);
           // holder.code.setText(" (" +  country.getCode() + ")");
            holder.name.setText(country.getName());
            //holder.name.setChecked(country.isSelected());
            holder.name.setTag(countryList.get(position).getName());     // here you are setting the tag, it will be retrieved in handleClick method
            // here create your on click handler, like
    
           holder.name.setOnClickListener(handleClick(holder.name));
    
    
            return convertView;
    
        }
    
    }
    

    Here define your handleClick method

    private OnClickListener handleClick(final Button butn) {
            // TODO Auto-generated method stub
            return new OnClickListener() {
    
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    String TAG = butn.getTag().toString();  // get the tag, now you can compare which button has been clicked with respect to its tag name 
    
                 if (TAG.equals("somehting")
                 {
                      // your logic here
                 }
                }
            };
        }
    

    I hope you got some idea from this answer.