嗨在下面的代码中,我使用自定义适配器显示带有图像和复选框的名称。现在,我的问题是基于我想添加的朋友位置复选框。
我的布局是这样的:
带有复选框的图片好友名称
现在,我希望基于朋友位置我想要返回复选框添加到朋友或不。如果我选择用户名,则选择用户:user1这个被检查。然后我想返回用户名,检查位置为我我正在使用listview。
的java
private class FriendListAdapter extends BaseAdapter {
class ViewHolder {
TextView text;
ImageView icon;
CheckBox check1;
}
private LayoutInflater mInflater;
private Bitmap mOnlineIcon;
private Bitmap mOfflineIcon;
private FriendInfo[] friends = null;
public FriendListAdapter(Context context) {
super();
mInflater = LayoutInflater.from(context);
mOnlineIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.greenstar);
mOfflineIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.redstar);
}
public void setFriendList(FriendInfo[] friends)
{
this.friends = friends;
}
public int getCount() {
return friends.length;
}
public FriendInfo getItem(int position) {
return friends[position];
}
public long getItemId(int position) {
return 0;
}
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.grouplist, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
holder.check1 = (CheckBox) convertView.findViewById(R.id.checkBox1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(friends[position].userName);
holder.icon.setImageBitmap(friends[position].status == STATUS.ONLINE ? mOnlineIcon : mOfflineIcon);
holder.check1.setChecked(checkBoxState[position]);
holder.check1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
checkBoxState[position] = isChecked;
}
});
return convertView;
}
}
答案 0 :(得分:0)
您可以使用
holder.check1.setTag(getItem(position));
为每一行设置一个标签,然后yoju可以使用
检查标签 int position = (Integer) buttonView.getTag();
这将返回位置
答案 1 :(得分:0)
holder.text.setText(friends[position].userName);
holder.icon.setImageBitmap(friends[position].status == STATUS.ONLINE ? mOnlineIcon : mOfflineIcon);
holder.check1.setChecked(checkBoxState[position]);
holder.check1.setTag(position);
holder.check1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int position = (Integer) buttonView.getTag();
checkBoxState[position] = isChecked;
}
});