How to reference OnListItemClick in ListActivity

I am working on an Android application and am having difficulty with ListActivity . I would like to have a different Activity launch, depending on which item in the list is clicked.

I made a list and call it setListAdapter in java, but I'm not sure how to reference in OnListItemClick . I assume that I need to reference a position in the list.

With Activity and Buttons I can set OnClickListener and use the switch with case for each Button . What is equivalent for ListActivity ?

Here is my code:

 public class Options extends ListActivity implements { String myHistory[]= { "Item 1", "Item 2", "Item 3" }; //---Set ListView--- @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setListAdapter(new ArrayAdapter<String> ( Options.this, android.R.layout.simple_list_item_1, myHistory))); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { // TODO Auto-generated method stub super.onListItemClick(l, v, position, id); //--if click position 1, do below //--Intent a = new Intent("show Item 1 corresponding page"); //--startActivity(a); //--if click item in position 2, do below //--Intent b = new Intent("show Item 2 corresponding page"); //--startActivity(b); //--if click item in position 3, do below //--Intent c = new Intent("show Item 3 corresponding page"); //--startActivity(c); } } 
+2
source share
4 answers

since there is only a few and some data in the list is not at run time, because each element has a separate action (for example, a list of the game menu), so I suggest going short and simple .......

  protected void onListItemClick(ListView l, View v, int position, long id) { // TODO Auto-generated method stub super.onListItemClick(l, v, position, id); Intent a; switch(position){ case 1: a = new Intent("show Item 1 corresponding page"); break; case 2: a = new Intent("show Item 2 corresponding page"); break; case 3: a = new Intent("show Item 3 corresponding page"); break; if(null!=a) startActivity(a); } } 
+2
source

I'm not sure there are other ways to do this, but I do it this way: I installed a click listener in the activity class (and not on the adapter, which I think makes more sense). SAve an array of classes that you want to call:

 Class[] classList = new Class[]{class1.class, class2.class....}; 

add listener to watchlist

  lv.setOnItemClickListener(listviewClickListener()); 

Then the onItemClickListener method:

 private OnItemClickListener listviewClickListener() { OnItemClickListener clickListener = new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, final int position, long id) { //And then call the corresponing one Intent I= new Intent(thisActivity, classList[i]); int id = listOfObjects.get(position).getId();//do whatever you want with the object on the postition "position" Bundle bundle = new Bundle(); bundle.putInt("id", id); i.putExtras(bundle); thisActivity.startActivity(i); } }; return clickListener; } 

I have not tested an array of class literals, but I think this should work.

Edit: I consider that you want to create different actions for each element (those who think about it do not make sense (having no context), since all elements in the list form belong to the same group, and therefore probably can be used in the same way ) Otherwise, you do not need a list of actions, and just add the one you want to the intent.

+5
source

you will need to use the Adapter object and set it using the ListView object.

 ListView listView= getListView(); Adapter medicineListAdapter = new ArrayAdapter<String>( Options.this, android.R.layout.simple_list_item_1, myHistory); listView.setAdapter(medicineListAdapter); 

Add this and the class implements onItemClickListener . This applies to the onItemClick method.

0
source
 onListItemClick(ListView l, View v, int position, long id) position Corresponding your item of myHistory you can judge position to do something : switch (position) { case 1: Intent a = new Intent("show Item 1 corresponding page"); //--startActivity(a); 

etc.

0
source

All Articles