0

I'm working on listview on android, each item as its own id and I don't want to display it to the users for example

List<String> carModelList = new ArrayList<>();
carModelList.add("car1");
carModelList.add("car2");
 adapter = new CustomView(this, (ArrayList<String>) carModelList);
 mListview.setAdapter(adapter);

Now i know the output will be

car1
car2

But what i need is

List<String> carModelList = new ArrayList<>();
carModelList.add("hidden unique id","car1");//like <option value="hidden unique id">car1</option>
carModelList.add("hidden unique id","car2");
 adapter = new CustomView(this, (ArrayList<String>) carModelList);
 mListview.setAdapter(adapter);

the output should be like

car1
car2

//when i set the OnItemClickListener
mListview.setOnItemClickListener((parent, view, position, id) -> {

     mListview.getItemAtPosition(position).toString()// should give car1 or car2
     mListview.getHiddenUniqueIdAtPosition(position).toString()//should give hidden unique id

}
Pavan Kumar
  • 164
  • 15
  • you can use `view.setTag(id,"hidden_id")` and `view.getTag()` to reterieve it. Do some research you will find many examples. In case you are unable to do so let me know I will post sample. – Shadow Droid Oct 31 '19 at 10:28
  • @ShadowDroid thank you for your time, I'll try this and let you know – Pavan Kumar Oct 31 '19 at 10:30
  • Also here by `view` i am referring is not listview, but itemview basically individual rows inside listview. – Shadow Droid Oct 31 '19 at 10:32
  • I cannot do this because I need to add the unique id's while adding the content into the carModelList i.e before creating listview – Pavan Kumar Oct 31 '19 at 10:34
  • I guess you have `carModelList` with property of `carModelName` and `UniqueId`. While generating row(itemView) in Adapter I guess you are only setting carModelName to `TextView` , you can also use `setTag` there. And in `setOnItemClickListener` you can go for `getTag` – Shadow Droid Oct 31 '19 at 10:44
  • You should create a new Object that holds the values you want(like id, carName). After that you should implement custom listview adapter. You can find in this link https://stackoverflow.com/a/8166802/10770362 – Ensar Bayhan Oct 31 '19 at 10:50

1 Answers1

2

You should use Model (POJO) to handle this. Check below:

class ListItem {
    String id;
    String name;

    public ListItem(String id, String name) {
        this.id = id;
        this.name = name;
    }
}

Change your adapter's constructor to handle this data type

public CustomView(Context context, ArrayList<ListItem> carModelList) {
}

And then pass this List to Adapter and show only name in getView

List<ListItem> carModelList = new ArrayList<>();
carModelList.add(new ListItem("hidden unique id","car1"));
carModelList.add(new ListItem("hidden unique id","car2"));
adapter = new CustomView(this, (ArrayList<ListItem>) carModelList);
mListview.setAdapter(adapter);

Get Id from OnItemClickListener like below:

mListview.setOnItemClickListener((parent, view, position, id) -> {
     ListItem listItem = mListview.getItemAtPosition(position);
     String id = listItem.id; // this is hidden id
}
Md. Asaduzzaman
  • 14,963
  • 2
  • 34
  • 46
  • Thank you for your time Md. Asaduzzaman but since I'm using custom listview the action button is in another class so if I use this method it becomes complicated but for now I changed my logic and will definitely use your concept in the future – Pavan Kumar Nov 01 '19 at 07:52