问题
I am posting my code, I am unable to remove the duplicate values from the listview?
Can someone please help me? Thanks in advance!
I am pasting my code here and I have used 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);
}
});
And the second of the code
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;
}
};
回答1:
If you don't want to allow duplicate into collection of your object then use the Set object it will now allow the duplicate value.
Set suggestion = new HashSet();
or you can add your list object to this object it will now add duplicate value
ArrayList al = new ArrayList();
// add elements to al, including duplicates
HashSet hs = new HashSet();
hs.addAll(al);
al.clear();
al.addAll(hs);
updated for your case:
use your suggestion object instead of the above al object just it.
回答2:
You could use that code:
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);
来源:https://stackoverflow.com/questions/7633742/removing-duplicates-from-listview-android