如何确保arrayadapter不接受重复的值 [英] how to ensure an arrayadapter does not accept duplicate values

查看:1038
本文介绍了如何确保arrayadapter不接受重复的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义arrayadapter一些pre_populated数据多的数据要被从所述用户添加到它input.The问题是重复的值仍被添加到arrayadapter..here是我code

 的String; //拥有用户输入数据的for(int i = 0; I< my_adapter.getCount();我++){
 MyCollection的itemObject = my_adapter.getItem(ⅰ);
 // MyCollection的是从集合类对象
 字符串C = itemObject.toString();     如果(c.matches(多个)){
     //不加s到阵列适配器}其他{
   //添加s到arrayadapter
    my_arrayvalues​​.add(新MyCollection的(多个));
    my_adapter.notifyDataSetChanged();
}

运行上面的code,没有变化是影响适配器上甚至当值不match..if我只是跑项目,而不包括上述
    重复值是我added.how能纠正这种?

以下的建议,但被添加的还是重复我做的答案后:
更新code

  HS =新的HashSet();
             my_arrayvalues​​.add(新MyCollections(S));
             hs.addAll(my_arrayvalues​​);
             my_arrayvalues​​.clear();
             my_arrayvalues​​.addAll(HS);
             my_adapter.notifyDataSetChanged();


解决方案

F你不想重复允许进入你的对象集合,然后使用Set对象,现在将允许重复的值。

 设置建议=新的HashSet();

也可以添加列表对象,这个对象,现在将增加重复的值

  ArrayList的人=新的ArrayList();
//添加元素人,包括重复
HashSet的HS =新的HashSet();
hs.addAll(人);
al.clear();
al.addAll(HS);

更新你的情况:

使用你的建议对象,而不是上面的人的对象只是它。

i have a custom arrayadapter with some pre_populated data and more data is to be added to it from the user input.The problem is that duplicate values are still added to the arrayadapter..here is my code:

String s;//holds data from user input

for(int i=0 ; i<my_adapter.getCount() ; i++){
 MyCollection itemObject=my_adapter.getItem(i);
 //MyCollection is an object from the collection class
 String c=itemObject.toString();

     if(c.matches(s)){
     //do not add s to array adapter

}else{
   //add s to arrayadapter
    my_arrayvalues.add(new MyCollection(s));
    my_adapter.notifyDataSetChanged();
}

Running the above code,no change is effected on the adapter even when the the values do not match..if i just run the project without including the above duplicate values are added.how can i correct this?

after following the answers i have done as suggested but still the duplicates are being added: the updated code

             hs = new HashSet();
             my_arrayvalues.add(new MyCollections(s));
             hs.addAll(my_arrayvalues);
             my_arrayvalues.clear();
             my_arrayvalues.addAll(hs);
             my_adapter.notifyDataSetChanged();

解决方案

f 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.

这篇关于如何确保arrayadapter不接受重复的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆