【问题标题】:Add item in listview only if not already present仅当尚未存在时才在列表视图中添加项目
【发布时间】:2012-11-17 09:44:16
【问题描述】:

我想在我的 Android 小部件中添加一个新项目,前提是该项目尚未出现在列表中。

我尝试使用 contains() 方法,但还是不行。

事实上,我得到了一个 JSON 对象,对于每个 JSON 对象,我想测试列表中是否已经存在与此 JSON 对象对应的项目。

如果是,什么都不做,如果不是,将项目添加到列表的顶部。

这是我的代码:

//parsing data
        String titre, url, site, ancienprix, nouveauprix, temperature, pseudo, categorie, dealtermine, date, id;

        try{
            jArray = new JSONArray(result);
            JSONObject json_data=null;

            SharedPreferences setNbDeal = PreferenceManager.getDefaultSharedPreferences(mContext);
            String nbDeal = setNbDeal.getString("nbDeal", "30");

            int nbDeals = Integer.parseInt(nbDeal);

            for(int i=0;i<nbDeals;i++){
                json_data = jArray.getJSONObject(i);
                titre=json_data.getString("titre");
                url=json_data.getString("url");
                site=json_data.getString("site");
                ancienprix=json_data.getString("ancienprix");
                nouveauprix=json_data.getString("nouveauprix");
                dealtermine=json_data.getString("dealtermine");
                temperature=json_data.getString("temperature");
                pseudo=json_data.getString("pseudo");
                categorie=json_data.getString("categorie");
                date=json_data.getString("date");
                id=json_data.getString("id");

                WidgetItem currentItem = new WidgetItem(titre, url, site, ancienprix, nouveauprix, dealtermine, temperature, pseudo, categorie, date);

                if (mWidgetItems.contains(currentItem)){
                    break;
                } else {
                    mWidgetItems.add(currentItem);
                }
            }
        }

这是声明 mWidgetItem 的代码:

private List<WidgetItem> mWidgetItems = new ArrayList<WidgetItem>();

这是我的 Widget Item 类的代码:

public class WidgetItem {
    public String titre, url, ancienprix, nouveauPrix, temperature, site, submitter, categorie, dealTermine, date;


    public WidgetItem(String titre, String url, String site, String ancienprix, String nouveauPrix, String dealTermine, String temperature, String submitter, String categorie, String date) {
        this.titre = titre;
        this.url = url;
        this.site = site;
        this.ancienprix = ancienprix;
        this.nouveauPrix = nouveauPrix;
        this.dealTermine = dealTermine;
        this.temperature = temperature;
        this.submitter = submitter;
        this.categorie = categorie;
        this.date = date;
    }
}

【问题讨论】:

  • 您的currentItem 是指向WidgetItem 的指针,而不是WidgetItem 本身。基础 Java。
  • 您能发布 WidgetItem 类的代码以及声明 mWidgetItems 变量的代码吗?
  • 您需要在 WidgetItem 类中使用 equal() 方法来比较两个 WidgetItem 对象。
  • 好的。我已经用 mWidgetItem 列表的声明和 WidgetItem 类的代码编辑了帖子。

标签: android android-widget android-listview updates


【解决方案1】:

您可以在将所有元素添加到列表中后使用此方法(它会删除重复项):

private ArrayList<WidgetItem> getUniqueElements(Collection<WidgetItem> values)
{
    return new ArrayList<WidgetItem>(new TreeSet<WidgetItem>(values));
}

【讨论】:

    猜你喜欢
    • 2022-11-12
    • 2016-10-19
    • 2018-09-19
    • 2023-03-19
    • 2023-03-26
    • 2022-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多