【问题标题】:Returning values from multiple selection ListView从多选 ListView 返回值
【发布时间】:2023-03-20 23:16:01
【问题描述】:

编辑:好的,我找到了解决方案。不知道这是正确的解决方案,但它确实可以正常工作。添加到下面的代码中。

我试图允许用户从清单中选择多个目录,并在单击“提交”按钮时返回它们。这是我的代码的 sn-p。它使用 /sdcard/ 上的所有目录填充 ListView,并且对于我提交时的初始选择(无论我选择多少),日志显示返回的正确选择。但是,如果我取消选中一个项目,然后再次单击“提交”,它仍然显示好像全部被选中。我是否需要编写一个处理程序来取消选中一个项目?我认为这是由choiceMode 选择处理的?谢谢!

private SparseBooleanArray a;    
directoryList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, directoryArray));
    submitButton = (Button)findViewById(R.id.submit_button);
    submitButton.setOnClickListener(new OnClickListener()
        {
        @Override
        public void onClick(View v)
        {
            a = new SparseBooleanArray();
            a.clear();
            a = directoryList.getCheckedItemPositions();

            for (int i = 0; i < a.size(); i++)
            {
                //added if statement to check for true. The SparseBooleanArray
                //seems to maintain the keys for the checked items, but it sets
                //the value to false. Adding a boolean check returns the correct result.                    
                if(a.valueAt(i) == true)
                    Log.v("Returned ", directoryArray[a.keyAt(i)]);

            }                
        }
    });

【问题讨论】:

    标签: android listview multiple-select


    【解决方案1】:

    进行了更多调试并找到了适合我的解决方案。编辑成上面的代码。出于某种原因,SparseBooleanArray 不会自己清空;它维护已选中的框的键。但是,当调用 getCheckedItemPositions() 时,它会将 VALUE 设置为 false。所以键仍然在返回的数组中,但它的值为 false。只有选中的复选框才会被标记为 true。

    【讨论】:

      【解决方案2】:

      我知道你找到了一个适合你的解决方案,但是可能大部分时间都可以使用的更清洁和更简单的解决方案是这样的(我想保留所选元素的所有 id):

      (在我的 ListActivity 中):

      SparseBooleanArray selectedPos = getListView()
              .getCheckedItemPositions();
      
      ListAdapter lAdapter = getListAdapter();
      List<Long> ids = new ArrayList<Long>();
      for (int i = 0; i < lAdapter.getCount(); i++) {
          if (selectedPos.get(i)) {
              ids.add(lAdapter.getItemId(i));
          }
      }
      

      【讨论】:

        【解决方案3】:

        并不是要这样做作为答案,但我必须扩展您为进行多选所做的工作。你为什么要为你的选择做一个字段变量?我刚刚做了本地 SparseBooleanArray...

        public class NaughtyAndNice extends ListActivity {
        TextView selection;
        String[] items={"lorem","ipsum", "dolor", "sit", "amet",
                "consectetuer", "adipisc", "jklfe", "morbi", "vel",
                "ligula", "vitae", "carcu", "aliequet"};
        
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,items));
                   selection = (TextView)findViewById(R.id.selection);
            this.getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        }
        
        public void onListItemClick(ListView parent, View view, int position, long id){
            SparseBooleanArray choices = parent.getCheckedItemPositions();
            StringBuilder choicesString = new StringBuilder();
            for (int i = 0; i < choices.size(); i++)
            {
            //added if statement to check for true. The SparseBooleanArray
            //seems to maintain the keys for the checked items, but it sets
            //the value to false. Adding a boolean check returns the correct result.                    
                if(choices.valueAt(i) == true)
                    choicesString.append(items[choices.keyAt(i)]).append(" ");
        
            } 
            selection.setText(choicesString);
        }
        }
        

        【讨论】:

        • 执行 for 循环然后在 for 循环中执行 keyAt 似乎非常低效... keyAt 运行有多糟糕?哦,好吧
        • 我不明白这段代码的区别......它似乎在功能上与我的相同。
        • 哦,没关系,我错过了你的问题。没有真正的理由为 SBA 做一个类变量,我只是 Java 和 Android 的新手,而且做得很愚蠢。 :)
        【解决方案4】:

        无需使用SparseBooleanArray choices = parent.getCheckedItemPositions();

        StringBuilder 就足够了。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-03-11
          • 1970-01-01
          • 1970-01-01
          • 2018-08-16
          • 1970-01-01
          相关资源
          最近更新 更多