Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to return the index of an item clicked on a list view android studion code

as my code shows I would like to open a new activity bases on the position of the item click on the list view. I have this if else statement for each position. it works fine because my list only takes 3 items but what if i have a long list(like 20 items) ? how can i get the position of the item clicked?.

Thank you for taking your time to answer. this is my current code

lv_places.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, savedAddresses));

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    lv_places.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
           

            if(position == 0) {
                // open map activity
                Intent i = new Intent(showSavedLocationsList.this, MapsActivity.class);
                i.putExtra("lat", savedLocations.get(0).getLatitude());
                i.putExtra("long", savedLocations.get(0).getLongitude());

                startActivity(i);
            }
            else if(position == 1){
                // open map activity
                Intent i = new Intent(showSavedLocationsList.this, MapsActivity.class);
                i.putExtra("lat", savedLocations.get(1).getLatitude());
                i.putExtra("long", savedLocations.get(1).getLongitude());

                startActivity(i);
            }
            else if(position == 2){
                // open map activity
                Intent i = new Intent(showSavedLocationsList.this, MapsActivity.class);
                i.putExtra("lat", savedLocations.get(2).getLatitude());
                i.putExtra("long", savedLocations.get(2).getLongitude());

                startActivity(i);
            }
            else if(position == 3){
                // open map activity
                Intent i = new Intent(showSavedLocationsList.this, MapsActivity.class);
                i.putExtra("lat", savedLocations.get(3).getLatitude());
                i.putExtra("long", savedLocations.get(3).getLongitude());

                startActivity(i);
            }


        }
    });

>Solution :

lv_places.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, savedAddresses));

    lv_places.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position/*weGetVariableHere*/, long id) {
           
                // open map activity
                Intent i = new Intent(showSavedLocationsList.this, MapsActivity.class);
                i.putExtra("lat", savedLocations.get(position/*variableUsedHere*/).getLatitude());
                i.putExtra("long", savedLocations.get(position/*variableUsedHere*/).getLongitude());

                startActivity(i);
            


        }
    });

We are using the variable itself instead of if-else-elseif it will work for n number of items

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading