1

我收到了一个奇怪的屁股错误,我似乎无法弄清楚为什么。我有一个“保存”按钮和一个带有两个选项 {select1 和 select2} 的下拉微调器。当用户按下按钮时,列表视图应该保存复选框的状态。如果用户更改为“select1”或“select2”,则应恢复保存的检查。

问题是,例如在“select1”下,当我取消选择一个复选框(所以 3 个中的 2 个)并保存(使用按钮)时,这两个函数给了我两个不同的结果。

ListView lv;
**Log.w(TAG,"saveResourceSettings. count: "+lv.getCheckedItemCount());**  <--- "count: 2"
SparseBooleanArray checked =lv.getCheckedItemPositions();

**Log.w(TAG,"saveResourceSettings. checked count: "+checked.size());** <----"checked count: 3" **?????????**

有谁知道出了什么问题?当我取消选中一个复选框时,就好像 SparseBooleanArray 没有被更新。例如,如果我取消选择“com.android.gesture.builder”,它仍会显示在数组中。

在此处输入图像描述

顺便说一句,我正在使用自定义数据结构来保存复选框的状态,这工作正常。

只是为了确保您知道上下文和方法,请发布部分代码:

packageListView=(ListView) findViewById(R.id.ListView01);

packageListView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,appNames ));

packageListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);




spinner = (Spinner) findViewById(R.id.my_spinner);

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.s_p_array, android.R.layout.simple_spinner_item);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

spinner.setAdapter(adapter);

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                       String selected = parent.getItemAtPosition(position).toString();

                       ListView temp=null;

                       if(selected.equalsIgnoreCase("select1")){

                             temp= Utils.restoreChecks(packageListView,profile , resource,thisActivity, State.SELECT1);

                        }

                        else if(selected.equalsIgnoreCase("select2")){

                             temp= Utils.restoreChecks(packageListView,profile , resource,thisActivity, State.SELECT2);

                         }



                        packageListView=temp;

                        Log.w(TAG,"REFRESHING");

                      //packageListView.invalidate();

                      ArrayAdapter adapt= (ArrayAdapter) packageListView.getAdapter();

                     adapt.notifyDataSetChanged();


                   }

           });





Button save = (Button) findViewById(R.id.save);

save.setOnClickListener(new OnClickListener() {


             public void onClick(View v) {



                        String spinnerSelected =spinner.getSelectedItem().toString();

                        if(spinnerSelected.equalsIgnoreCase("select1")){

                                Log.w(TAG,"onCreate. count: "+packageListView.getCheckedItemCount());


                             AccessFileCmds.saveResourceSettings( packageListView, installedPackageNames, profile, resource,thisActivity, State.SELECT1);

                        }

                        else if(spinnerSelected.equalsIgnoreCase("select2")){

                              Log.w(TAG,"onCreate. count: "+packageListView.getCheckedItemCount());

                             AccessFileCmds.saveResourceSettings( packageListView, installedPackageNames, profile, resource,thisActivity, State.SELECT2);

                        }

         }

});

解决方案:

public static int[] getItemPositions(ListView lv){
    List<Integer> indexes=new ArrayList<Integer>();

    SparseBooleanArray checkedItems = lv.getCheckedItemPositions();
    if (checkedItems != null) {
        for (int i=0; i<checkedItems.size(); i++) {
            if (checkedItems.valueAt(i)) {
                String item = lv.getAdapter().getItem(checkedItems.keyAt(i)).toString();
                Log.i(TAG,item + " was selected. index: "+checkedItems.keyAt(i));
                indexes.add(checkedItems.keyAt(i));
            }
        }
    }
    int[] ret=new int[indexes.size()];

    for(int i=0;i<indexes.size();i++){
        ret[i]=indexes.get(i);
    }
    return ret;
}
4

0 回答 0