如何为ListView中的每个项设置id

时间:2013-07-31 03:06:54

标签: java android

当我从数据库中检索数据时,我对每一行都有唯一的ID我希望匹配id与listview中的字符串但ID我希望它是不可见的,所以当我点击listview中的任何项目时,转换到另一个活动有数据关于这个项目。

这意味着我有两个数据库匹配表在一起我想要检索一个作为列表视图,当点击项目转换为数据匹配我已被点击的项目。一个作为列表视图,当点击项目转换为数据匹配项目我被点击了。

我该怎么做?

3 个答案:

答案 0 :(得分:1)

这应该有效

myAdapter.setViewBinder(new MyViewBinder());

public class MyViewBinder implements ViewBinder {
@Override
public boolean setViewValue(View view, Object data, String text){
    //Since it iterates through all the views of the item, change accordingly 
    if(view instanceof TextView){ 
        ((TextView)view).setTag("whatever you want");
    }
}
}

答案 1 :(得分:0)

您可以使用自定义适配器并使用此方法来实现您的目标

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;

    if(vi == null){
        vi = inflater.inflate(R.layout.row_layout, null);
    }

    final int id = idGetFuncttion(position);

    vi.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            doSomethingWithID(id);              
        }
    });

    return vi;      
}

答案 2 :(得分:0)

1.set your unique id to each row by setTag property and retrieve it by getTag property
2.use custom adapter and custom listview layout for the listview and set your unique id to invisible textview in custom listview layout through custom adapter