0

I have a ListView with three ImageButtons and TextViews, ImageView. How can I perform onListItemClick in a ListView for individual actions for each ImageButton.

public class AndroidThumbnailList extends ListActivity{
    ..........
    ib2=(ImageButton)findViewById(R.id.imageButton2);
    ib3=(ImageButton)findViewById(R.id.imageButton3);
    ib1=(ImageButton)findViewById(R.id.imageButton1);

    public class MyThumbnaildapter extends ArrayAdapter<String>{
            public MyThumbnaildapter(Context context, int textViewResourceId,String[] objects) {
                    super(context, textViewResourceId, objects);
                    // TODO Auto-generated constructor stub
            }
            public View getView(int position, View convertView, ViewGroup parent) {
                     .........
            }
    }

    protected void onListItemClick(ListView l, View v, int position, long id) {

        Log.e("video", "called");
        super.onListItemClick(l, v, position, id);
        if(v.equals(ib1)){
             ....
        } 
        if(v.equals(ib2)){
             ....
        }

   }
   public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setListAdapter(new MyThumbnaildapter(AndroidThumbnailList.this, R.layout.row,  _videosId));
   }

}

If I touch ImageButton no action is done and theImageButton`s also not highlighted.

my XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="100dp"      android:descendantFocusability="blocksDescendants">
<image view>....</image view>
<LinearLayout android:orientation="vertical"   android:descendantFocusability="blocksDescendants"
 <ImageButton android:id="@+id/imageButton1 ... ></ImageButton>
 <ImageButton android:id="@+id/imageButton2 ... ></ImageButton>
 <ImageButton android:id="@+id/imageButton3 ... ></ImageButton>

please help me.

Sufian
  • 6,405
  • 16
  • 66
  • 120
M.A.Murali
  • 9,988
  • 36
  • 105
  • 182

3 Answers3

1

You will have to set listener to each Button in getView() of ListAdapter.

Check this thread.

Community
  • 1
  • 1
sat
  • 40,138
  • 28
  • 93
  • 102
0

First, are you using ListView or LinearLayout? ListActivity is used along with ListView, I don't see yours. You have used LinearLayout instead and have given your own ImageButtons, which are not created by the adapter, thus not using onListItemClick.

Second (this is just opinion, you can make it work without that), IMHO, you better define OnClickListener when you define your items in getView(), and not using onListItemClick.

Sufian
  • 6,405
  • 16
  • 66
  • 120
Danail
  • 10,443
  • 12
  • 54
  • 76
0

You can set individuals onClick() event handlers for each ImageButton.

You have to declare the method of your context (typically, your Activity):

public void onClickHandler(View v)

See Javadocs of ImageButton and onClick() for further details.

Sufian
  • 6,405
  • 16
  • 66
  • 120
Toni Toni Chopper
  • 1,833
  • 2
  • 20
  • 29
  • i implement as follows: i get null pointer exception please help me class implement OnclickListener{ ib1=(ImageButton)findViewById(R.id.image1); ib1.setOnclickListener(this); // null pointer exception public void onClickHandler(View v){ if(v.getid.equals(R,id.image){...} } } – M.A.Murali Jun 09 '11 at 07:44