1

我是Android的初学者。当我尝试将 SQLite 数据库中的值填充到 AutoCompleteTextView 时出现问题。我通过函数getAllFnames() [*如下所示*] 从数据库获取值到字符串数组。但是 AutoCompleteTextView 在运行时没有显示任何建议..任何人都可以帮助我..下面给出了代码..请检查..谢谢..(Log Cat 显示一些过滤问题!)

 public String[] getAllFnames() {

     database=open();
     Fname=new String[1000];
     int i=0;
Cursor cursor = database.query(DbCreation.PATIENT_TABLE,Columns, null, null, null, null, null);
          cursor.moveToFirst();
          while (!cursor.isAfterLast()) {
          Fname[i]=cursor.getString(1).toString();
          Log.v("DataBaseValues...", Fname[i]);
          cursor.moveToNext();
          i++;
        }

        // Make sure to close the cursor
        cursor.close();
        return Fname;
}

在文本视图侧

      Fname=ss.getAllFnames();//here Fname is a string array
             //"ss" is the object of class where method getAllFname()  is placed.
ArrayAdapter<String> fnameAdapter=new ArrayAdapter<String>(this,R.layout.list,Fname);

     fnameSearch.setThreshold(1);//fnameSearch is the AutoCompleteTextView

     fnameSearch.setAdapter(fnameAdapter);

     fnameSearch.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
}
4

4 回答 4

0

我通过以下方式更改了方法 getAllFnames()

   public String[] getAllFnames() {

     database=open();


         Cursor cursor = database.query(DbCreation.PATIENT_TABLE,Columns, null, null, null, null, null);
         if(cursor.getCount() >0){
             String []  Fname=new String[cursor.getCount()];
             int i=0;
            while (cursor.moveToNext()) {

           Fname[i]=cursor.getString(0).toString();//+" "+cursor.getString(1).toString();
          Log.v("DataBaseValues...", Fname[i]);

          i++;
        }
        // Make sure to close the cursor
        cursor.close();
        return Fname;
         }
         else return new String[] {};
  }

     String [] Fname=ss.getAllFnames();

      int i=0;
      Log.v("insidichSearchAct..", Fname[i]);

     ArrayAdapter<String> fnameAdapter=new ArrayAdapter<String>(this,R.layout.fname_search_list,Fname);
     fnameSearch.setThreshold(1);
     fnameSearch.setAdapter(fnameAdapter);

然后我得到了 AutoCompleteTextView 的建议值 感谢所有专家...

于 2012-12-06T16:00:04.313 回答
0

试试这个代码。使用您的数据数组代替 Months 数组。而是使用 arraylist 代替大小为 1000 的数组。它更有效。

公共类自动完成扩展了 Activity {

 static final String[] Months = new String[]{
    "January","February","March","April","May","June","July","August",
    "September","October","November","December"
    };

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.autocomplete);

    ArrayAdapter<String> monthArray = new ArrayAdapter<String>(this,
            android.R.layout.simple_dropdown_item_1line, Months);


    final AutoCompleteTextView textView = (AutoCompleteTextView)findViewById(R.id.testAutoComplete);
            textView.setAdapter(monthArray);

   }

}
于 2012-12-05T12:17:36.347 回答
0

你用这个

ArrayAdapter<String> fnameAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_dropdown_item_1line, Fname);

尝试这个。谢谢。

于 2012-12-05T12:17:49.163 回答
0

我在这里看到的唯一问题是(假设您从数据库中获取值)

ArrayAdapter<String> fnameAdapter=new ArrayAdapter<String>(this,R.layout.list,Fname);

将其替换为

myAutoComplete.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, Fname));
于 2012-12-05T12:20:48.027 回答