0

I have generated a list of customers. On click this should open edit view to edit the customer. Here the parameter should pass the _id of the row according to stored in the database. But everytime passing it's position in the list. So the edit view is opening wrong data. Please help.

    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> l, View v, int position, long id) {
            Uri customerEditUri = Uri.parse(CustomerBean.CONTENT_URI + "/" + id);
            customerEdit(customerEditUri);
        }
    });

Answer: Thank you all. Your comments helped me to solve this. I have created following function inside my CustomerObject class:

@Override
    public String toString() {
        return this.name;
    }

After that created an array of CustomerObject in activity like following:

List<CustomerObject> customers = new ArrayList<CustomerObject>();

Created ArrayAdapter by following:

adapter = new ArrayAdapter<CustomerObject>(this, R.layout.list, R.id.customer_name, customers);

Finally called setOnItemClickListener() like this:

    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> l, View v, int position, long id) {
            CustomerObject custObj = adapter.getItem(position);
            Uri customerEditUri = Uri.parse(CustomerBean.CONTENT_URI + "/" + custObj.pkid);
            customerEdit(customerEditUri);
        }
    });
ray
  • 4,210
  • 8
  • 35
  • 46

4 Answers4

1

You will have to set the ID you would like it to return in the adapter, the List View Adapter that you used to bind data to the ListView.

If I am not wrong, the method is in the adapter class under the following method name:

public long getItemId(int position) {
  return myitem[position].getId();
}

Returning the appropriate ID will get you the results you wanted.

questions.
  • 26
  • 1
  • 4
0

I believe that the "long id" is not the record id but the internally generated view id.

If you want to get back to the datasource id then you need to use position and something like:

// Assuming datasource is an ArrayAdapter<Customer>
Customer customer = customerAdapter.getItemAtPosition(position);

// then you can do
Uri customerEditUri = Uri.parse(CustomerBean.CONTENT_URI + "/" + customer.getId());
customerEdit(customerEditUri);
Corey Scott
  • 2,430
  • 3
  • 28
  • 33
  • I haven't found any method call getId() under customer. Do I need to create a method inside Customer class? – ray Feb 20 '13 at 08:54
  • I'm sorry I was guessing at the contents of your Customer class. You should replace it with whatever is appropriate based on your implementation – Corey Scott Feb 20 '13 at 10:50
0

Replace id with position.

Use

 lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> l, View v, int position, long id) {
            Uri customerEditUri = Uri.parse(CustomerBean.CONTENT_URI + "/" + position);
            customerEdit(customerEditUri);
        }
    })
RobinHood
  • 10,897
  • 4
  • 48
  • 97
0

In my opinion, through the position, you can get row item with adapter's getItem(position). So, the position mean the data position in the adapter.

For the id parameter, I think it is a help method, as you know, the data in adapter always is a recorder. general speaking, a recorder should have an id column(something like the database id). when coding, you can get the item through position, then get the item's id(if the item has id). but you can get such "id" directly with "id" parameter.

By the way, if you want use the id parameter, you have to implement the getItemId() method in adapter. the default implement in ArrayAdapter is just return the position.

David Guo
  • 1,749
  • 3
  • 20
  • 30