2011-11-29 4 views
1

私は、テーブル 'employee'を検索して結果を返すアプリケーションを作っています。これを行うにはどうすればアレイアダプターを使用できますか?私はアンドロイドが初めてです。arrayadapterの使い方は?

public class SimpleSearch extends Activity { 
    /** Called when the activity is first created. */ 
    protected SQLiteDatabase db; 
    Intent intent = getIntent(); 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     // Get the intent, verify the action and get the query 
     Intent intent = getIntent(); 
     if (Intent.ACTION_SEARCH.equals(intent.getAction())) { 
      String query = intent.getStringExtra(SearchManager.QUERY); 
      doMySearch(query); 
     } 
    } 
    public List<String> doMySearch(String query) { 
     List<String> result = new ArrayList<String>(); 

     Cursor c = db.query(
       "employee", 
       new String[] { "_id" }, // The column you want as a result of the Query 
       "firstName like '%?%' OR lastName like '%?%' OR officePhone like '%?%'", // The where-Clause of the statement with placeholders (?) 
       new String[] { query, query, query }, // One String for every Placeholder-? in the where-Clause 
       ); 
     while (c.moveToNext()) { 
      result.add(c.getString(0)); 
     } 
     c.close(); 
     return result; 
    } 


} 
+0

代わりにSimpleCursorAdapterを使用する必要があります。 http://thinkandroid.wordpress.com/2010/01/09/simplecursoradapters-and-listviews/とhttp://groups.google.com/group/android-developers/browse_thread/thread/347c5df5a13fec58?pliを確認してください。 = 1 – Hiral

答えて

0

あなたはListActivityまたはListFragmentを使用し、CursorAdapterを使用してそれらを読み込む必要があります。(SimpleCursorAdapter)

CursorAdapterがあなたのdoMySearchMethodによって返されたリストから移入することができ、実装クラスです。

+0

これは何ですか? ます。public void doMySearch(文字列のクエリ){ \t \t \tカーソル= db.rawQuery( "は_idを選択、firstNameの、lastNameの、従業員FROMタイトルのfirstName || '' || lastNameのLIKE?"、 \t \t \t \t \t \t新しい文字列[] {"%" + query.getText()。toString()+ "%"}); \t \tアダプタ=新しいSimpleCursorAdapter( \t \t \t \tこの、 \t \t \t \t R.layout.emp_list_item、 \t \t \t \tカーソル、 \t \t \t \t新しいString [] { "firstNameの"、 "lastNameの" 、 "title"}、 \t \t \t \t新しいint [] {R.id.firstName、R.id.lastName、R.id.title}); \t \t setListAdapter(adapter); } } –

+0

はいいいです。 –

1

代わりにアレイアダプターを使用する必要はありません。代わりにSimpleCursorAdapterを使用してください。使用例が必要な場合はNotepad tutorialをご覧ください。

関連する問題