james
james

Reputation: 157

problems opening listView items with intents in android studio

I have a listView with 2 items in the list, the first item in position 0 responds to clicks but the other does not! i have tried to copy the same onclick method and changed the if statement to the list item in position 1 but it is not working out for me so i am looking how to get this working if someone could help me? i have included the only code i think is needed to resolve this,

public class TopLevelActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_top_level);
        //Create an OnItemClickListener
        AdapterView.OnItemClickListener itemClickListener =
            new AdapterView.OnItemClickListener() {
                public void onItemClick(AdapterView<?> listView,
                                        View v,
                                        int position,
                                        long id) {
                    if (position == 0) {
                        Intent intent = new Intent(TopLevelActivity.this,
                                                   DrinkCategoryActivity.class);
                        startActivity(intent);
                    }
                }
            };
        //Add the listener to the list view
        ListView listView = (ListView) findViewById(R.id.list_options);
        listView.setOnItemClickListener(itemClickListener);
    

      //what ive tried to open the foodCategoryActivity list item

AdapterView.OnItemClickListener itemClickListener =
            new AdapterView.OnItemClickListener() {
                public void onItemClick(AdapterView<?> listView,
                                        View v,
                                        int position,
                                        long id) {
                    if (position == 1) {
                        Intent intent = new Intent(TopLevelActivity.this,
                        
                        FoodCategoryActivity.class);
                        startActivity(intent);
                    }
                }
            };
        //Add the listener to the list view
        ListView listView = (ListView) findViewById(R.id.list_options);
        listView.setOnItemClickListener(itemClickListener);
    }
}

thank you.

Upvotes: 0

Views: 134

Answers (2)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132992

Currently using two different click listeners for same ListView to do different on different items click in ListView with is not valid way to do task according to click position in ListView.

Use single click listener and inside onItemClick method use switch-case or if-else ladder for doing task like:

AdapterView.OnItemClickListener itemClickListener =
        new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> listView,
                                    View v,
                                    int position,
                                    long id) {
                if (position == 0) {

                }
                else  if (position == 1) {{

                }
            }
        };

and also remove following line which you are using two times:

ListView listView = (ListView) findViewById(R.id.list_options);
listView.setOnItemClickListener(itemClickListener);

because no need to create listView and call setOnItemClickListener multiple times just do it once.

Upvotes: 2

breakline
breakline

Reputation: 6093

You can only set one OnItemClickListener

you need to differentiate in the implemented method via either the position or the View object itself:

AdapterView.OnItemClickListener itemClickListener =
        new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> listView,
                                    View v,
                                    int position,
                                    long id) {
                if (position == 0) {
                  //code for drink category
                }
                else {
                  //code for food category
                }
            }
        };
    //Add the listener to the list view
    ListView listView = (ListView) findViewById(R.id.list_options);
    listView.setOnItemClickListener(itemClickListener);

Upvotes: 2

Related Questions