I have a bunch of items on my ListView, but I don't know how to implement a listener on a specific item. I also tried to use the if statement and still didn't work.
I tried this code:
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position,long id)
{
if(list.getItemAtPosition(position).equals(mStrings[0]))
{ 开发者_运维知识库
Intent i = new Intent(MainActivity.this, Activity2.class);
startActivity(i);
}
}
});
}
But this code is applied to all items.
just put setListAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1,String[] to display as List));
then you can came to know which item is clicked.....
arg1 is the View that was clicked inside of your ListView. position is the index of that item in your ListView. If you passed an ArrayList of Strings into your ListAdapter, you would call stringArray.get(position) to get the item that was clicked.
I was facing a similar situation after adding this android:descendantFocusability="blocksDescendants"
to my list view xml i was able to get my on click listener work properly
精彩评论