我已经将ListView与自定义适配器一起使用,因此我的ListView包含CheckBox,但问题是当我向下滚动时,选中CheckBoxes取消选中,我该如何使用此代码执行此操作?
更新
我的自定义适配器:
public class AppsArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private List<Integer> checkedItems;
private final String DefaultApps = "def_apps";
public AppsArrayAdapter(Context context, String[] values, List<Integer> checkedItems) {//XXX
super(context, R.layout.apps, values);
this.context = context;
this.checkedItems = checkedItems;//XXX
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
SharedPreferences myPrefsApps =context.getSharedPreferences("myPrefsApps", Context.MODE_PRIVATE);
String prefNameDefaultApps = myPrefsApps.getString(DefaultApps, "");
String prefNameDefaultAppsVer = myPrefsApps.getString(DefaultAppsVer, "");
LayoutInflater inflater = (LayoutInflater)context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.apps, parent, false);
TextView text_apps = (TextView)rowView.findViewById(R.id.text_apps);
CheckBox check = (CheckBox)rowView.findViewById(R.id.check_apps);
text_apps.setText(prefNameDefaultApps.split("\n")[position]);
check.setChecked(checkedItems.contains((Integer) position));
return rowView;
}
}
我的活动:
public class AppsActivity extends ListActivity {
private final String DefaultApps = "def_apps";
private List<Integer> checkedItems = new ArrayList<Integer>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AppsArrayAdapter adapter = new AppsArrayAdapter(this, prefNameDefaultApps.split("\n"), checkedItems);
setListAdapter(adapter);
}
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
if(checkedItems.contains((Integer) position)) checkedItems.remove((Integer) position);
else checkedItems.add((Integer) position);
}
答案 0 :(得分:0)
// initialise the list
private List<Integer> selection = new ArrayList<Integer>();
for (String str: prefNameDefaultApps.split("\n")) {
selection.add(0);
}
// setting the list
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
if(CheckBox is checked) {//(check if the checkbox is checked )
selection.set(position, 1);
}
else{
selection.set(position, 0);
}
}
// show in the listView
@Override
public View getView(int position, View convertView, ViewGroup parent) {
....
....
check.setChecked(selection.get(position) == 0 ? false : true);
}
这应该让你去:)
答案 1 :(得分:0)
我不完全确定Android中是否有标准解决方案,但这应该可以解决问题:
public class AppsArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private List<Integer> checkedItems;//Here we save the position of the selected items.
private final String DefaultApps = "def_apps";
//This will be invoked when one of the CheckBoxes gets clicked. It will update the checkedItems list.
private View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Integer position = (Integer) v.getTag();
if(checkedItems.contains(position)) checkedItems.remove(position);
else checkedItems.add(position);
}
};
public AppsArrayAdapter(Context context, String[] values, List<Integer> checkedItems) {//XXX
super(context, R.layout.apps, values);
this.context = context;
this.checkedItems = checkedItems;//XXX
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
SharedPreferences myPrefsApps =context.getSharedPreferences("myPrefsApps", Context.MODE_PRIVATE);
String prefNameDefaultApps = myPrefsApps.getString(DefaultApps, "");
//You shouldn't inflate the View everytime getView() is called,
/*LayoutInflater inflater = (LayoutInflater)context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.apps, parent, false);*/
//rather let the superclass handle it as it's more efficient.
View rowView = super.getView(position, convertView, parent);
TextView text_apps = (TextView)rowView.findViewById(R.id.text_apps);
CheckBox check = (CheckBox)rowView.findViewById(R.id.check_apps);
text_apps.setText(prefNameDefaultApps.split("\n")[position]);
check.setChecked(checkedItems.contains((Integer) position));//XXX
check.setTag((Integer) position);//XXX
check.setOnClickListener(onClickListener);//XXX
return rowView;
}
}
在ListActivity
中,添加字段private List<Integer> checkedItems = new ArrayList<Integer>;
。您必须在适配器构造函数中传递此List!
如果要检查ListView
中的第n项是否被选中,只需查看n是否保存在checkedItems中,即
if(checkedItems.contains(requestedItemPosition)) {
//Item was selected.
} else {
//Item was not selected.
}
我真的建议你不要自己夸大观点。
答案 2 :(得分:0)
试试这个:(您的自定义适配器)
public class AppsArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private List<Integer> checkedItems;
private final String DefaultApps = "def_apps";
LayoutInflater inflater;
public AppsArrayAdapter(Context context, String[] values, List<Integer> checkedItems) {//XXX
super(context, R.layout.apps, values);
this.context = context;
this.checkedItems = checkedItems;//XXX
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
SharedPreferences myPrefsApps =context.getSharedPreferences("myPrefsApps", Context.MODE_PRIVATE);
String prefNameDefaultApps = myPrefsApps.getString(DefaultApps, "");
String prefNameDefaultAppsVer = myPrefsApps.getString(DefaultAppsVer, "");
View rowView = convertView;
ViewHolder viewH = new ViewHolder();
if ( rowView == null){
rowView = inflater.inflate(R.layout.apps, parent, false);
viewH.text_apps = (TextView)rowView.findViewById(R.id.text_apps);
viewH.check = (CheckBox)rowView.findViewById(R.id.check_apps);
rowView.setTag(viewH);
}else{
viewH = (ViewHolder) rowView.getTag();
}
viewH.text_apps.setText(prefNameDefaultApps.split("\n")[position]);
viewH.check.setChecked(checkedItems.contains((Integer) position));
return rowView;
}
static class ViewHolder {
public TextView text_apps;
public CheckBox check;
}
}
希望这会有所帮助:)
答案 3 :(得分:0)
LayoutInflater in; 在=(LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 在适配器构造函数简单