New Activity from ListView Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of New Activity from ListView without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I’m working on a music sheet app, and have a bunch of songs in a ListView.

I’m using an onItemClick method, but the problem is, I don’t know how to open an activity depending on what subitem is selected.

The array of songs is uta[], so I can find the specific String with uta[position], but how can I open a specific activity based off of the position that is picked by the user in the ListView?

Answer

You can make a switch/case statement on the String that you fetch with uta[position]

public void onItemClick(AdapterView parent, View v, int position, long id) {
    String value = uta[position].getValue();
    switch(value){
        case "value1":
            Intent intent = new Intent(this, activity1.class); startActivity(intent);
        break;

        case "value2":
            Intent intent = new Intent(this, activity2.class); startActivity(intent);
        break;

        case "value3":
            Intent intent = new Intent(this, activity3.class); startActivity(intent);
        break;
    }
}

Note: switch/case statement on Strings requires JDK 7, see the Oracle documentation.

We are here to answer your question about New Activity from ListView - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji