2

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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
ReNa
  • 1,114
  • 6
  • 19
  • 37

3 Answers3

8

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.

Community
  • 1
  • 1
balazsbalazs
  • 4,071
  • 1
  • 24
  • 29
6

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) {
        }
});
Anton Derevyanko
  • 3,405
  • 1
  • 24
  • 32
  • 1
    This can not be the reason, method is overridden whether or not there is an @Override annotation. – koders Apr 01 '14 at 08:56
0

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.

Gangnus
  • 24,044
  • 16
  • 90
  • 149