如何在SharedPreferences RecyclerView中保存特定的复选框项目并查看它们

时间:2019-06-18 06:39:40

标签: android sharedpreferences

我在RecyclerView中有一些数据,每一项都有一个复选框按钮。我使用SharedPreference保存特定的检查数据并检索它们。

例如:如果我仅检查数据“ A”,它将仅保存数据“ A”。我编写了一些代码来执行此操作,但是每当我检查特定数据时,它将检查所有数据,而每当我取消检查特定数据时,就会取消检查所有数据。

我已经尝试了很多天来解决这个问题,但直到现在仍然停滞不前。

MainActivity.java

public class HomeActivity extends AppCompatActivity {

private RecyclerView recyclerView;
private List<Story> storyList  = new ArrayList<>();
AppPreferences appPreferences;
private Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    recyclerView = findViewById(R.id.recycler_view);

    SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("MyPref", 0);
    PreferenceManager preferenceManager = new PreferenceManager(sharedPreferences);
    appPreferences = new AppPreferences(preferenceManager);

    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
    recyclerView.setAdapter(new AdapterHome(storyList, this, appPreferences));
    setUpData();
}

private void setUpData() {

    for (int i = 0; i < 10; i++) {
        Story story = new Story();
        story.setIdStory(String.valueOf(i));
        story.setIsLiked(0);
        story.setTitle("Sample " + i);
        storyList.add(story);
    }

} }

AdapterHome.java

public class AdapterHome extends RecyclerView.Adapter<AdapterHome.ViewHolder> {

public static final String PREFS_NAME = "PRODUCT_APP";
public static final String FAVORITES = "Product_Favorite";

private List<Story> storyList;
private List<Story> favorites;
private Context context;

private int changedItemPosition;
private boolean isLiked;
private AppPreferences appPreferences;
private SharedPreferences preferences;
private CheckBox checkBox;
Boolean checked = false;
Boolean[] checkedStatus;

public AdapterHome(List<Story> storyList, Context context, AppPreferences appPreferences) {
    this.storyList = storyList;
    this.context = context;
    this.appPreferences = appPreferences;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_story_home, parent, false));
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    holder.setViewData(storyList.get(position), holder.getAdapterPosition());
}

@Override
public int getItemCount() {
    return storyList.size();
}


//The problem lays here
public void putHeart(boolean isChecked, String key) {
    SharedPreferences sharedPreferences = context.getSharedPreferences("MyPref", Activity.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean("MyPref", isChecked);
    editor.commit();
}

public boolean getHeart(String key) {
    SharedPreferences sharedPreferences = context.getSharedPreferences("MyPref", Activity.MODE_PRIVATE);
    Boolean isChecked = sharedPreferences.getBoolean("MyPref", false);
    return isChecked;
}



//ViewHolder
public class ViewHolder extends RecyclerView.ViewHolder {

    private TextView textView;
    private CheckBox likeCheckBox;

    public ViewHolder(View itemView) {
        super(itemView);
        textView = itemView.findViewById(R.id.text_view);
        likeCheckBox = itemView.findViewById(R.id.like_button_cb);
    }

    public void setViewData(final Story story, final int adapterPosition) {

        textView.setText(story.getTitle());

        if (story.getIsLiked() == 1) {
            likeCheckBox.setChecked(true);
        }
        else {
            likeCheckBox.setChecked(false);
        }

        // The problem lays here
        boolean isChecked = getHeart("MyPref");
        likeCheckBox.setChecked(isChecked);

        likeCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                changedItemPosition = adapterPosition;

                if (buttonView.isPressed()) {
                    if (isChecked) {
                        isLiked = true;
                        updateLikes();
                        appPreferences.saveFavouriteCard(story);
                        putHeart(isChecked,"isChecked"); // The problem lays here
                        Toast.makeText(context, "Saved", Toast.LENGTH_SHORT).show();
                    }
                    else {
                        isLiked = false;
                        updateLikes();
                        appPreferences.deleteCard(story.getIdStory());
                        putHeart(isChecked,"isChecked"); // The problem lays here
                        Toast.makeText(context, "Removed", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });

    } //setviewdata

    public void updateLikes() {
        if (isLiked && storyList.get(changedItemPosition).getIsLiked() == 0) { //jika dilakukan like (pada posisi hati kosong) di halaman home
            storyList.get(changedItemPosition).setIsLiked(1); //maka jadikan hati berwarna merah di halaman favourite list
            notifyItemChanged(changedItemPosition, ACTION_LIKE_IMAGE_CLICKED);
        }
        else if (!isLiked && storyList.get(changedItemPosition).getIsLiked() == 1) { //jika like dicabut (pada posisi hati yang sedang merah) di halaman home
            storyList.get(changedItemPosition).setIsLiked(0); //maka cabut juga warna merah di halaman favourite list
            notifyItemChanged(changedItemPosition, ACTION_LIKE_IMAGE_CLICKED);
        }

    } //updateLikes

} //viewholder } //AdapterHome

问题出在AdapterHome.java中。您可以通过获取项目列表的ID进行更改,并将其保存到收藏夹SharedPreference中。

我试图更改一些代码,但是它不起作用:

//问题出在这里

 public void putHeart(View v, boolean isChecked, String key, int pos) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("MyPref", Activity.MODE_PRIVATE);

        likeChecbox = v.findViewById(R.id.like_button_cb);
        likeCheckBox.setOnCheckedChangeListener(null);
        likeCheckBox.setChecked(storyList.get(pos).isSelected());
        storyList.get(pos).setChecked(isChecked);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(storyList.get(pos), isChecked);
        editor.commit();

        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean("MyPref", isChecked);
        editor.commit();
    }

   public boolean getHeart(String key) {
SharedPreferences sharedPreferences = 
       context.getSharedPreferences("MyPref", Activity.MODE_PRIVATE);

        for(int i=0; i<storyList.size(); i++)
        {
            storyList.get(i).getIsLiked(sharedPreferences.getBoolean(storyList.get(i),false));
        }

    }

Story.java

public class Story {
public Story(){}

private String idStory;
private String title;
private int isLiked;
private int ambilID;

public String getIdStory() {
    return idStory;
}
public void setIdStory(String isStory) {
    this.idStory = isStory;
}

public int ambilIdStory() {
    return ambilID;
}
public void putIdStory(int isStory) {
    this.ambilID = isStory;
}

public String getTitle() {
    return title;
}
public void setTitle(String title) {
    this.title = title;
}

public int getIsLiked() {
    return isLiked;
}
public void setIsLiked(int isLiked) {
    this.isLiked = isLiked;
}

@Override
public String toString() {
    return GsonUtils.convertToJSON(this);
}
}

更新的代码[工作]

最后,问题消失了。可以通过每个项目的唯一标识符来解决。这样,即使故事顺序发生更改,故事的已检查状态也可以保存。

public void putHeart(boolean isChecked, int position) {
    SharedPreferences sharedPreferences = context.getSharedPreferences("MyPref", Activity.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    Story story = storyList.get(position);
    editor.putBoolean(story.getIdStory(), isChecked);
    editor.commit();
}

public boolean getHeart(int position) {
    SharedPreferences sharedPreferences = context.getSharedPreferences("MyPref", Activity.MODE_PRIVATE);
    Story story = storyList.get(position);
    boolean isChecked = sharedPreferences.getBoolean(story.getIdStory(), false);
    return isChecked;
}

2 个答案:

答案 0 :(得分:1)

由于总是生成数据,因此您可以将列表中的位置用作“首选项”中布尔值的键。

SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(String.valueOf(position), isChecked);
editor.commit();

然后:

SharedPreferences.Editor editor = sharedPreferences.edit();
boolean b = editor.getBoolean(String.valueOf(position), false);

已更新

boolean isChecked = getHeart(getAdapterPosition());
likeCheckBox.setChecked(isChecked);
likeCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            changedItemPosition = getAdapterPosition();

            if (buttonView.isPressed()) {
                if (isChecked) {
                    isLiked = true;
                    updateLikes();
                    appPreferences.saveFavouriteCard(story);
                    putHeart(isChecked, getAdapterPosition()); // The problem lays here
                    Toast.makeText(context, "Saved", Toast.LENGTH_SHORT).show();
                }
                else {
                    isLiked = false;
                    updateLikes();
                    appPreferences.deleteCard(story.getIdStory());
                    putHeart(isChecked,getAdapterPosition()); // The problem lays here
                    Toast.makeText(context, "Removed", Toast.LENGTH_SHORT).show();
                }
            }
        }
    });

保存和恢复状态

public void putHeart(boolean isChecked, int position) {
    SharedPreferences sharedPreferences = context.getSharedPreferences("MyPref", Activity.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean(String.valueOf(position), isChecked);
    editor.commit();
}

public boolean getHeart(int position) {
    SharedPreferences sharedPreferences = context.getSharedPreferences("MyPref", Activity.MODE_PRIVATE);
    Boolean isChecked = sharedPreferences.getBoolean(String.valueOf(position), false);
    return isChecked;
}

答案 1 :(得分:1)

您应该为Story生成一个唯一的标识符。例如ID,名称等。

// put isChecked to shared prefs
Story story = storyList.get(pos);
editor.putBoolean(Integer.toString(story.getId()), isChecked);
// get isChecked from shared prefs
Story story = storyList.get(pos);
boolean isChecked = boolean b = editor.getBoolean(Integer.toString(story.getId()), false);

// generate id
// when you create a Story

int lastIdGenerated = editor.getInt("LAST_ID_GENERATED", 1);
int newId = lastIdGenerated + 1;
editor.putInt("LAST_ID_GENERATED", newId);

Story newStory = new Story();
newStory.setId(newId);

这样,即使故事顺序发生更改,故事的选中状态也会保存。