【问题标题】:Removing Duplicates From ListView android从ListView android中删除重复项
【发布时间】:2011-11-29 19:54:52
【问题描述】:

我正在发布我的代码,我无法从列表视图中删除重复值? 有人可以帮帮我吗?提前致谢! 我在这里粘贴我的代码,我使用了BaseAdapter


@Override
            public void onCompleted(final List<Recommendable> result) {
                android.util.Log.w("suggestionview>>>>>", "suggestion"+ result.size());
                ((Activity) mContext).runOnUiThread(new Runnable() {
                    public void run() {
                        Iterator<Recommendable> itr = result.iterator();
                        while (itr.hasNext()) {
                            Recommendable element = itr.next();
                            suggestions.add(element);
                            android.util.Log.w("suggestionview", "Adding elements::>"+suggestions.add(element));
                        }
                        suggestionListView.setAdapter(new Suggestiondapter(mContext));
                        android.util.Log.w("suggestionview","suggestion adapter Values::>"+suggestionListView);
                    }
                });

以及第二个代码

public class Suggestiondapter extends BaseAdapter {


    // private LayoutInflater mInflater = null;
    private Context mContext;

    public Suggestiondapter(Context mContext) {
        this.mContext=mContext;
        android.util.Log.w("Suggestion Adapter","vlues are comming.....");
    }

    @Override
    public int getCount() {
        android.util.Log.w("suugestion adapter","suggstion size::>"+suggestions.size());
        return suggestions.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Recommendable recommendable = suggestions.get(position);
        if(convertView==null){
            android.util.Log.w("convertView", "adaptervalues........::>"+ recommendable.Program);
            android.util.Log.w("series conveter", "program series values::>"+recommendable.Series);
            convertView = new HeaderView(mContext, recommendable.Program,recommendable.Series, SuggestionView.class);
        }else{
            Toast.makeText(mContext, "HelloView", Toast.LENGTH_SHORT);
        }
        return convertView;
    }
};

【问题讨论】:

    标签: android listview duplicates


    【解决方案1】:

    如果您不想允许重复到对象的集合中,则使用 Set 对象,它现在将允许重复值。

    Set suggestion = new HashSet();
    

    或者您可以将您的列表对象添加到此对象,它现在将添加重复值

    ArrayList al = new ArrayList();
    // add elements to al, including duplicates
    HashSet hs = new HashSet();
    hs.addAll(al);
    al.clear();
    al.addAll(hs);
    

    根据您的情况更新:

    使用您的 suggestion 对象而不是上面的 al 对象。

    【讨论】:

    • 这个建议是有效的,但我会用以下方式修改它:声明集合,在将元素添加到适配器之前检查其中是否存在元素,将元素添加到集合中。因此,您将避免移动大量数据。
    【解决方案2】:

    您可以使用该代码:

        private static HashSet<String> hs = new HashSet<String>();
        private static ArrayList<String> list= new ArrayList<String>();
    
        for(int i=0; i<list.size(); i++){
            hs.add(list.get(i));
    
        }
    
        list.clear();
        list.addAll(hs);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-08
      • 1970-01-01
      • 2015-08-04
      • 1970-01-01
      • 2014-01-20
      相关资源
      最近更新 更多