2015-09-04 81 views
0

我知道有多個問題與我的問題類似,但沒有答案尚未解決我的特定問題。在取消選擇並向下滾動後,listview中的複選框被選中。

我有一個BaseAdapter準備一個列表與複選框。

這裏是適配器的相關代碼 -

@Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 
     selectedFolderIds = new ArrayList<>(); 
     selectedFolderNames = new ArrayList<>(); 
     selectedFolderParentIds = new ArrayList<>(); 
     deselectedFolderIds = new ArrayList<>(); 

     final FolderStructureDataModel folderDetails = (FolderStructureDataModel) getItem(position); 

     final ViewHolder holder; 
     LayoutInflater inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     if(convertView == null) { 
      convertView = inflater.inflate(R.layout.folder_structure_list_item_template, 
        parent, false); 
      holder = new ViewHolder(); 
      holder.folderListLayout = (RelativeLayout) convertView.findViewById(R.id.folder_list_layout); 
      holder.childFoldersIndicator = (ImageView) 
        convertView.findViewById(R.id.child_folders_indicator); 
      holder.selectionCheckBox = (CheckBox) convertView.findViewById(R.id.checkbox); 

      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
      holder.selectionCheckBox.setOnCheckedChangeListener(null); 
      holder.selectionCheckBox.setOnClickListener(null); 
     } 

     /** 
     * Attach the listeners to the checkboxes. 
     */ 
     holder.selectionCheckBox.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       CheckBox cb = (CheckBox) view; 
       if (cb.isChecked()) { 
        selectedFolderIds.add(HelperFunctions.EncodeString(folderDetails.ID)); 
        selectedFolderNames.add(HelperFunctions.EncodeString(folderDetails.DisplayName)); 
        selectedFolderParentIds.add(HelperFunctions.EncodeString(folderDetails.ParentFolderID)); 

        SelectedFolderListener selectedFolderListener = 
          (SelectedFolderListener) context; 
        selectedFolderListener.getSelectedFoldersArrayList(selectedFolderIds, 
          selectedFolderNames, selectedFolderParentIds); 

        Log.i("Test : " + folderDetails.DisplayName + " at position " + 
          String.valueOf(position), "Set to " + 
          String.valueOf(checkboxStatus.get(position)) 
          + " From onClickListener"); 
       } else { 
        selectedFolderIds.remove(HelperFunctions.EncodeString(folderDetails.ID)); 
        selectedFolderNames.remove(HelperFunctions 
          .EncodeString(folderDetails.DisplayName)); 
        selectedFolderParentIds.remove(HelperFunctions 
          .EncodeString(folderDetails.ParentFolderID)); 
        deselectedFolderIds.add(HelperFunctions.EncodeString(folderDetails.ID)); 

        UnselectedFolderListener unselectedFolderListener = 
          (UnselectedFolderListener) context; 
        unselectedFolderListener.getDeselectedFolderArrayList(deselectedFolderIds); 

        Log.i("Test : " + folderDetails.DisplayName + " at position " + 
          String.valueOf(position), "Set to " + 
          String.valueOf(checkboxStatus.get(position)) 
          + " From onClickListener"); 
       } 
      } 
     }); 

     holder.selectionCheckBox.setOnCheckedChangeListener 
       (new CompoundButton.OnCheckedChangeListener() { 

        @Override 
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 

         if (((buttonView).isChecked()) && isChecked) { 
          checkboxStatus.set(position, true); 
          Log.i("Test : " + folderDetails.DisplayName + " at position " + 
            String.valueOf(position), "Set to " + 
            String.valueOf(checkboxStatus.get(position)) 
            + " From onCheckChangedListener"); 
         } else { 
          checkboxStatus.set(position, false); 
          Log.i("Test : " + folderDetails.DisplayName + " at position " + 
            String.valueOf(position), "Set to " + 
            String.valueOf(checkboxStatus.get(position)) 
            + " From onCheckChangedListener"); 
         } 
        } 
       }); 

     //Populate the data 
     holder.selectionCheckBox.setText(String.valueOf(folderDetails.DisplayName)); 

     if(Integer.valueOf(folderDetails.ChildFolderCount) == 0) { 
      holder.childFoldersIndicator.setVisibility(View.GONE); 
      holder.folderListLayout.setFocusable(true); 
      holder.folderListLayout.setClickable(true); 
     } else { 
      holder.childFoldersIndicator.setVisibility(View.VISIBLE); 
      holder.folderListLayout.setFocusable(false); 
      holder.folderListLayout.setClickable(false); 
     } 

     /** 
     * Set the checkbox as per the service reply (status in the server) 
     */ 
     if(Boolean.valueOf(folderDetails.IsAlreadySelected)) { 
      holder.selectionCheckBox.setChecked(true); 
      Log.i("Test : " + folderDetails.DisplayName + " at position " + 
        String.valueOf(position), "Set to " + 
        String.valueOf(holder.selectionCheckBox.isChecked()) + " From folderDetails"); 
     } else { 
      holder.selectionCheckBox.setChecked(false); 
      Log.i("Test : " + folderDetails.DisplayName + " at position " + 
        String.valueOf(position), "Set to " + 
        String.valueOf(holder.selectionCheckBox.isChecked()) + " From folderDetails"); 
     } 

     /** 
     * Set the checkbox as per the values maintained in the checkboxStatus ArrayList 
     */ 
     if(checkboxStatus.get(position)) { 
      holder.selectionCheckBox.setChecked(true); 
      Log.i("Test : " + folderDetails.DisplayName + " at position " + 
        String.valueOf(position), "Set to " + 
        String.valueOf(holder.selectionCheckBox.isChecked()) + " From checkBoxStatus"); 
     } else { 
      holder.selectionCheckBox.setChecked(false); 
      Log.i("Test : " + folderDetails.DisplayName + " at position " + 
        String.valueOf(position), "Set to " + 
        String.valueOf(holder.selectionCheckBox.isChecked()) + " From checkBoxStatus"); 
     } 

     return convertView; 
    } 

我有一個ArrayListcheckboxStatus它保持在每個位置的複選框的狀態。

我面臨的問題是,如果複選框已被選中(根據'folderDetails.IsAlreadySelected'的值),我取消選中它,然後向下滾動,然後向上滾動,複選框會再次被選中。特殊的部分是,這不會發生在相反的情況 - 如果複選框未選中,我檢查它並向下滾動,然後再次向上滾動;狀態保持爲已檢查狀態。

適配器可能有什麼問題?

回答

2

爲了解決你的問題做到這一點:

if(convertView == null) { 
     convertView = inflater.inflate(R.layout.folder_structure_list_item_template, 
       parent, false); 
     holder = new ViewHolder(); 
     holder.folderListLayout = (RelativeLayout) convertView.findViewById(R.id.folder_list_layout); 
     holder.childFoldersIndicator = (ImageView) 
       convertView.findViewById(R.id.child_folders_indicator); 
     holder.selectionCheckBox = (CheckBox) convertView.findViewById(R.id.checkbox); 
    // Add this line, set tag on your checkbox. 
holder.selectionCheckBox .setTag(holder); 
     convertView.setTag(holder); 
    } 

現在在clicklistner,

holder.selectionCheckBox.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
     // add this line,declare holder_new as an instance of ViewHolder 
     holder_new= (ViewHolder) view.getTag(); 
      // rest is your code what you have already written 
    }); 

現在的onclick後,無論您使用的是持有人訪問selectionCheckBox,將其更改爲holder_new。

holder.selectionCheckBox.setChecked(true); 更改爲holder_new.selectionCheckBox.setChecked(true);

+0

'holder_new ==(ViewHolder)view.getTag();' 我假設你的意思'holder_new =(ViewHolder)view.getTag();',對吧? 另外,我在哪裏聲明'holder_view'? –

+0

yup ..打字錯誤:P 宣佈它在宣佈「持有人」的地方。它最多可以在課堂上聲明。 –

+0

我必須使它成爲全局的,否則我會得到'從內部類訪問的變量需要被聲明爲最終的'錯誤。 我想你實現上述變化和活動,因爲在'holder_new.selectionCheckBox.setChecked(真)NullPointerException異常的崩潰;' –

相關問題