Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将图像url存储在SharedReferences中,并在Recyclerview中显示它们_Java_Android_Sharedpreferences_Favorites - Fatal编程技术网

Java 将图像url存储在SharedReferences中,并在Recyclerview中显示它们

Java 将图像url存储在SharedReferences中,并在Recyclerview中显示它们,java,android,sharedpreferences,favorites,Java,Android,Sharedpreferences,Favorites,我正在尝试将(添加到收藏夹)功能添加到我的应用程序中,因为我使用了SharedReferences下面的代码,但当我显示图像时,我只看到我添加的最后一张图像,我相信,而不是将新图像添加到由旧图像重新绘制的收藏夹部分: 下面是我在arraylist中添加和删除url的代码 public void add(String name, String url) { ArrayList<Post> myfav = new ArrayList<>();

我正在尝试将(添加到收藏夹)功能添加到我的应用程序中,因为我使用了
SharedReferences
下面的代码,但当我显示图像时,我只看到我添加的最后一张图像,我相信,而不是将新图像添加到由旧图像重新绘制的收藏夹部分:

下面是我在arraylist中添加和删除url的代码

  public void add(String name, String url) {
        ArrayList<Post> myfav = new ArrayList<>();
        Post post = new Post();
        post.setImgUrl(url);
        post.setName(name);
        myfav.add(post);
        TinyDB tinydb = new TinyDB(getApplicationContext());
        tinydb.putListObject("fav", myfav);
        Toast.makeText(getApplicationContext(), "added to fav", Toast.LENGTH_SHORT).show();
    }

    public void remove(String url) {
    TinyDB tinydb = new TinyDB(getApplicationContext());
    ArrayList<Post> myfavSaved;
    myfavSaved = tinydb.getListObject("fav", Post.class);
    for (int i = 0; i < myfavSaved.size(); i++) {
        if (myfavSaved.get(i).getImgUrl().equalsIgnoreCase(url)) {
            myfavSaved.remove(i);
            saveFav(myfavSaved);
        }
    }
    Toast.makeText(getApplicationContext(), "removed from fav", Toast.LENGTH_SHORT).show();
}

public void saveFav(ArrayList<Post> arrayList) {
    TinyDB tinydb = new TinyDB(getApplicationContext());
    tinydb.putListObject("fav", arrayList);
}

您总是在
add()
(第一行)中添加新的对象列表。 相反,您必须首先检索listobject,然后添加新的收藏夹帖子

试试这个:

 public void add(String name, String url) {
    //code to retrieve your arraylist stored in SharedPrefs
    TinyDB tinydb = new TinyDB(getApplicationContext());
    ArrayList<Object> postObjects = tinydb.getListObject("fav", Post.class);
    ArrayList<Post> myfav = new ArrayList<Post>();
    for(Object objs : postObjects){
        myfav.add((Post)objs);
    }
        Post post = new Post();
        post.setImgUrl(url);
        post.setName(name);
        myfav.add(post);
        tinydb.putListObject("fav", myfav);
        Toast.makeText(getApplicationContext(), "added to fav", Toast.LENGTH_SHORT).show();
 }
public void add(字符串名称、字符串url){
//用于检索存储在SharedPrefs中的arraylist的代码
TinyDB TinyDB=新的TinyDB(getApplicationContext());
ArrayList postObjects=tinydb.getListObject(“fav”,Post.class);
ArrayList myfav=新的ArrayList();
对于(对象对象对象:postObjects){
myfav.add((Post)objs);
}
Post Post=新Post();
post.setImgUrl(url);
post.setName(name);
myfav.add(post);
tinydb.putListObject(“fav”,myfav);
Toast.makeText(getApplicationContext(),“添加到fav”,Toast.LENGTH_SHORT.show();
}
 public void add(String name, String url) {
    //code to retrieve your arraylist stored in SharedPrefs
    TinyDB tinydb = new TinyDB(getApplicationContext());
    ArrayList<Object> postObjects = tinydb.getListObject("fav", Post.class);
    ArrayList<Post> myfav = new ArrayList<Post>();
    for(Object objs : postObjects){
        myfav.add((Post)objs);
    }
        Post post = new Post();
        post.setImgUrl(url);
        post.setName(name);
        myfav.add(post);
        tinydb.putListObject("fav", myfav);
        Toast.makeText(getApplicationContext(), "added to fav", Toast.LENGTH_SHORT).show();
 }