Listview将以前的项目显示为已检查的android

时间:2015-02-02 09:33:33

标签: java android listview android-listview

我有一个带有listview和listview标题的alertdialog,带有"检查全部"和"取消选中所有"按钮和列表视图页脚与"完成"按钮。 Listview是CHOICE_MODE_MULTIPLE,其arrayList值为EavesListEavesList将与另一个arrayList selectedItems进行交叉检查,以便将这些项目显示为已选中。我面临的问题是Listview显示正确的值,但显示以前的项目已选中。

这是我的代码:

AlertDialog.Builder builder = new AlertDialog.Builder(
                        getActivity());
                builder.setTitle("Select Category List");

                final ListView modeList = new ListView(getActivity());
                LayoutInflater inflater = LayoutInflater.from(getActivity());

                View rootView = inflater.inflate(
                        R.layout.listview_header_eavesdrop, null);
                modeList.addHeaderView(rootView);

                Button btnDone = new Button(getActivity());
                btnDone.setText("Done");
                btnDone.setTextColor(getActivity().getResources().getColor(
                        android.R.color.white));
                final ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(
                        getActivity(),
                        android.R.layout.simple_list_item_multiple_choice,
                        android.R.id.text1, EavesList);
                modeList.addFooterView(btnDone);
                modeList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
                modeList.setAdapter(modeAdapter);   
                    if (selectedItems.size() != 0) {
                    for (int i = 0; i < EavesList.size(); i++) {

                        if (selectedItems.size() != 0) {
                            // modeList.setItemChecked(2, true);

                            for (int j = 0; j < selectedItems.size(); j++) {
                                if (EavesList.get(i).contains(
                                        selectedItems.get(j))) {
                                    modeList.setItemChecked(i, true);
                                    Log.e("Selected Item ",
                                            "there Values true "
                                                    + selectedItems.get(j));
                                    j = selectedItems.size() + 2;
                                } 
                            }
                        } else {
                            Log.e("Selected Item ", "No Values");
                        }
                    }
                } else {
                    for (int i = 0; i < EavesList.size(); i++) {

                        if (category.size() != 0) {
                                for (int j = 0; j < category.size(); j++) {
                                if (EavesList_id.get(i).contains(category.get(j))) {
                                    modeList.setItemChecked(i, true);
                                    Log.e("List " + EavesList_id.get(i), ""+ category.get(j));                                  
                                    Log.e("Selected category Item ",
                                            "there Values true "
                                                    + category.get(j));
                                } else {
                                    if (j == category.size()) {
                                        modeList.setItemChecked(i, false);
                                        Log.e("Selected category Item ",
                                                "there Values  false "
                                                        + category.get(j));
                                    }
                                }
                            }
                        } else {
                            Log.e("Selected Item ", "No Values");
                        }
                    }
                }
                builder.setView(modeList);

                final Dialog dialog = builder.create();

                btnDone.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        selectedItems.clear();
                        eavesdropping.setText("");
                        SparseBooleanArray checked = modeList
                                .getCheckedItemPositions();
                        selected_community_id = new String[checked.size()];
                        String[] evas_id;
                        for (int i = 0; i < checked.size(); i++) {
                            // Item position in adapter
                            int position = checked.keyAt(i);
                            // Add sport if it is checked i.e.) == TRUE!
                            if (checked.valueAt(i))
                                selectedItems.add(EavesList.get(position));
                                selected_community_id[i] = EavesList_id
                                    .get(position);
                            }     

                        for (int i = 0; i < selected_community_id.length; i++) {
                            eavesdropping_hide.append(selected_community_id[i]);
                            if (i != selected_community_id.length ) {
                                eavesdropping_hide.append(",");
                            }
                        }

                        String[] outputStrArr = new String[selectedItems.size()];

                        for (int i = 0; i < selectedItems.size(); i++) {
                            outputStrArr[i] = selectedItems.get(i);

                            eavesdropping.append(outputStrArr[i]);
                            eavesdropping.append(" | ");
                        }
                        dialog.dismiss();
                    }
                });
                dialog.show();

P.S:列表视图显示正确的内容,但列表视图的选中项目始终是一个前一个项目始终被选中,并且未选中要检查的项目。

2 个答案:

答案 0 :(得分:0)

为什么不在提交按钮上单击

尝试此方法
SparseBooleanArray sparseBooleanArray = list.getCheckedItemPositions();

List<String> lstCheck= new ArrayList<String>();

for (int i = 0; i < list.getCount(); i++) {
if (sparseBooleanArray.get(i) == true) {
      lstCheck.add(list.getItemAtPosition(i).toString());
   }
 }

现在lstCheck只会导致选中的值与您的EavesList进行比较,并找到那些不存在的项目,并且您还会获得未选择的值

答案 1 :(得分:0)

使用以下内容:(它是伪代码):

ArrayList<String> data;
ArrayList<String> checkboxes;

.....

    listview.setOnItemClickListener(new OnItemClickListener()
{
    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
    { 
       if(chockBoxes.containds(data.get(position))){
       chockBoxes.remove(data.get(position));
       }else{
       chockBoxes.add(data.get(position))
       }

    }
});

... 现在在你的适配器getView():

if(checkBoxes.contains(data.get(position))){
checkBox.setChecked(true);
}else{
checkBox.setChecked(false);
}