从ListView android中删除重复项

时间:2011-10-03 10:25:20

标签: android listview duplicates

我发布了我的代码,我无法从列表视图中删除重复的值? 有人可以帮帮我吗?提前致谢! 我在这里粘贴我的代码并使用了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;
    }
};

2 个答案:

答案 0 :(得分:12)

如果您不想允许重复收集对象,请使用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);

根据您的情况更新:

使用建议对象而不是上面的 al 对象。

答案 1 :(得分:-1)

您可以使用该代码:

    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);