Showing posts with label ListAdapter. Show all posts
Showing posts with label ListAdapter. Show all posts

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

Wednesday, 22 February 2012

IconAndTextListAdapter

// Comment
public class IconAndTextListAdapter extends ArrayAdapter {

private final Context context;
private final String[] strings;
private final int[] iconsIDs;
private final int rowLayoutID, iconViewID, textViewID;
private View rowView ;


/**
* @param context
* @param rowLayoutID
* @param iconsIDs
* @param strings
* @param iconViewID
* @param textViewID
*/
public IconAndTextListAdapter(Context context, int rowLayoutID,
int[] iconsIDs, String[] strings, int iconViewID, int textViewID) {
super(context, rowLayoutID, strings);
this.context = context;
this.strings = strings;
this.iconsIDs = iconsIDs;
this.rowLayoutID = rowLayoutID;
this.iconViewID = iconViewID;
this.textViewID = textViewID;
// TODO Auto-generated constructor stub
}
static class ViewHolder {
public TextView text;
public ImageView image;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(rowLayoutID, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) rowView.findViewById(textViewID);
viewHolder.image = (ImageView) rowView.findViewById(iconViewID);
rowView.setTag(viewHolder);

}

ViewHolder holder = (ViewHolder) rowView.getTag();

holder.text.setText(strings[position]);

holder.image.setImageResource(iconsIDs[position]);

return rowView;
}

}



Mohammad Abu Hmead