自动填充文本视图无法正常工作

时间:2012-01-04 11:54:37

标签: android

在我的联系人列表应用程序中,我尝试在搜索联系人时使用autocompletetextview但是无法正常工作

这是我的Search.java:

public class Search extends  ListActivity {

 private static int[] TO = {R.id.name };
private static String[] FROM = {DbConstants.NAME, DbConstants.PHONE, DbConstants.EMAIL,_ID};
private Button sButton;

 private ListView lv1;
 private static SQLiteDatabase db;
  private DbCreate contacts;
  private Cursor cursor;
  private AutoCompleteTextView searchText;
  protected SimpleCursorAdapter adapter;


protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search);

    ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,new String[] {DbConstants.NAME});
    searchText=(AutoCompleteTextView)findViewById(R.id.searchtext);
    searchText.setAdapter(adapter);

    sButton=(Button)findViewById(R.id.searchButton);
    sButton.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            showDatabaseContent();
              lv1 = getListView();

              lv1.setTextFilterEnabled(true);
        }
    });





}

这是我的search.xml:

<?xml version="1.0" encoding="utf-8" ?> 
       <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"           android:orientation="vertical" android:layout_width="fill_parent"           android:layout_height="fill_parent">
      <LinearLayout 
       android:orientation="horizontal" 
       android:layout_width="fill_parent" 
      android:layout_height="wrap_content">

       <AutoCompleteTextView
     android:id="@+id/searchtext"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:hint="@string/searchDefault">

     <requestFocus />
         </AutoCompleteTextView> 
       <Button android:id="@+id/searchButton" 
      android:icon="@drawable/search" 
       android:text="Search"
      android:layout_width="wrap_content" 
       android:layout_height="wrap_content" /> 
        </LinearLayout>
        <TextView
    android:id="@android:id/empty"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/empt" />
        <ListView
 android:id="@android:id/list"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content" />

    </LinearL ayout>

我没有得到任何错误但是当我按下搜索按钮时搜索有效。但是当我开始输入名称时没有任何反应。问题出在哪里?

2 个答案:

答案 0 :(得分:3)

AutoCompleteTextView

下的XML文件中添加以下行
android:completionThreshold="1"

它可能会起作用,试试吧。

答案 1 :(得分:2)

AutoComplete TextView

的这些方法在哪里?
@Override
public void afterTextChanged(Editable arg0) {
 // TODO Auto-generated method stub
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
  int after) {
 // TODO Auto-generated method stub
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
 // TODO Auto-generated method stub
}
}

请参阅此基本Example