可能重复:
Getting an issue while checking the dynamically generated checkbox through list view
我有listview with checkboxes.Bydefault所有复选框都被选中。之后一些复选框被取消选中并滚动那些复选框被自动检查。但我想滚动列表视图后检查这些复选框。 我的代码是:
public class FriendListAdapter extends BaseAdapter {
private LayoutInflater mInflater;
DrunkMessages friendsList;
boolean[] checkBoxState;
public FriendListAdapter(DrunkMessages friendsList) {
this.friendsList = friendsList;
if (Utility.model == null) {
Utility.model = new FriendsGetProfilePics();
}
Utility.model.setListener(this);
mInflater = LayoutInflater.from(friendsList.getBaseContext());
checkBoxState=new boolean[jsonArray.length()];
}
public int getCount() {
return jsonArray.length();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(final int position, View convertView, ViewGroup parent) {
View hView = getLayoutInflater().inflate(R.layout.friendslist, null);
ImageView profile_pic = (ImageView) hView.findViewById(R.id.profile_pic);
TextView name = (TextView) hView.findViewById(R.id.name);
final CheckBox checkbox=(CheckBox)hView.findViewById(R.id.checkbox);
profile_pic.setImageBitmap(Utility.model.getImage(friendid[position], pictures[position]));
name.setText(names[position]);
checkbox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(((CheckBox)v).isChecked()){
checkBoxState[position]=true;
status[position]="1";
Log.e("checked","checked");
Log.e("Checked",status[position]);
}
else{
checkBoxState[position]=false;
status[position]="0";
Log.e("checked","unchecked");
Log.e("Checked",status[position]);
}
}
});
return hView;
}
答案 0 :(得分:3)
convertview可能会为您提供以前生成的视图,因此不要依赖它。只需将您的复选框状态存储在一个布尔数组中。相应的改变取决于checkchangelistener回调这里是你的代码我只是修改你的代码的getView方法一切保持原样
public View getView(final int position, View convertView, ViewGroup parent) {
View hView = getLayoutInflater().inflate(R.layout.friendslist, null);
ImageView profile_pic = (ImageView) hView.findViewById(R.id.profile_pic);
TextView name = (TextView) hView.findViewById(R.id.name);
final CheckBox checkbox=(CheckBox)hView.findViewById(R.id.checkbox);
checkbox.setTag(new Integer(position));
profile_pic.setImageBitmap(Utility.model.getImage(friendid[position], pictures[position]));
name.setText(names[position]);
checkbox.setOnCheckedChangeListener(null);
if(checkBoxState[position])
checkbox.setChecked(true);
else
checkbox.setChecked(false);
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Integer pos = (Integer)buttonView.getTag();
if(isChecked){
checkBoxState[pos.intValue()]=true;
}
else{
checkBoxState[pos.intValue()]=false;
Log.e("checked","unchecked");
}
}
});
return hView;
}
答案 1 :(得分:0)
在getview方法中添加以下代码
if(checkBoxState.length>0)
checkbox.setChecked(checkBoxState[position]);
答案 2 :(得分:0)
您可以使用simple_list_item_multiple_choice属性获取listview,而不是为另一个xml充气。像这样
adapter = new ArrayAdapter<String> (this,android.R.layout.simple_list_item_multiple_choice,objectOfList);