2012-12-05 45 views
1

我是Android的初學者。當我試圖將SQLite數據庫中的值填充到AutoCompleteTextView時,我遇到了一個問題。我通過函數getAllFnames() [* (如下面顯示的 *)]獲取數據庫中的值到字符串數組。但AutoCompleteTextView沒有在運行時顯示任何建議..任何人在那裏幫助我.....代碼在下面給..請檢查..謝謝你..(Log Cat顯示一些過濾問題!)Android:AutoCompleteTextView

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()); 
} 

回答

0

試試這個代碼。使用您的數組數組來代替Months數組。而是使用數組列表來代替1000的數組。它更高效。

公共類自動完成您可以使用此

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

嘗試這種延伸活動{

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); 

    } 

} 
0

。 Thanx。

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)); 
+0

@Anu請通過接受正確的答案關閉問題。 –

+0

我得到了答案..我可以找到任何選項來關閉這個問題......? – Anu

+0

@Anu當你接受任何幫助你的正確答案時。 –

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[] {}; 
    } 

and

 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感謝名單向所有的專家...