2012-04-29 26 views
0

我正在做Android教程中添加列表部分的額外功能部分。我嘗試使用ArrayAdapter中包含的項目填充AutoCompleteTextView(ACTV),但如果addr字段中的字符數超過閾值限制,則列表不會顯示任何項目。下面是代碼:AutoCompleteTextView導致ListView中的項消失

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改變字符在列表中的項目消失之前,我可以進入的數量。

感謝您的幫助

回答

3

你試圖使用同一個適配器爲ListViewAutoCompleteTextView兩者。這不是一個好主意。請使用單獨的適配器。您甚至可能需要與AutoCompleteTextView一起使用的適配器的單獨佈局(例如,android.R.layout.simple_dropdown_item_1line,正如我在this sample project中所使用的)。

+0

偉大的修復消失的項目。你能詳細說明爲什麼你需要單獨的適配器嗎?另外,您將如何使用列表中的數據填充建議清單?現在,它使用硬編碼的字符串,但不能與列表中的數據一起使用。 – Richard

+0

@Richard:「你能詳細說明爲什麼你需要單獨的適配器嗎?」 - 因爲它們擁有兩個託管'AdapterViews'之間需要不同的數據,特別是用於'AutoCompleteTextView'的過濾器。鑑於您提供的有限信息,我無法幫助您。 – CommonsWare

+0

當然哦。愚蠢的錯誤。謝謝您的幫助。 – Richard