java - 自动完成文本问题在列表中获取重复值?

标签 java android autocompletetextview

enter image description here

请帮我解决这个问题:

我想在自动完成 TextView 中搜索时显示国家和代码,它工作正常,但我得到的是两个值而不是单个值。

MainActivity.java:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    String[] countries = new String[] {
                "Allentown",
            "Albuquerque","Aberdeen","Acapulco","Waco","Atlantic City",
            "Marka-Amman","Pittsburgh","Augusta","Auckland","Albany",
            "Amarillo","Amman","Amsterdam","Anchorage","Antwerp",
            "Naples","Aqaba","Stockholm","Aspen","Asuncion"......};

    String[] codes=new String[]{
        "ABE","ABQ","AMS","ANC","ANR","APF","AQJ","ARN","ASE","ASU","ATH",
    .............}; 
    List<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
    for(int i=0;i<countries.length;i++){
        HashMap<String, String> curGroupMap = new HashMap<String,String>();
        curGroupMap.put("country", countries[i]);
        curGroupMap.put("code", codes[i]);
        list.add(curGroupMap);
    }
    final AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);
    SimpleAdapter adapter = 
            new SimpleAdapter(getBaseContext(), list, R.layout.twolist, new String[] {"code","country"}, new int[] { R.id.textView1,R.id.textView2});
    textView.setAdapter(adapter);
    textView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> p, View v, int pos, long id) {
            Map<String, String> map = (Map<String, String>) p.getItemAtPosition(pos);
            String itemName = map.get("code");
            textView.setText(itemName);
        }
    });
 }

}

我获取 ListView 的 xml 是 twolist.xml:

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="horizontal" >

    <TextView
     android:id="@+id/textView1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="TextView" />
   <TextView
      android:id="@+id/a"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="-" />

  <TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp"
    android:text="TextView" />

最佳答案

您在实现中使用此代码删除列表中的重复项

List<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
for(int i=0;i<countries.length;i++){
    HashMap<String, String> curGroupMap = new HashMap<String,String>();
    curGroupMap.put("country", countries[i]);
    curGroupMap.put("code", codes[i]);
    list.add(curGroupMap);
}

// adde elements to list, including duplicates
HashSet hs = new HashSet();
hs.addAll(list);
list.clear();
list.addAll(hs);

关于java - 自动完成文本问题在列表中获取重复值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15715552/

相关文章:

java - 为什么库模块 android.support.test 在添加依赖项中不可见

java - 在centos for java上安装opencv

android - 在 Android 中创建类似网格的布局

java - 使用按钮存储附加信息

android - phonegap 开发者应用程序的行为与已安装的 apk 不同

android - 如何从 HashMap 绑定(bind) AutoCompleteTextView

java - AutoCompleteTextview 不显示第一个字符条目的下拉建议

Android:如何在 onItemClick 中获取 AutoCompleteTextView

java - 在 Tomcat 上运行的基于 Spring-Boot 的 WAR 上,在哪里以及如何启用 CORS?

java - Quartz 中的多个 SchedulerFactory