1

我在我的Recyclerview中有複選框,現在我可以單擊它並顯示快餐欄。但任何人都可以幫助我保存從recyclerview複選框的狀態。從Recyclerview保存複選框狀態

我在回收站視圖適配器的onBindViewHolder中創建了一個onclicklistener。

private SparseBooleanArray mCheckedItems = new SparseBooleanArray(); 
     @Override 
     public void onBindViewHolder(final RecyclerViewHolder viewHolder, int i) { 
      // get the single element from the main array 
      DisplayImageOptions options = new DisplayImageOptions.Builder() 
        .showImageOnLoading(R.drawable.mirlogo) 
        .showImageForEmptyUri(R.drawable.gallery_no_image) 
        .showImageOnFail(R.drawable.gallery_image_fail) 
        .cacheInMemory(true) 
        .cacheOnDisk(true) 
        .considerExifParams(true) 
        .bitmapConfig(Bitmap.Config.RGB_565) 
        .build(); 

      final String images = HomeDataImages.IMAGES[i]; 
      // Set the values 

      ImageLoader.getInstance() 
        .displayImage(images, viewHolder.mImage, options); 

      viewHolder.mCheck.setChecked(mCheckedItems.get(i)); 
     Log.d("Position=", String.valueOf((mCheckedItems.get(i)))); 

     viewHolder.mCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 

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

       int position = viewHolder.getAdapterPosition(); 
       mCheckedItems.put(position, isChecked); 
       Log.d("Checked Array = ", String.valueOf(mCheckedItems)); 
//    viewHolder.mCheck.setChecked(mCheckedItems); 
//error comes here setChecked(boolean) cannot be applied to SparseBooleanArray 
       if (isChecked){ 
       Snackbar snackbar = Snackbar.make(viewHolder.itemView, "Item Favorited", Snackbar.LENGTH_SHORT); 
       snackbar.show();} 
       else{ 
        Snackbar snackbar = Snackbar.make(viewHolder.itemView, "Item Unfavorited", Snackbar.LENGTH_SHORT); 
        snackbar.show();} 
       } 
      }); 

    } 

在此先感謝。

回答

1

假設viewHolder.mCheckCheckedTextView我會使用一些這樣的:

private SparseBooleanArray mCheckedItems = new SparseBooleanArray(); 

@Override 
public void onBindViewHolder(final RecyclerViewHolder viewHolder, int i) { 
    // get the single element from the main array 
    DisplayImageOptions options = new DisplayImageOptions.Builder() 
      .showImageOnLoading(R.drawable.mirlogo) 
      .showImageForEmptyUri(R.drawable.gallery_no_image) 
      .showImageOnFail(R.drawable.gallery_image_fail) 
      .cacheInMemory(true) 
      .cacheOnDisk(true) 
      .considerExifParams(true) 
      .bitmapConfig(Bitmap.Config.RGB_565) 
      .build(); 

    final String images = HomeDataImages.IMAGES[i]; 
    // Set the values 

    ImageLoader.getInstance() 
      .displayImage(images, viewHolder.mImage, options); 

    viewHolder.mCheck.setChecked(mCheckedItems.get(i)); 

    viewHolder.mCheck.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      int position = viewHolder.getAdapterPosition(); 
      final boolean newValue = !mCheck.isChecked(); 

      mCheckedItems.put(position, newValue); 
      checkedTextView.setChecked(newValue); 

      //display the text accordingly with the newValue value 
      Snackbar snackbar = Snackbar.make(v, "Item Favorited", Snackbar.LENGTH_SHORT); 
      snackbar.show(); 
     } 
    }); 

    public List<Integer> getSelectedItemPositions() { 
     List<Integer> selected = new ArrayList<>(); 
     for (int i = 0; i < mCheckedItems.size(); i++) { 
      final boolean checked = mCheckedItems.valueAt(i); 
      if (checked) { 
       selected.add(mCheckedItems.keyAt(i)); 
      } 
     } 
     return selected; 
    } 

    public void restoreSelectedItems(List<Integer> positions){ 
     for (Integer position : positions) { 
      mCheckedItems.put(position, true); 
     } 
    } 
} 

基本上我以存儲檢查哪些元素使用SparseBooleanArray

+0

現在無法記錄所有選中和未選中的值。但是,如何保存該數組,並恢復檢查項目時,應用程序重新打開..?根據你的理念,你可以幫助我 –

+0

編輯的代碼。 –

+1

我添加了兩種方法:一種用於獲取所選項目,另一種用於初始化所選項目數組。一旦你選擇了items數組,你可以將它保存在共享首選項中並在Activity啓動時讀取它。 – rciovati