Sunday, 26 February 2012

Catch Mutliple ListViews events in the same activity

Dears,

Maybe you want to create many ListViews Dynamically, and you want to catch their events in single listener, you can do that in both ways: if you know the list adapter object name or list object name in
let us assume we have 3 listViews
lv1,lv2,lv3 and 3 lists adapters
la1, la2, la3

we want to set the list item adpaterslike this
lv1.setAdapter(la1);
lv2.setAdapter(la2);
lv3.setAdapter(la3);

and listeners
lv1.setOnItemClickListener(this);
lv2.setOnItemClickListener(this);
lv3.setOnItemClickListener(this);

public void onItemClick(AdapterView parent, View selectedView,
int selectedViewPos, long rowId) {
MyListAdapter myListAdapter = (MyListAdapter) ((ListView) selectedView
.getParent()).getAdapter();
if(myListAdapter==la1){
//write each list item event here or write your code for list1
}else if(myListAdapter==la2){
//write each list item event here or write your code for list2
}else if(myListAdapter==la3){
//write each list item event here or write your code for list3
}

//or

public void onItemClick(AdapterView parent, View selectedView,
int selectedViewPos, long rowId) {
ListView myListView =((ListView) selectedView.getParent());
if(myListView ==lv1){
//write each list item here event or write your code for list1
}else if(myListView ==lv2){
//write each list item here event or write your code for list2
}else if(myListView ==lv3){
//write each list item here event or write your code for list3
}




Mohammad Abu Hmead

No comments:

Post a Comment