AutoText error to get duplicate values ​​in a list?

enter image description here

Please help me with this problem:

I want to display the country and code when searching in the text representation of autocomplete and its working mode, but iam gets two values, not one value.

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

}

And my xml for getting the list is 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" />

+5
source share
3 answers
 new SimpleAdapter(getBaseContext(), 
                   list, R.layout.twolist,
                   new String[] {"code","country"},
                   new int[] { R.id.textView1,R.id.textView2});

Here you use:

R.id.textView1,R.id.textView2

Thus, it will display it as in Textviews.

0
source

This code is used in the implementation to remove the duplication element in the list.

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

. , , AutoCompleteTextView, . AutoComplete .

0
source

All Articles