Android scoll时更改复选框状态的listview
可能重复:Android scoll时更改复选框状态的listview,android,Android,可能重复: 我有带复选框的listview。默认情况下,所有复选框都被选中。然后,一些复选框被取消选中,并自动滚动这些复选框。但我希望在滚动listview之后,这些复选框被选中。 我的代码是: public class FriendListAdapter extends BaseAdapter { private LayoutInflater mInflater; DrunkMessages friendsList; boolean[] checkBoxState
我有带复选框的listview。默认情况下,所有复选框都被选中。然后,一些复选框被取消选中,并自动滚动这些复选框。但我希望在滚动listview之后,这些复选框被选中。 我的代码是:
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;
}
在getview方法中添加以下代码
if(checkBoxState.length>0)
checkbox.setChecked(checkBoxState[position]);
您可以使用具有简单列表项多项选择属性的列表视图,而不是为复选框增加另一个xml。像这样
adapter = new ArrayAdapter<String> (this,android.R.layout.simple_list_item_multiple_choice,objectOfList);
adapter=new ArrayAdapter(这是android.R.layout.simple\u list\u item\u多选,objectOfList);
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;
}
您可以使用具有简单列表项多项选择属性的列表视图,而不是为复选框增加另一个xml。像这个适配器=新的ArrayAdapter(这个,android.R.layout.simple\u list\u item\u multiple\u selection,objectOfList);我建议使用简单的checkbox.setChecked(checkBoxState[position]);还有同样的问题,只需通过log catI查看checkBoxState[position]的值,我会得到false,默认情况下也会得到uncheck。为什么我得到假值请给我答案:我希望默认情况下所有都是checkedI使用的自定义listview。这怎么可能?更新了我的答案请现在检查我在第行得到空指针异常:checkBoxState[pos.intValue()]=true;检查我的更新答案你必须像这个复选框一样设置复选框的标签;工作正常。但我希望所有复选框都默认选中。在我的xml中也选中=“true”。但是为什么我在默认情况下未选中所有复选框。请回答我您的数组未初始化,因此请像以下数组一样初始化您的checkBoxState数组。fill(checkBoxState,true);