开发者

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开发者_Python百科);
                    // 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.


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

Check this thread.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜