Android get data from cursor when click on layout?
I have a layout for my list row with some text and a layout which is supposed to emulate a button. The problem I'm facing is I will always get the latest data in the cursor, meaning if I click the layout on row one I should get grp开发者_开发技巧 id 1 and grp name ONE, but I'm getting grp id 3, grp name THREE regardless of which layout I click on in the listview. What's the way to fix this? My code:
In the getView method
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
holder.text2 = (TextView) vi.findViewById(R.id.text2);
holder.layout1 = (LinearLayout)vi.findViewById(R.id.info);
vi.setTag(holder);
} else {
holder = (ViewHolder) vi.getTag();
}
String grpCount = c.getString(c
.getColumnIndex(TestDbAdapter.KEY_GRP_COUNT));
holder.text2.setText("Channels: " + grpCount);
holder.layout1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
System.out.println(c.getString(c.getColumnIndex(TestDbAdapter.KEY_GRP_NAME)));
System.out.println(c.getString(c.getColumnIndex(TestDbAdapter.KEY_GRP_ID)));
}
});
return vi;
Just check it, I think it should get fix like this:
String grpCount = c.getString(c
.getColumnIndex(TestDbAdapter.KEY_GRP_COUNT));
final String grpName = c.getString(c.getColumnIndex(TestDbAdapter.KEY_GRP_NAME));
final String grpId = c.getString(c.getColumnIndex(TestDbAdapter.KEY_GRP_ID));
holder.text2.setText("Channels: " + grpCount);
holder.layout1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
System.out.println(grpName);
System.out.println(grpId);
}
});
精彩评论