How To Reference Onlistitemclick In Listactivity
Solution 1:
I'm not sure there are other ways of doing it, but I do it this way: I set the click listener on the activity class (not on the adapter one, which I think makes more sense). SAve an array of the classes you wish to call:
Class[] classList = new Class[]{class1.class, class2.class....};
add the listener to the listview
lv.setOnItemClickListener(listviewClickListener());
Then the onItemClickListener method:
private OnItemClickListener listviewClickListener() {
OnItemClickListenerclickListener=newOnItemClickListener() {
publicvoidonItemClick(AdapterView<?> parent, View view, finalint position, long id) {
//And then call the corresponing one
Intent I= newIntent(thisActivity, classList[i]);
intid= listOfObjects.get(position).getId();//do whatever you want with the object on the postition "position"Bundlebundle=newBundle();
bundle.putInt("id", id);
i.putExtras(bundle);
thisActivity.startActivity(i);
}
};
return clickListener;
}
I didn't test the array of class literals, but I guess it should work..
Edit: I'm considering you want to create different activities for each item (which thinking about it doesn't make much sense (not having the context) as all items on a list view belong to the same group, and therefor might probably be used in the same way). Otherwise you don't need the list of activities, and just add the one you wish to the intent.
Solution 2:
as looks there are only few and certain data in list not at run time because each item has a separate activity (like a menu list of game) so I will suggest to go short and simple .......
protectedvoidonListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stubsuper.onListItemClick(l, v, position, id);
Intent a;
switch(position){
case1:
a = newIntent("show Item 1's corresponding page");
break;
case2:
a = newIntent("show Item 2's corresponding page");
break;
case3:
a = newIntent("show Item 3's corresponding page");
break;
if(null!=a)
startActivity(a);
}
}
Solution 3:
you must have to use Adapter object and set that with the ListView Object.
ListView listView= getListView();
AdaptermedicineListAdapter=newArrayAdapter<String>(
Options.this, android.R.layout.simple_list_item_1, myHistory);
listView.setAdapter(medicineListAdapter);
Add this and in Class implements onItemClickListener .This refer that onItemClick method.
Solution 4:
onListItemClick(ListView l, View v, int position, long id)
position Corresponding your item of myHistory
you can judge positionto do something :
switch (position) {
case 1:
Intent a = new Intent("show Item 1's corresponding page");
//--startActivity(a);
and so on...
Post a Comment for "How To Reference Onlistitemclick In Listactivity"