简体   繁体   中英

how to start a activity clicking a list item in listactivity class

i want start activities based on the list item i click. i created a listactivity class and i have already done the arrayadpter stuff and all but in the i want to start an activity based on the item i clicked. like if the item name is Menu and i click on it, it should start the menuactivity class. same goes for the rest.

i tried this

@Override
public void onListItemClick(ListView l, View v, int position, long id){
// getting the position and launching the activity associated with it
switch(position){
startActivity(new Intent(MainActivity.this, MenuList.class));
break;
default:
break;
}
}

All it does is, go blank and return me back to the MainActivity class. somebody help me here. In the list is MenuList, Beverages, drinks and contact and i want when i click on each list item, it launches the activity associated with it

You have to choose the correct Activity based on the clicked item. If you have a static list of items, eg, Menu, Beverages, Drinks , you could write something like this:

@Override
public void onListItemClick(ListView l, View v, int position, long id){
    // getting the position and launching the activity associated with it
    switch(position){
    case 0:
        startActivity(new Intent(MainActivity.this, MenuList.class));
        break;
    case 1:
        startActivity(new Intent(MainActivity.this, Beverages.class));
        break;
    case 2:
        startActivity(new Intent(MainActivity.this, Drinks.class));
        break;
    default:
        break;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM