繁体   English   中英

回收器适配器和共享首选项

[英]Recycler Adapter and Shared preferences

我这里有一个简单的用例,使用 shardpreferences 保存的单词列表,使用回收器视图显示。 我需要添加单词和删除单词

添加单词:

public void get_word(String new_word) {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(String.valueOf(sharedPreferences.getAll().size()), new_word);
        System.out.println(sharedPreferences.getAll());
        editor.commit();
        vocabL.add(new_word);
        adapter.notifyItemInserted(vocabL.size() - 1);
        VVlist.scrollToPosition(vocabL.size() -1);
    }

这里的想法非常简单而且有问题,我用于共享首选项的键是数字,所以这里我使用了大小 function,它将返回我可以使用的下一个数字,但这是问题所在:

 public void onBindViewHolder(@NonNull RecyclerAdapter.ViewHolder holder, int position) {
        holder.textView.setText(list.get(position));
        holder.textView.setOnClickListener(v -> Toast.makeText(context, "Clicked on " + holder.getAbsoluteAdapterPosition(), Toast.LENGTH_SHORT).show());
        holder.textView.setOnLongClickListener(view -> {
            AlertDialog.Builder builder = new AlertDialog.Builder(context)
                    .setTitle("Delete word")
                    .setMessage("are you sure you want to delete?")
                    .setPositiveButton("yes", (dialogInterface, i) -> {
                        list.remove(holder.getAbsoluteAdapterPosition());                        editor.remove(String.valueOf(holder.getAbsoluteAdapterPosition()));
                        notifyItemRemoved(holder.getAbsoluteAdapterPosition());
                        editor.commit();
                    })
                    .setNegativeButton("no", (dialogInterface, i) -> {

                    });
            builder.show();
            return true;
        });
    }

在这里,当我删除一个词时,我也松了钥匙,所以我想问题很明显,下次我尝试添加一个词时,我覆盖了一个现有值,这个低效的代码还有其他问题,删除时, position 可能与密钥不同,因此删除了一些不应该删除的内容

上面的代码中有一个列表'list',它只是一个包含所有单词的列表,我用它来测试可能的解决方案

我是自学的,我不知道在这里使用正确的编程实践。 我能想到的唯一可能的解决方案是使用 map 接口,但我觉得有更好的方法来解决这个问题 go 谢谢

我就是这样做的。

1.有一个model的话

data class Word(var id:Long,var word:String)

2.为回收者视图填充单词列表。 对于 ID,请使用 System.nanotime() & 0xfffff。 这保证了每次调用时都有一个唯一的 ID(至少我对它进行了一百万次迭代测试)。

val wordlist= mutableListOf<Word>()

for (s in 0 until 10){
val word=Word((System. nanotime & 0xfffff).toLong(),"Some random string")

worldList.add(word)
//save the word to preference with the key word.id

  1. 在 onBindViewHolder 中的适配器中
val word= worldList[position]

yourTextView.text=word

yourDeleteView.setOnClickListener{
val wordID=word.id
deleteWord(wordID)
}
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.textView.setText(pist.get(position).toString());
        holder.textView.setTag(pist.get(position).toString());
        holder.textView.setOnClickListener(v -> Toast.makeText(context, "Clicked on " + holder.getAbsoluteAdapterPosition(), Toast.LENGTH_SHORT).show());
        holder.textView.setOnLongClickListener(view -> {
            AlertDialog.Builder builder = new AlertDialog.Builder(context)
                    .setTitle("Delete word")
                    .setMessage("are you sure you want to delete?")
                    .setPositiveButton("yes", (dialogInterface, i) -> {
                        pist.remove(holder.getAbsoluteAdapterPosition());
                        editor.remove(String.valueOf(view.getTag()));
                        notifyItemRemoved(holder.getAbsoluteAdapterPosition());
                        editor.commit();
                    })
                    .setNegativeButton("no", (dialogInterface, i) -> {

                    });
            builder.show();
            return true;
        });
    }

我找到的解决方案是使用标签进行识别,我制作了共享首选项中所有值的列表(pist)并使用单词作为键,因为它更方便,如果需要我们总是可以制作另一个键列表并相应地设置标签我不确定这是否是通往 go 的专业方式,但它工作正常并且看起来足够体面不过我仍然愿意回答

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM