2012-08-12 85 views
0

我有從SQLite數據庫填充的微調器。但是我想從字符串數組來填充它:使用SQLite從字符串數組填充微調器

<string-array name="category"> 
    <item name="accommodation">Accommodation</item> 
    <item name="automobile">Automobile</item> 
    <item name="credit cards">Credit Cards</item> 
    <item name="donations">Donations</item> 
    <item name="entertainment">Entertainment</item> 
    <item name="food">Food</item> 
</string-array> 

其中name我從SQLite表走。 我有方法從數據庫中檢索name

public Cursor getAllCategory() { 
    mDB = dbHelper.getReadableDatabase(); 
    return mDB.query(CATEGORY_TABLE_NAME, null, null, null, null, null,null); 
} 

而在我的節目,我用這些名字:

// spinner 
     catSpinner = (Spinner) view.findViewById(R.id.category_spinner); 
     cursor = adapter.getAllCategory(); 
     String[] from = new String[] { DataAdapter.CATEGORY_COL_NAME }; 
     int[] to = new int[] { android.R.id.text1 }; 
     SimpleCursorAdapter catAdapter = new SimpleCursorAdapter(
       getActivity(), 
       android.R.layout.simple_spinner_dropdown_item, cursor, 
       from, to, 0); 
     catAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     catSpinner.setAdapter(catAdapter); 

但我想使用來自數據庫的名稱,但價值,應對這些名字(string.xml)使用從string-array

我該怎麼做?我想這樣做是爲了避免在更改本地化時更改數據庫。

+0

從只有字符串數組填充微調不sqlite – 2012-08-12 09:07:59

回答

0

試試這個。要獲取數據到微調,你必須使用下面給出的SimpleCursorAdapter

// This is your method of database class which returns cursor 
Public Cursor getAllNames(){ 
    return mDb.query(table_name, new String[]{KEY_ROWID, KEY_TITLE}, null, null, null, null, null); 
} 

並使用此方法在微調獲取項目(即名稱)。

Cursor c = mDbHelper.getAllNames(); 
startManagingCursor(c); 

    String[] from = new String[]{your_name}; 
// create an array of the name which you want to bind our data to 
    int[] to = new int[]{android.R.id.text1}; 

    // This is your simple cursor adapter 
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c, from, to); 
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

    Spinner s = (Spinner) findViewById(R.id.unique_id); // This is your spinner 
    s.setAdapter(adapter); // Set it to the adapter 

希望這會有所幫助。 :)

+0

我知道如何從字符串資源填充微調。但我如何將它與sqlite連接?它有可能與否? – 2012-08-13 08:27:23

+0

我有更改我的帖子請通過它。 – Akshay 2012-08-13 09:13:44

+0

我也編輯我的文章。也許現在更清楚。有可能做我想做的事嗎?謝謝 – 2012-08-14 11:20:45