2012-08-12 61 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