1

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.

3
  • 1
    you should use viewholder class as per doc "The World of ListView-2010" by Romain Guy. Commented Jun 27, 2013 at 4:36
  • NewOverHere did you try my answer? does it work? Commented Jun 27, 2013 at 5:37
  • It's a 1 hour long video, still watching it now. Thanks for helping. I'll get back to you when I finish watching it and find a solution. Cheers Commented Jun 27, 2013 at 5:44

2 Answers 2

1

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();
    }
    });
Sign up to request clarification or add additional context in comments.

4 Comments

but I want to change image resource of the image button when its clicked. So how do I access my imagebutton to call image_button.setImageResource(drawable)?
In the video also they mentioned this ViewHolder class but so far they havent told how to access views on click event. Still watching...
why don't you give the above a try?
@NewOverHere That itemView in OnItemClick is the View you returned in your adapter. You could use itemView.findViewById(R.id.imagebutton) to get the ImageButton and then change the image from there.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.