Android setOnItemClickListener

為{幸葍}努か 提交于 2019-12-12 18:26:15

问题


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.


回答1:


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



回答2:


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.




回答3:


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 f.e.), than setOnItemClickListener won't work on B. It is pure magic, but it works so.



来源:https://stackoverflow.com/questions/5714522/android-setonitemclicklistener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!