I have a ListView, with items with this layout:
<LinearLayout ...>
<LinearLayout ...>
<!--Elements -->
</LinearLayout>
<LinearLayout ...>
<!--Elements -->
</LinearLayout>
</LinearLayout>
So my items have two different sections. I want to be able to setup different onclickListener inside each item(one for each LinearLayout). I have tried so far to override the onListItemClick, but it doesnt seem to work properly:
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
ViewGroup vg = (ViewGroup)v;
vg.getChildAt(0).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mCtx, "element1", Toast.LENGTH_SHORT).show();
}
});
vg.getChildAt(1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mCtx, "element2", Toast.LENGTH_SHORT).show();
}
});
}
Any ideas how to tackle the problem?
Thanks.
You have to use set
onclicklistener
to all elements inside ListView. I have created a listview. set an adapter to the listview. My adapter is reponsible to show each element of the listview. so insidegetView()
I will assign onclicklistener to all the elemnts to whom i want to get the click event.Example
Thanks Deepak
I might be missing something here, but isn't the neat version to go with setting:
And then implement:
The above solutions has been available since API level 1
You dont need a OnClickListener for each list item, but you do need to find each listview, e.g. in your activity OnCreate
then your onListItemClick becomes -
Unless I've misunderstood what your trying to do.