ListView with OnItemClickListener android

2020-01-23 10:23发布

I am using a custom ListView with RatingBar and ImageButton. Here is my problem: When I click on my ListView, my OnItemClickListener is not working. Please can any one help me. Code:

ListView lv = getListView();
setContentView(lv);
lv.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
        Toast.makeText(SuggestionActivity.this, "" + position, Toast.LENGTH_SHORT).show();
    }
});

Thanks in advance!

15条回答
仙女界的扛把子
2楼-- · 2020-01-23 10:57

Is there and image in the list view that you are using> then follow the link: http://vikaskanani.wordpress.com/2011/07/20/android-custom-image-gallery-with-checkbox-in-grid-to-select-multiple/

I think when you work out on the link that I have provided first every thing will work fine, I have tried that. If you want a refined answer please elaborate the question with code and description.

查看更多
叼着烟拽天下
3楼-- · 2020-01-23 11:02

1) Check if you are using OnItemClickListener or OnClickListener (which is not supported for ListView)
Documentation Android Developers ListView

2) Check if you added Listener to your ListView properly. It's hooked on ListView not on ListAdapter!

ListView.setOnItemClickListener(listener);

3) If you need to use OnClickListener, check if you do use DialogInterface.OnClickListener or View.OnClickListener (they can be easily exchanged if not validated or if using both of them)

查看更多
小情绪 Triste *
4楼-- · 2020-01-23 11:04

Though a very old question, but I am still posting an answer to it so that it may help some one. If you are using any layout inside the list view then use ...

android:descendantFocusability="blocksDescendants"    

... on the first parent layout inside the list. This works as magic the click will not be consumed by any element inside the list but will directly go to the list item.

查看更多
够拽才男人
5楼-- · 2020-01-23 11:04

Hey check this, works for me... hope it work for u too

If list item contains ImageButton

Problem: OnItemClickListener just doesn’t repond any at all!

Reason: No idea

Solution: in code, set ImageButton's focus to “false”

1: ImageButton button = (ImageButton) convertView.findViewById(R.id.imageButton);

2: button.setFocusable(false);

查看更多
该账号已被封号
6楼-- · 2020-01-23 11:04

I have an Activity that extends ListActivity.

I tried doing something like this in onCreate:

ListView listView = getListView();
listView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {

        Log.i("Hello!", "Y u no see me?");

    }

});

But that didn't work.

Instead I simply needed to override onListItemClick:

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

    Log.i("Hello!", "Clicked! YAY!");

}
查看更多
祖国的老花朵
7楼-- · 2020-01-23 11:04

You can also use lambda. Lambda syntax is not supported under Java 1.7 or earlier JVMs.

listView.setOnItemClickListener((parent, view, position, id) -> {
    ...
});
查看更多
登录 后发表回答