简体   繁体   中英

Android setOnItemClickListener

I'm not able to initiate the " OnItemClickListener ".

You can see my code snippet

 ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main, new String[] { "title"}, new int[] { R.id.item_title});
    setListAdapter(adapter);

    final ListView lv = getListView();
    lv.setTextFilterEnabled(true);

    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {              
            @SuppressWarnings("unchecked")
            HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);                   
            Toast.makeText(TopNewsActivity.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_LONG).show(); 

        }
    });

after the setListAdapter my debugger goes to " lv.setOnItemClickListener " but then does not get into the loop and moves out.

I want to make the links Clickable kindly help.

The most probable cause is that your ListView items contain either focusable or clickable Views. If a view contains either focusable or clickable item the OnItemCLickListener won't be called. (Instead the clickable View's own click handlers will be called).

Click here for more information. See my previous answer here or find more information here .

Try it with a very simple ListItem layout - it should work.

Maybe you forgot to write @Override before public void onItemClick?

Adapter.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
        }
});

bbalazs is right. I would like to put it more precisely: If you have a view A as a child of a view B and A is by default clickable(button fe), than setOnItemClickListener won't work on B. It is pure magic, but it works so.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM