atari83
atari83

Reputation: 489

Android - ListView Items with specific IDs

I'm developing a small application on android, and in an activity I need to display list of customers. Actually I've done a query (by using Cursor object) which in result, it will output the ID (of customer) and CustomerName.

I need to display the CustomerName in ListView and in meantime, when user tap on a customer on the list, I could get the related ID.

can anyone help me with that ?

Upvotes: 0

Views: 192

Answers (1)

cafebabe1991
cafebabe1991

Reputation: 5176

Just follow these steps

  1. Make a simple class having two variables like name and id class name: customer

  2. now make a adapter class extending base adaptor override the getView method and set the text(name) of the customer

  3. Handle clicks in the in listview onitemclick method

So heres a sample code

Pass the adapter an ArrayList<Customer>

So now your listview will have customer objects

On click you will get the customer object,use the parent.itematposition(position) Cast the result of this method into customer alike this

Customer c=(Customer)parent.itematposition(position);

System.out.print(c.getId+c.getName());

public class Customer{ private Sting name; private String id;

Provide setters and getters for both them so that they can be used above

}

Upvotes: 1

Related Questions