2013-01-04 23 views
1

首先對不起的過濾的ListView,我的英語安卓:如何用simpleCursorAdapter

我看過很多對於這個問題的答案,但我不知道該怎麼做我的代碼運行。

我想過濾我的listView與上面的EditText文本。

這裏是我的代碼:

private DbAdapter mDbHelper; 
    private EditText mSearch; 




    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.baes_bat_list); 
     setTitle(R.string.baes_list_title); 
     mDbHelper = new DbAdapter(this); 
     mDbHelper.open(); 
     fillData(); 
     registerForContextMenu(getListView()); 
     mSearch = (EditText) findViewById(R.id.searchbox); 

    } 

    private void fillData() { 

     String search = ""; 
     search = mSearch.getText().toString(); 

     // Get all of the rows from the database and create the item list 
     Cursor baesCursor = mDbHelper.fetchAllBaes(search); 
     startManagingCursor(baesCursor); 

     // Create an array to specify the fields we want to display in the list (only TITLE) 
     String[] from = new String[]{DbAdapter.BAES_NOM, DbAdapter.BAES_BAT}; 

     // and an array of the fields we want to bind those fields to (in this case just text1) 
     int[] to = new int[]{R.id.text1, R.id.text2}; 

     // Now create a simple cursor adapter and set it to display 
    // Now create a simple cursor adapter and set it to display 
     SimpleCursorAdapter baes = 
      new SimpleCursorAdapter(this, R.layout.baes_row, baesCursor, from, to); 
     setListAdapter(baes); 

     }; 

這裏是我的數據庫適配器

public Cursor fetchAllBaes(String string) { 
     String [] columns=new String[]{BAES_ID +" as _id",BAES_NOM,BAES_BAT,BAES_LOC,BAES_MISESERVICE,BAES_COUP,BAES_DATE_VISITE,BAES_ETAT, BAES_SYNC}; 
     String [] search = null; 
     return mDb.query(TABLE_BAES, columns, BAES_BAT + "= ?", search, null, null, null); 
    } 

我想我錯過了一步,但I'don't知道。感謝您的幫助。

+0

只要使用這個鏈接... HTTP://mfarhan133.wordpress.com/2010/10/01/autocomplete- text-view/ –

+0

我已經看到了關於這個主題的教程,但我不知道它是如何將數組適配器的過濾代碼調整到simplecursoradapter – gonzamax

回答

0

我對這個工作的代碼,你可以嘗試,如果它的工作原理,

listView = (ListView) findViewById(R.id.sendToList); 
     editText = (EditText) findViewById(R.id.searchList); 
     adapter = new CustomListViewAdapter(this, 
       R.layout.list_row, rowItems); 
     listView.setAdapter(adapter); 
     listView.setTextFilterEnabled(true); 

     editText.addTextChangedListener(new TextWatcher() { 

      public void onTextChanged(CharSequence arg0, int arg1, int arg2, 
        int arg3) { 

      } 

      public void beforeTextChanged(CharSequence arg0, int arg1, 
        int arg2, int arg3) { 

      } 

      public void afterTextChanged(Editable arg0) { 
       sendToList.this.adapter.getFilter().filter(arg0); 

      } 
     }); 
+0

我的挑戰是使代碼適應我的arrayDapater simpleCursorAdapter。你能幫我嗎? – gonzamax

+0

對不起,但你在談論什麼ArrayAdapter。代碼是什麼,你使用它? –