2010-05-17 50 views
-2

我使用autocompletetextview和SimpleCursorAdapter从sqlite获取数据。我想要得到由输入键开始的下拉列表。在我的autocompletetextview中,列表不会被输入文本显示或过滤。Autocomplettextview通过输入键过滤

例如, 如果用户输入“an”,所有以“an”开头的文本都会出现在这个列表中。

在Java

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.new); 

     txtPNo = (AutoCompleteTextView) findViewById(R.id.txtSTo);   

     mDbHelper = new DBAdapter(this); 
     mDbHelper.open(); 

     SimpleCursorAdapter notes = fillToData(); 
     txtPNo.setAdapter(notes); 
} 

    private SimpleCursorAdapter fillToData() { 
     Cursor c = mDbHelper.getName(); 
     startManagingCursor(c); 
     String[] from = new String[] {DBAdapter.Name,DBAdapter.No1}; 
     int[] to = new int[] {R.id.txtName,R.id.txtNo1}; 

     Log.d(TAG, "cursor.getCount()=" + c.getCount()); 
     SimpleCursorAdapter notes = 
      new SimpleCursorAdapter(this, R.layout.autocomplete, c, from, 
to); 

     return notes; 
    } 

在new.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:background="#ffffff" 
    > 
    <AutoCompleteTextView android:id="@+id/txtSTo" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textSize="18sp" 
     android:textColor="#000000"  
     android:hint="To"  
     android:completionThreshold="1" 
     android:selectAllOnFocus="true" 
     android:layout_alignParentTop = "true" 
     android:layout_alignParentLeft="true"/>  
</RelativeLayout> 

在autocomplete.xml

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="wrap_content" 
    android:stretchColumns="0" android:padding="5dp"> 
    <TableRow android:padding="5dp"> 
     <LinearLayout android:orientation="vertical"> 
      <TextView android:id="@+id/txtName" 
       xmlns:android="http://schemas.android.com/apk/res/android" 
       android:textColor="#000000" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"/> 
      <TextView android:id="@+id/txtNo1" 
       xmlns:android="http://schemas.android.com/apk/res/android" 
       android:textColor="#000000" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"/> 
     </LinearLayout> 
    </TableRow> 
</TableLayout> 

如何实现得到公正过滤列表?

回答

0
txtPNo = (AutoCompleteTextView) findViewById(R.id.txtSTo);   

    mDbHelper = new DBAdapter(this); 
    mDbHelper.open(); 

    SimpleCursorAdapter notes = fillToData(); 
    txtPhoneNo.setAdapter(notes); 

嗯,如果你只是复制正粘贴代码...

的问题是变量名。


AutoCompleteTextView

+0

遗憾。 txtPNo.setAdapter(附注); – soclose 2010-05-19 01:30:04

+0

好吧,我认为你的问题是你没有正确设置SimpleCursorAdapter。如果你看看http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html它说你需要设置一个过滤器。另请看:http://developer.android.com/reference/android/widget/CursorAdapter.html#runQueryOnBackgroundThread(java.lang.CharSequence) – Brian 2010-05-19 05:54:37