1

我正在开发一个包含多答案类型和单答案类型的测验应用程序。问题和答案存储在 sqlite 数据库中。我使用简单的光标适配器并从这里了解如何使用!我的问题是,如果我的答案类型发生变化(单个到多个),我如何从 simple_list_item_single_choice 切换到 simple_list_item_multiple_choice,以及如何保存选择的答案。请给出一些想法。这里是编码....

  db = new DBAdapter(this);
  SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
  android.R.layout.simple_list_item_1, 
  db.getAllTitles(), 
  new String[] { "title" }, 
  new int[] { android.R.id.text1 });

 ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
4

1 回答 1

2

检查此代码:单选:

 SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
  android.R.layout.simple_list_item_single_choice, 
  db.getAllTitles(), 
  new String[] { "title" }, 
  new int[] { android.R.id.text1 });

 ListView listView = (ListView) findViewById(R.id.list);
 listView.setAdapter(adapter);
 listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

对于多个:

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
  simple_list_item_multiple_choice, 
  db.getAllTitles(), 
  new String[] { "title" }, 
  new int[] { android.R.id.text1 });

 ListView listView = (ListView) findViewById(R.id.list);
 listView.setAdapter(adapter);
 listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

取决于您的要求,您必须设置以上之一:我认为您已经知道哪个问题需要单个答案以及需要多个答案取决于您还必须创建列表项点击

listView.setOnItemClickListener(new OnItemClickListener() {

            private String my_sel_items;

            public void onItemClick(AdapterView arg0, View arg1, int arg2,
                    long arg3) {
                if(ans == multiple){
                my_sel_items = new String("Selected Items");
                SparseBooleanArray a = lView.getCheckedItemPositions();

                for (int i = 0; i < a.size(); i++) {
                    if (a.valueAt(i)) {
                        my_sel_items = my_sel_items + ","
                                + (String) listView.getAdapter().getItem(i);
                    }
                }
                Log.v("values", my_sel_items);
               }else{
                 // for single it default selected item
               }
            }
        });
于 2013-03-28T09:17:32.917 回答