我正在做 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 会更改在列表中的项目消失之前我可以输入的字符数。
谢谢您的帮助