M.A.Murali
M.A.Murali

Reputation: 10148

How can I perform onListItemClick in a list view on android

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.

Upvotes: 0

Views: 1529

Answers (3)

Danail
Danail

Reputation: 10583

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.

Upvotes: 0

sat
sat

Reputation: 41076

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

Check this thread.

Upvotes: 1

Toni Toni Chopper
Toni Toni Chopper

Reputation: 1851

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.

Upvotes: 0

Related Questions