【问题标题】:ListView with Multiple choices具有多项选择的 ListView
【发布时间】:2011-11-02 22:30:35
【问题描述】:

我在 ListView 中有一个字符串数组,该数组设置为在用户完成时选择多个选项,并检查“完成”。我想使用 Intent 创建一个新 Activity

String[] names = new String[] {"Ham","Cheese","Lettuce", "Bacon", "Done"};
    setListAdapter(new ArrayAdapter<String>(this,
                        android.R.layout.simple_list_item_multiple_choice,
                    android.R.id.text1, names));
    ListView listView = getListView();
    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

我在下面尝试做的是基于用户检查的信息,我想将某些信息放入新的列表视图中,然后在他们单击 startActivity 的“完成”后向他们显示列表视图。我不确定这是否是解决此问题的正确方法。

Intent e = new Intent(getApplicationContext(), FormedList.class);
      if(listView.getCheckItemIds().toString().equals("Ham"))
          e.putExtra("Meat", selectedChildren);
      if(listView.getCheckItemIds().toString().equals("Cheese"))
          e.putExtra("Dairy", selectedChildren);
      if(listView.getCheckItemIds().toString().equals("Bacon"))
          e.putExtra("Swine", selectedChildren);
      if(listView.getCheckItemIds().toString().equals("Done"))
           startActivity(e);

【问题讨论】:

    标签: android android-listview


    【解决方案1】:

    喜欢this example

     ArrayList<Integer> extra_array = new ArrayList<Integer>();
         for (long i : mContactList.getCheckedItemIds()) {
    
        Log.v(TAG, "getCheckItemIds(): id = " + i);
        extra_array.add((int) i);
      }
    

    数组包含所选项目的索引。您可以将这些项目放在一个字符串数组中,并将其作为意图传递给下一个活动。

    更多解释:getCheckedItemIds() 返回一个数组long [],其中包含检查项目的位置(数量)。您应该遍历数组并使用索引从names中选择选中的项目

    【讨论】:

    • //new listview with selected elements ArrayList&lt;String&gt; selectedChildren = new ArrayList&lt;String&gt;(); //Create an Intent for the next class Intent formed = new Intent(this, FormedList.class); for(long i : listView.getCheckItemIds() ) { selectedChildren.add((int) i, null); Log.v(TAG, "getCheckItemIds(): id = " + i); if(selectedChildren.equals("Done")) startActivity(formed); } @Reno 我不确定这是否正确,但我会谈到你提到的。我没有收到任何错误,但是当我点击“完成”选项时,新活动没有开始。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多