Chintan Trivedi
Chintan Trivedi

Reputation: 748

Only first row's views(button) are clickable in custom list view, android

Here is my adapter class:-

public class CustomAdapter extends BaseAdapter {

        Context c;

        CustomAdapter(Context c)
        {
            this.c=c;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return 3;
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return arg0;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            final int pos=position;
            LayoutInflater inflater=LayoutInflater.from(c);
            View v=inflater.inflate(R.layout.layout_list_item, parent, false);
            ImageButton image_button=v.findViewById(R.id.imagebutton);
            image_button.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Toast.makeText(c, "Image Button clicked:" + pos, Toast.LENGTH_SHORT).show();
                }
            });

            return v;
        }

    }

How do I make all imagebuttons clickable? I tried searching for answers and as per an answer given here: how to make an imageview clickable in an listview I tried but only my first row button is clickable. Please help.

Upvotes: 1

Views: 810

Answers (2)

Sharad Mhaske
Sharad Mhaske

Reputation: 1103

As told by "the World of Listview-2010" see below link show how to use viewholder class with custom listview in android http://impressive-artworx.de/2011/list-all-installed-apps/ Hope this helps you.

Upvotes: 1

Raghunandan
Raghunandan

Reputation: 133560

In your custom adapter constructor

    LayoutInflater mInflater;  
    CustomAdapter(Context c)
    {
        mInflater = LayoutInflater.from(c); 
        // initialize inflater in the constructor.
        // need not initialize everytime getView is called.
    }

Use a View Holder

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

      static class ViewHolder
       {
           ImageButton ib;
       }
      @Override
      public View getView(int position, View item, ViewGroup parent) { 
       ViewHolder holder; 
       if(item == null){
       item = mInflater.inflate(R.layout.elementos_lista_temas, null);
       holder = new ViewHolder(); 
       holder.ib = (ImageButton) item.findViewById(R.id.imagebutton);
       item.setTag(holder);
       }
       else{
        holder = (ViewHolder)item.getTag();
       }
       return item;
       }

Then in your activity class

 ListView lv = (ListView) findViewById(R.id.listview);
 CustomAdapter cus = new CustomAdapter(this);
 lv.setAdapter(cus); 
 lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> listView, View itemView, int itemPosition, long itemId)
    {           
         Toast.makeText(ActivityName.this, "Image Button clicked:" + itemPosition, Toast.LENGTH_SHORT).show();
    }
    });

Upvotes: 1

Related Questions