Java 如何为ListView中的每个项目设置id
当我从数据库检索数据时,对于每一行,我都有唯一的ID,我希望将ID与listview中的字符串匹配,但我希望它不可见,这样,当我单击listview中的任何项目时,将我传输到另一个活动时,就有关于该项目的数据 这意味着我在数据库中有两个表一起匹配,我想检索一个作为listview,当单击item trans me以数据匹配我已单击的项目时。一个作为listview,当单击item trans me以数据匹配我已单击的项目时 我怎样才能做到这一点?这应该行得通Java 如何为ListView中的每个项目设置id,java,android,Java,Android,当我从数据库检索数据时,对于每一行,我都有唯一的ID,我希望将ID与listview中的字符串匹配,但我希望它不可见,这样,当我单击listview中的任何项目时,将我传输到另一个活动时,就有关于该项目的数据 这意味着我在数据库中有两个表一起匹配,我想检索一个作为listview,当单击item trans me以数据匹配我已单击的项目时。一个作为listview,当单击item trans me以数据匹配我已单击的项目时 我怎样才能做到这一点?这应该行得通 myAdapter.setViewB
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");
}
}
}
您可以使用自定义适配器并像这样使用方法来实现您的目标
@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;
}
投票赞成你的回答,康纳先生:
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