如何将SimpleCursorAdapter与single_choice和multiple_choice一起使用

时间:2013-03-28 08:41:42

标签: android adapter simplecursoradapter listadapter

我正在开发一个包含多个答案类型和单一答案类型的测验应用程序。问题和答案存储在sqlite数据库中。我使用简单的游标适配器并从here了解如何使用!我的问题是,如果我的答案类型改变(单个到多个),我怎么能从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);

1 个答案:

答案 0 :(得分: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);

for multiple:

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);

取决于你的要求,你必须设置以上一项:我认为你很难知道哪个问题需要单一的ans&这需要多个依赖于你必须创建列表项单击像

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
               }
            }
        });