0

我有从 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

我怎么能做到这一点?我想这样做是为了避免在更改本地化时更改数据库。

4

1 回答 1

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

希望这可以帮助。:)

于 2012-08-12T09:16:20.657 回答