arraylist包含具有共享首选项的重复值

时间:2018-12-24 09:49:46

标签: android android-studio sharedpreferences

我注意到一个非常奇怪的情况……

  private void saveFavArticles() {
    SharedPreferences pref = getSharedPreferences(PUT_FAVORITE_CRNT_ARTICLE_KEY, Context.MODE_PRIVATE);
    String currentFavList = pref.getString(PUT_FAVORITE_CRNT_ARTICLE_KEY, "");
    SharedPreferences.Editor editor;
    ArrayList<Article> articles = new ArrayList<>();
    //if we have favorite articles
    if(currentFavList != "") {
        Type type = new TypeToken<List<Article>>(){}.getType();
        articles = new Gson().fromJson(currentFavList, type);
        //It returns true if the specified element is found in the list else it gives false.
        if(articles.contains(currentArticle))
            Toast.makeText(this, "this article exists into favorite list", Toast.LENGTH_SHORT).show();
        else{
            articles.add(currentArticle);
            Toast.makeText(this, "current article added to favorites", Toast.LENGTH_SHORT).show();
        }
        editor = pref.edit();
        editor.putString(DetailsActivity.PUT_FAVORITE_CRNT_ARTICLE_KEY, new Gson().toJson(articles));
    }else {
        editor = pref.edit();
        articles.add(currentArticle);
        editor.putString(DetailsActivity.PUT_FAVORITE_CRNT_ARTICLE_KEY, new Gson().toJson(articles));
        Toast.makeText(this, "current article added to favorites", Toast.LENGTH_SHORT).show();
    }
    editor.commit();
}

我正在调试此语句…问题是,即使我的数组列表包含currentArticle,它也总是说不是。

有什么帮助吗?

2 个答案:

答案 0 :(得分:1)

由于商品arraylist是自定义对象的列表,因此,假设默认实现不是您所需要的,则需要覆盖equals方法。

  

List.contains(...)方法定义为使用equals(Object)来   决定参数对象是否由列表“包含”。   当您比较对象(带有contains方法)时,它实际上是比较JAVA生成的哈希码。

因此您的模型类必须覆盖等于 hashCode 方法

public class NavBarItem
{
    public NavBarItem()
    {
        Childs = new List<NavBarItem>();
    }
    public int ID { get; set; }
    public int? ParentID { get; set; }
    public string Text { get; set; }
    public List<NavBarItem> Childs { get; set; }
    public override string ToString()
    {
        return Text;
    }
}

我的示例模型类是

  @Override
public boolean equals(final Object object) {

    if (object instanceof Selector) {

        Selector target = (Selector) object;

        return target.name.equalsIgnoreCase(name);

    }

    return false;

}

@Override
public int hashCode() {
    return toString().hashCode();
}

在这里,我根据 NAME 比较我的对象,如果您使用的话,可以使用变量。

答案 1 :(得分:0)

您的共享首选项标识符键和放置文本标识符键相同,这就是它发生问题的原因。我只是更改了放置字符串标识符键。现在可以了。希望它也对您有用

private String FAV_LIST = "fav_list";

private void saveFavArticles() {
    SharedPreferences pref = getSharedPreferences(PUT_FAVORITE_CRNT_ARTICLE_KEY, Context.MODE_PRIVATE);
    String currentFavList = pref.getString(FAV_LIST, "");
    SharedPreferences.Editor editor;
    ArrayList<Article> articles = new ArrayList<>();
    //if we have favorite articles
    if (currentFavList != "") {
        Type type = new TypeToken<List<Article>>() {
        }.getType();
        articles = new Gson().fromJson(currentFavList, type);
        //It returns true if the specified element is found in the list else it gives false.
        if (articles.contains(currentArticle))
            Toast.makeText(this, "this article exists into favorite list", Toast.LENGTH_SHORT).show();
        else {
            articles.add(currentArticle);
            Toast.makeText(this, "current article added to favorites", Toast.LENGTH_SHORT).show();
        }
        editor = pref.edit();
        editor.putString(FAV_LIST, new Gson().toJson(articles));
    } else {
        editor = pref.edit();
        articles.add(currentArticle);
        editor.putString(FAV_LIST, new Gson().toJson(articles));
        Toast.makeText(this, "current article added to favorites", Toast.LENGTH_SHORT).show();
    }
    editor.commit();
   }   
 }