簡體   English   中英

AutoCompleteTextView導致ListView中的項目消失

[英]AutoCompleteTextView causes items in ListView to disappear

我正在做Android教程中“添加列表”部分的“額外信用”部分。 我嘗試用ArrayAdapter中包含的項目填充AutoCompleteTextView(ACTV),但是如果addr字段中的字符超出閾值限制,則該列表不顯示任何項目。 這是代碼:

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(){
    ...
};
}

還有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>

更改completionThreshold會更改在列表中的項目消失之前可以輸入的字符數。

謝謝您的幫助

您正在嘗試對ListViewAutoCompleteTextView使用相同的適配器。 那不是一個好主意。 請使用單獨的適配器。 您甚至可能希望為與AutoCompleteTextView一起使用的適配器提供單獨的布局(例如,在本示例項目中使用的android.R.layout.simple_dropdown_item_1line )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM