如何取消选中数组适配器中的复选框

时间:2017-06-16 08:03:18

标签: android listview checkbox android-arrayadapter

我正在尝试取消选中onItemClickListener上listview数组适配器中的复选框。 它一直保持检查。如果我再次点击它取消选中它不会被取消选中。 谁能在这里请建议我怎么做?

 pagesresult = new JSONArray(getIntent().getStringExtra("pageResult"));
                for (int i = 0; i < pagesresult.length(); i++) {
                    JSONObject json = pagesresult.getJSONObject(i);
                    Iterator<String> keys = json.keys();

                    while (keys.hasNext()) {
                        key = keys.next();
                        //pageValue = );
                        pageKeyArray.add(key);
                        pageArray.add(String.valueOf(json.get(key)));
                        System.out.println("Key :" + key + "  Value :" + json.get(key));
                        Log.d(TAG, "pageKeyArray Check :" + pageKeyArray);
                        Log.d(TAG, "currentKey Check :" + pageArray);
                    }

                }

                alreadyBookedByUser = new JSONArray(getIntent().getStringExtra("alreadyBookedByUser"));
                Log.d(TAG, "compositionCount Check :" + pagesresult);
                Log.d(TAG, "compositionCount Check :" + alreadyBookedByUser);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            done = (Button) findViewById(R.id.done);
            findViewsById();
            adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,pageArray);
            listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
            listView.setAdapter(adapter);

            listView.setOnItemClickListener(this);
     @Override
        public void onClick(View v) {
            if (v == done) {

                SparseBooleanArray checked = listView.getCheckedItemPositions();

                ArrayList<String> selectedItems = new ArrayList<String>();

                for (int i = 0; i < checked.size(); i++) {
                    // Item position in adapter

                    position = checked.keyAt(i);
                    listView.setItemChecked(position, true);
                    Log.d(TAG, "Position check :" + position);
                    // Add sport if it is checked i.e.) == TRUE!

                    if (checked.valueAt(i)) {
                        selectedItems.add(adapter.getItem(position));

                    }
                }
                Log.d(TAG, "Page key array show :" + this.pageKeyArray);
                Log.d(TAG, "selectedItems show :" + selectedItems);

                int max = -1;
                for(String value: selectedItems) {
                    int selectedIndex= this.pageArray.indexOf(value);
                    this.selectedPageIems.add(this.pageKeyArray.get(selectedIndex));
                    if(selectedIndex > max) {
                        max = selectedIndex;
                    }
                }
                String firstPageNumber = getFirstPageNumber(this.pageKeyArray.get(0));
                String currentPageNumber = firstPageNumber;
                String lastPageNumber = getLastPageNumber(this.pageKeyArray.get(max));
                Log.d(TAG, "First Page Number :" + firstPageNumber);
                Log.d(TAG, "Last Page Number :" + lastPageNumber);
                Log.d(TAG, "selectedPageIndexes show :" + selectedPageIndexes);
                Log.d(TAG, "selectedPageIems show :" + selectedPageIems);

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

                for (int i = 0; i < selectedItems.size(); i++) {
                    outputStrArr[i] = selectedItems.get(i);
                }
                try {
                    selectPages();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                Intent intent = new Intent(getApplicationContext(),MyBookings.class);
                session.setPageNumbers(firstPageNumber,currentPageNumber,lastPageNumber);
                startActivity(intent);
            }
        }

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Log.d(TAG, "First Page Number :" + position);
            for (int i=0; i<=position; i++){
                listView.setItemChecked(i,true);
            }
        }

1 个答案:

答案 0 :(得分:0)

每次按项目时都会选中复选框吗? @Override public void onItemClick(AdapterView parent, View view, int position, long id) { Log.d(TAG, "First Page Number :" + position); for (int i=0; i<=position; i++){ listView.setItemChecked(i,true); } }

我没有在任何地方看到setItemChecked(i,false)