4

I am trying to create an application for android in which I want to keep AutoCompleteTextView to show suggestion to reduce the efforts of users. Currently I am testing with a small code I wrote but I am not getting suggestions. I am putting up the code, Please help me to find the error.

public class MainActivity extends Activity {

private ArrayList<Map<String, String>> objects;
private AutoCompleteTextView autotextView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    autotextView = (AutoCompleteTextView) findViewById(R.id.autocompleteView);

    objects = new ArrayList<Map<String, String>>();

    Map<String, String> NamePhoneType = new HashMap<String, String>();
    NamePhoneType.put("Name", "John");
    NamePhoneType.put("Phone", "1234567890");
    objects.add(NamePhoneType);
    NamePhoneType.put("Name", "Steve");
    NamePhoneType.put("Phone", "4567890123");
    objects.add(NamePhoneType);

    ArrayAdapter<Map<String, String>> am = new ArrayAdapter<Map<String, String>>(
            this, R.layout.list_item, objects);

    autotextView.setAdapter(am);
}
}

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/ccontName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Name"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#A5000000" />

<TextView
    android:id="@+id/ccontNo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/ccontName"
    android:text="Phone"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#A5000000" />

</RelativeLayout>
AndroidLearner
  • 4,500
  • 4
  • 31
  • 62
android_newbie
  • 667
  • 2
  • 14
  • 32
  • http://stackoverflow.com/questions/12691679/android-autocomplete-textview-similar-to-the-facebook-app – Omarj Nov 19 '12 at 15:17
  • 1
    I guess you should write your own ArrayAdapter to handle list item. Actually i'm surprised that app didn't crash. – yahya Nov 30 '12 at 21:33
  • When I look at the layout xml code, I don't see any element/view of type AutoCompleteTextView declared with an Id attribute called autocompleteView or am I missing something? – yaddly Feb 09 '21 at 19:48

1 Answers1

5

I had the same problem as yours. But manage to solve it by adding this

    am.notifyDataSetChanged();

after

    ArrayAdapter<Map<String, String>> am = new ArrayAdapter<Map<String, String>>(this, R.layout.list_item, objects);
    autotextView.setAdapter(am);

Hope that this will help :)

Lee Yi Hong
  • 1,310
  • 13
  • 17