Reputation: 495
I have a list view that is built using a simpleAdapter, each row is defined by a Layout containing a TableRow and a few TextView.
What should I do to know on which TextView (in the layout) was clicked on?
I am able to get the listview row (position) if I use a OnItemClickListener on the Listview, however it dosen't tell me on which TextView on that row was clicked on.
I would like to take different action depending on which TextView a user click on in a listview
listview
-----------------------
TextView1 : TextView2 | <--Row1
-----------------------
TextView1 : TextView2 | <--Row2
-----------------------
TextView1 | TextView2 | <--Row3
-----------------------
I would like to be able to tell, that row 2's TextView2 was click on.
Hopefully I am explaining myself well enough.
Thanks you
Upvotes: 1
Views: 5012
Reputation: 998
I needed to do something similar to set a tag on elements within the list view, so I could find which button was clicked
myAdapter = new SimpleAdapter(this, myArrayList, R.layout.list_item,
new String[] {"ItemID", "ItemText", "ItemImage"},
new int[] {R.id.item_id, R.id.item_text, R.id.item_image})
{
@Override
public View getView (int row, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
view.findViewById(R.id.item_id).setTag(row);
view.findViewById(R.id.item_text).setTag(row);
view.findViewById(R.id.item_image).setTag(row);
return view;
}
};
I added an onClick event for each item in the layout
<TextView
...
android:clickable="true"
android:onClick="myClickEvent"
...
/>
And then in my onClick events I had something like
public void myClickEvent(View v) {
Object o = v.getTag();
if (o instanceof Integer) {
int row = (Integer) o;
// do something
}
You can either use one onClick event per item in the view to determine which item was clicked, or you can have one onClick event for all and modify the tag to contain both the item and the row.
Upvotes: 0
Reputation: 3193
you can handle the click of different view's in Adapter.The Adapter must be customized
Upvotes: 0
Reputation: 1833
you can use below code in your List Activity
setListAdapter(new ArrayAdapter<String>(this, R.layout.your name, String Name));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
}
Upvotes: 3
Reputation: 33771
If the click is being handled by onItemClick
, then you cannot know from this which View
WITHIN that View
was clicked. An OnItemClickListener
just handles clicking a list item. What you might try is using a custom adapter
in which you grab hold of all the Views in the row in getView()
and set onClickListeners
on them there. Or you can define the onClickListeners
directly in your View
class if possible.
Upvotes: 1