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

Time:01-05

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));

    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);
            }


        }
    });

CodePudding user response:

You have a position number as a parameter, so you can optimize your code:

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

            startActivity(i);
      
            }
});

CodePudding user response:

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

CodePudding user response:

you can use direct position like below

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

            startActivity(i);

    }
});
  •  Tags:  
  • Related