我是 Android 设备编程的新手。我试图弄清楚如何在使用 simple_list_item_multiple_choice 和数组适配器生成的列表视图中保存复选框。我希望能够保存复选框的状态,以便当用户点击后退按钮转到另一个清单时,他们可以回到这个清单并从他们离开的地方拾取。
求求求救求救!!代码和/或解释将是理想的。
代码:
public class BeachBabyStuff extends Activity {
String[] beachstuffbaby = new String[]{
"Beach Blanket or Mat",
"Beach Towels",
"Beach Umbrella",
"Beach Chair",
"Books / Magazines",
"Radio",
"Pen / Paper",
"Tablet"};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
// Getting the reference to the listview object of the layout
ListView listView = (ListView) findViewById(R.id.listview);
// The checkbox for the each item is specified by the layout android.R.layout.simple_list_item_multiple_choice
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, beachstuffbaby);
// Setting adapter to the listview
listView.setAdapter(adapter);
//Manage the onItemClick method
listView.setOnItemClickListener(new OnItemClickListener() {
private View view;
public void onItemClick(AdapterView<?> ListView, View view, int position, long id) {
CheckedTextView textView = (CheckedTextView)view;
textView.setChecked(!textView.isChecked());
this.view = view;
}
});