user1465656
user1465656

Reputation: 153

How to reference OnListItemClick in ListActivity

I am working on an android app, and I am having difficulties with ListActivity. I would like to have a different Activity start depending on which item in the list is clicked.

I made a list and referenced it with setListAdapter in java, but I am not sure how to reference in the OnListItemClick. I assume I need to reference the position in the list.

With Activity and Buttons, I can set the OnClickListener and use a switch statement with a case for each Button. What is the 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's corresponding page");
        //--startActivity(a);

        //--if click item in position 2, do below
        //--Intent b = new Intent("show Item 2's corresponding page");
        //--startActivity(b);

        //--if click item in position 3, do below
        //--Intent c = new Intent("show Item 3's corresponding page");
        //--startActivity(c);
    }
}

Upvotes: 2

Views: 5439

Answers (4)

jues
jues

Reputation: 1

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's corresponding page");
    //--startActivity(a);

and so on...

Upvotes: 0

Dheeresh Singh
Dheeresh Singh

Reputation: 15701

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 .......

    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's corresponding page");
            break;
         case 2:
           a  = new Intent("show Item 2's corresponding page");
           break;
        case 3:
           a = new Intent("show Item 3's corresponding page");
           break;

        if(null!=a)
        startActivity(a);
     }
}

Upvotes: 2

Nuno Gon&#231;alves
Nuno Gon&#231;alves

Reputation: 6805

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() {
    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 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.

Upvotes: 5

Anil Jadhav
Anil Jadhav

Reputation: 2158

you must have to use Adapter object and set that with 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 in Class implements onItemClickListener .This refer that onItemClick method.

Upvotes: 0

Related Questions