0

我是 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;

                                    }

        });
4

1 回答 1

0

将布尔数组添加到维护检查的活动中。

boolean checked[] = new boolean[beachstuffbaby.length];

将所有选中的位置设置到此数组中。从此数组中创建一个逗号分隔的项目字符串并放入SharedPreferences.

通过从首选项中读取此数组并将字符串拆分为逗号来恢复状态。

于 2012-08-05T11:17:02.467 回答