ListView with OnItemClickListener android


6 Answers

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);

Question

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!




listPaired = (ListView) findViewById(R.id.listView1);
listPairedData = new ArrayList<String>();
araPaired = new ArrayAdapter(this, android.R.layout.simple_list_item_1, listPairedData);
listPaired.setAdapter(araPaired);
listPaired.setOnItemClickListener(listPairedClickItem);

private OnItemClickListener listPairedClickItem = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

String info = ((TextView) arg1).getText().toString();
Toast.makeText(getBaseContext(), "Item "+info, Toast.LENGTH_LONG).show();
}
 };



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)




lv.setOnItemClickListener(new OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // TODO Auto-generated method stub

        }
    });



if list item view contains button or checkbox or imagebutton, the onitemclicklistener and onitemlongclicklistener not working due to it has own onclick listener.

set focusable as false

holder.button.setFocusable(false);






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

    listView.setOnItemClickListener((parent, view, position, id) -> {

    });



Related