第二次打开时预选择了Android listview multichoice

问题描述

我对Android的“多项选择”列表视图有疑问:

  1. 我打开包含相关列表视图的片段(通过片段事务)。

  2. 选择少量条目;

  3. 单击按钮以关闭片段;

  4. 再次打开片段;

  5. 即使在“ listView.getCheckedItemPositions();”中,我在第2步中选择的所有元素仍处于选中状态(我不希望如此)。返回空数组(即使我在电话屏幕上看到选中的标记,我也无法清除它,因为它已经为空)。

  6. 单击关闭片段的按钮,突然数组不为空。

     public class AreaChooserListFragment extends Fragment implements View.OnClickListener {
     ArrayList<SkelbiuLTMap> allValues;
     ArrayList<Integer> selectedValues;
     String[] textValues;
     private ListView listView;
     Button saveButton;
     int layer;
     @Override
     public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
    
         View view = inflater.inflate(R.layout.fragment_area_chooser_list,container,false);
         allValues = getArguments().getParcelableArrayList("list");
         layer = getArguments().getInt("layer");
         textValues = new String[allValues.size()];
         listView = view.findViewById(R.id.cities_list_view);
         saveButton = view.findViewById(R.id.button4);
         saveButton.setonClickListener(this);
    
         for (int i=0; i<allValues.size(); i++){
             textValues[i] = allValues.get(i).getName();
         }
         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getContext(),android.R.layout.simple_list_item_multiple_choice,textValues);
         listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
         listView.setAdapter(adapter);
         System.out.println(listView.getCheckedItemPositions());  // This is empty
         return view;
     }
    
    
     @Override
     public void onClick(View view) {
         if(view.getId() == R.id.button4){
             System.out.println(listView.getCheckedItemPositions()); // This isn't empty (contains all the pre-checked elements)
             getActivity().getSupportFragmentManager().popBackStack();
         }
     }}
    

这是一个非常标准的片段XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="400dp"
    android:background="@android:color/white"
    android:orientation="vertical"
    tools:context=".fragments.AreaChooserListFragment">

    <ListView
        android:id="@+id/cities_list_view"
        android:layout_width="match_parent"
        android:layout_height="250dp">

    </ListView>

    <Button
        android:id="@+id/button4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Save" />

</LinearLayout>

我尝试将所有内容放入OnViewCreated中,但这没什么区别。 我尝试用listView.clearChoices();清除它,但是也没有什么区别(因为它已经是空的了)。另外,我尝试通过循环取消选中每个元素-没什么区别。

任何帮助将不胜感激。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)