Android的OnItemClickListener长的id参数是不是给字段id [英] Android OnItemClickListener long id parameter is not giving field id

查看:96
本文介绍了Android的OnItemClickListener长的id参数是不是给字段id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经生成客户的列表。在点击这个应该打开编辑视图编辑的客户。这里的参数应根据存储在数据库中的通行的_id。但每次路过它在列表中的位置。所以编辑视图打开错误的数据。请帮助。

  lv.setOnItemClickListener(新OnItemClickListener(){
        公共无效onItemClick(适配器视图<> L,视图V,INT位置,长的id){
            乌里customerEditUri = Uri.parse(CustomerBean.CONTENT_URI +/+ ID);
            customerEdit(customerEditUri);
        }
    });

答:
谢谢你们。您的意见帮我解决这个问题。我创建了我CustomerObject类的内部功能如下:

  @覆盖
    公共字符串的toString(){
        返回this.name;
    }

在创建CustomerObject数组在类似的活动如下:

 列表< CustomerObject>客户=新的ArrayList< CustomerObject>();

按照创建ArrayAdapter:

 适配器=新ArrayAdapter< CustomerObject>(这一点,R.layout.list,R.id.customer_name,客户);

终于拨通setOnItemClickListener()是这样的:

  lv.setOnItemClickListener(新OnItemClickListener(){
        公共无效onItemClick(适配器视图<> L,视图V,INT位置,长的id){
            CustomerObject custObj = adapter.getItem(位置);
            乌里customerEditUri = Uri.parse(CustomerBean.CONTENT_URI +/+ custObj.pkid);
            customerEdit(customerEditUri);
        }
    });


解决方案

您必须设置你希望它在适配器返回的ID,您用来将数据绑定到ListView列表视图适配器。

如果我没看错的,方法是在下面的方法名称下的适配器类:

 众长getItemId(INT位置){
  返回myitem [位置] .getId();
}

返回相应的ID会得到你想要的结果。

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);
        }
    });

解决方案

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.

这篇关于Android的OnItemClickListener长的id参数是不是给字段id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆