Ali
Ali

Reputation: 469

Android ListView has some items exactly same, how distinguish which one is clicked?

I have a ListView in my Android app, which it has some item with exactly same name. How can distinguish which one is clicked? Is there any solution? For example, I have retrieved these rows from database:

enter image description here

How do I send their ID and receive back their ID by selecting the name in ListView? (I just want to display item's name)

Upvotes: 2

Views: 109

Answers (3)

Sergey
Sergey

Reputation: 65

String[] locations = {"russia", "ukraine", "brazil"};
locationList = new ArrayList<String>(Arrays.asList(locations));
search_listview_result = (ListView)findViewById(R.id.search_listview_result);
searchResultListViewAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_2, android.R.id.text1, locationList);
search_listview_result.setAdapter(searchResultListViewAdapter);
search_listview_result.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            SharedPreferences.Editor editor = getSharedPreferences("SelectedLocation", MODE_PRIVATE).edit();
            editor.putString("Name", locationList.get(position));
            editor.putString("Lat", String.valueOf(locationLatList.get(position)));
            editor.putString("Long", String.valueOf(locationLongList.get(position)));
            editor.commit();

            finish();
        }
    });

Upvotes: 0

Shreshth Kharbanda
Shreshth Kharbanda

Reputation: 1838

With the position is:

plansList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            int intPosition = position;
            String clickedValue = plansList.getItemAtPosition(intPosition).toString();
        }
    });

Upvotes: 0

Ajay Sivan
Ajay Sivan

Reputation: 2989

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
           // use the position to distinguish the view clicked
        });
    }

Upvotes: 2

Related Questions