5

enter image description here

Please help me out with this issue:

I want to display country and code when i search in the autocomplete text view and its working fine but iam getting twice values rather than single 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 listview 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" />

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Danny
  • 1,050
  • 3
  • 11
  • 26

3 Answers3

0
 new SimpleAdapter(getBaseContext(), 
                   list, R.layout.twolist,
                   new String[] {"code","country"},
                   new int[] { R.id.textView1,R.id.textView2});

Here you are using:

R.id.textView1,R.id.textView2

So it will display it in both Textviews.

Viswanath Lekshmanan
  • 9,945
  • 1
  • 40
  • 64
  • yeah @Arju but im displaying the code and country as well in the list to appear when i search in textbox and i tried with textview1 also by not giving the other textview, in that case it appends only code but not country.so can u pls check the image above and solve it. – Danny Mar 30 '13 at 06:15
  • @Danny its a hell prob iam trying it step by step – Viswanath Lekshmanan Mar 30 '13 at 08:58
  • I gave you a downvote just for that horrible formatting. – Ali Bdeir Aug 03 '16 at 14:26
  • @AbAppletic Theres only a line of code and it is readable. Edited for formatting ? – Viswanath Lekshmanan Aug 04 '16 at 05:09
  • @ViswanathLekshmanan I edited it for formatting, but your effort in making it READABLE (No, it wasn't readable at all) deserved that downvote. I have no intention of discussing this futher with you. – Ali Bdeir Aug 04 '16 at 11:12
  • Oh and not only the formatting was bad, but also you were too lazy to even type in "you" right. (*"u"? Seriously? **We're not on Instagram, we're on a programmer's Q&A site***) – Ali Bdeir Aug 04 '16 at 11:14
  • OP got what I said. What's wrong with you ? Finding mistakes give you satisfaction ? **SERIOUSLY** ? – Viswanath Lekshmanan Aug 04 '16 at 15:21
0

You use this code in you implementation for removing duplication item in 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);
Pratik
  • 30,639
  • 18
  • 84
  • 159
  • updated the above code but still now iam getting the duplicate values the issue is not fixed :( @Pratik – Danny Mar 30 '13 at 06:19
  • Hi @Danny check out this post answer you need to filter using custom adapter http://stackoverflow.com/a/8784931/760489 – Pratik Mar 30 '13 at 06:30
  • yeah but is there any method are API to display only distinct values of which it should not get repeated values and can u post the simple logic @Pratik – Danny Mar 30 '13 at 09:08
0

I faced the same issue, But simple answer for this problem is that your List that you populate through adapter in the AutoCompleteTextView, contains duplicates. Because AutoComplete will always populate only unique Data.

Quintin Balsdon
  • 5,484
  • 10
  • 54
  • 95
anand jha
  • 9
  • 1
  • 2