Richard
Richard

Reputation: 242

AutoCompleteTextView causes items in ListView to disappear

I'm doing the Extra Credit section of the Adding a List section in Android Tutorials. I tried populating the AutoCompleteTextView (ACTV) with the items contained in the ArrayAdapter, but if the addr field has characters in it above the threshold limit, the list doesn't show any items. Here is the code:

public class MyActivity extends Activity
{
List<Restaurant> model = new ArrayList<Restaurant>();
ArrayAdapter<Restaurant> adapter = null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button save = (Button)findViewById(R.id.save);
    save.setOnClickListener(onSave);

    ListView list = (ListView)findViewById(R.id.restaurants);

    adapter = new ArrayAdapter<Restaurant>(this,
                    android.R.layout.simple_list_item_1,
                    model);
    list.setAdapter(adapter);

    AutoCompleteTextView textView = (AutoCompleteTextView)findViewById(R.id.addr);
    textView.setAdapter(adapter);
}

private View.OnClickListener onSave= new View.OnClickListener(){
    ...
};
}

And the XML...

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent">
<TableLayout
    android:id="@+id/details"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:stretchColumns="1"
    android:shrinkColumns="1"
    android:layout_alignParentBottom="true"
    >
    <TableRow>
        <TextView android:text="Name:"/>
        <EditText android:id="@+id/name"/>
    </TableRow>

    <TableRow>
        <TextView android:text="Address:"/>
        <AutoCompleteTextView android:id="@+id/addr"
                              android:completionThreshold="5"
                />
    </TableRow>
    <TableRow>
        <TextView android:text="Type:"/>
        <RadioGroup android:id="@+id/types">
            <RadioButton android:id="@+id/take_out"
                         android:text="Take Out"
                         android:layout_height="wrap_content"
                         android:layout_width="wrap_content"
                         android:checked="true"
                         />
            <RadioButton android:id="@+id/sit_down"
                         android:text="Sit Down"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"/>
            <RadioButton android:id="@+id/delivery"
                         android:text="Delivery"
                         android:layout_height="wrap_content"
                         android:layout_width="wrap_content"/>
        </RadioGroup>
    </TableRow>
    <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/save"
            android:text="Save"/>
</TableLayout>
<ListView android:id="@+id/restaurants"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_alignParentTop="true"
          android:layout_above="@id/details"/>
</RelativeLayout>

Changing the completionThreshold changes the number of characters I can enter before the items in the List disappear.

Thanks for the help

Upvotes: 0

Views: 2065

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007658

You are attempting to use the same adapter for both the ListView and the AutoCompleteTextView. That is not a good idea. Please use separate adapters. You might even want a separate layout for the adapter used with the AutoCompleteTextView (e.g., android.R.layout.simple_dropdown_item_1line, as I used in this sample project).

Upvotes: 3

Related Questions