如何在 ListView 中滚动时保存 CheckBox 的状态?
我已将 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);
}
I have used ListView with custom adapter, so my ListView includes CheckBox, but problem is that when I scroll down, checked CheckBoxes uncheck, how can I do it with this code?
UPDATE
My custom adapter:
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;
}
}
And my Activity:
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是否有标准解决方案,但这应该可以解决问题
:
我不完全确定 Android中 checkItems = new ArrayList;.您必须在适配器构造函数中传递此列表!
如果你想检查你的
ListView
中的第n个项目是否被选中,只需查看n是否保存在checkedItems中,即我真的建议你不要自己膨胀视图。
I'm not entirely sure whether there is a standard solution in Android, but this should do the trick:
}
In your
ListActivity
, add a fieldprivate List<Integer> checkedItems = new ArrayList<Integer>;
. You have to pass this List in the Adapter constructor!If you want to check whether the nth item in your
ListView
was selected, just look whether n is saved in checkedItems, i.e.I really recommend you not to inflate the views by yourself.
试试这个:(您的自定义适配器)
}
希望这有帮助:)
Try this: (your custom adapter)
}
Hope this helps :)
你应该使用
MyAdapter newad=(MyAdapter)adapter.getAdapter();
newad.notifyDataSetChanged();
onitem click 方法中的这段代码
MyAdapter 是 Adapter 的类并创建像这样的新对象
在适配器类中,您应该编写一行
LayoutInflater ;
in=(LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
在适配器构造函数中简单
you should use
MyAdapter newad=(MyAdapter)adapter.getAdapter();
newad.notifyDataSetChanged();
this code in onitem click method
and MyAdapter is class of Adapter and make new object like this one
and in adapter class you should have to write a line
LayoutInflater in;
in=(LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
in adapter constructor simple
这应该能让你继续下去:)
This should get you going :)