0

How to create a custom listview populated with data from a webservice, the data contains item with an ID, and a NAME. the NAME should be visible in the listView but when a user clicks on the item, i want to get the ID. i.e the ID from the server and not the ListView item id.

All i want is to be able to add my own item "properties" that i can get when the user clicks on an item

Christopher M.
  • 130
  • 1
  • 9

1 Answers1

1

Create a set-get class like:

private class NameDetails{
    private final String id;
    private final String name;

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

    public String getid() {
        return id;
    }

    public String getname() {
        return name;
    }
}

Create a list containing the name and ids like:

List<NameDetails> namedetailssetget ;
namedetailssetget = new ArrayList<NameDetails>();

Now whenever you are getting name and ids,add them to the list like:

namedetailssetget.add(new NameDetails(id,name));

In this way you can add all your data to the list.

To retrieve:

Within the onItemclickListner of listview write:

if(namedetailssetget.size()>0){
    NameDetails item = namedetailssetget.get(position);
    String id=item.getid();
    String name=item.getname();
}

N.B. The above position is the position of the item of the listview clicked.

kgandroid
  • 5,219
  • 5
  • 34
  • 65