ReNa
ReNa

Reputation: 1124

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.

Upvotes: 2

Views: 22961

Answers (3)

Gangnus
Gangnus

Reputation: 24464

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.

Upvotes: 0

balazsbalazs
balazsbalazs

Reputation: 4081

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.

Upvotes: 8

Anton Derevyanko
Anton Derevyanko

Reputation: 3445

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

Upvotes: 6

Related Questions