从SQLite数据库Android填充微调器

crv*_*crv 7 database sqlite android spinner

我正在尝试创建一个由SQLite表填充的动态下拉列表.我有一个Cursor对象,我可以从中提取所需的数据.我已经能够使用下面的代码将值加载到下拉列表中:

Spinner s = (Spinner) findViewById(R.id.spinner);
    ArrayAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    s.setAdapter(adapter);

    try{
        Cursor cursor = getAccounts();
        int accountnameIndex = cursor.getColumnIndexOrThrow(ACCOUNT_NAME);
        if(cursor.moveToFirst()){
            do{
                adapter.add(cursor.getString(accountnameIndex));
            } while(cursor.moveToNext());
        }
    } finally {
        MintLink.close();
    }
Run Code Online (Sandbox Code Playgroud)

我的问题是我需要从下拉列表中选择一个也包含所选项目的RowID.我需要能够选择一个项目并且可以访问后端中该项目的值.例如,想一想HTML中的下拉菜单.每个下拉选项都有自己的隐藏值.我需要隐藏此值才能让我知道他们选择了哪个ID.

Min*_*ker 15

这是一个老问题,但是我在找出这个问题时找到的第一个问题.这里有一个完整的来源的详细解释,可能削减一些腿部工作.

答案确实是使用SimpleCursorAdapter处理字符串列表,但也对匹配的ID字段进行特殊处理,该字段在选择行时返回.完成这项工作的关键是要知道以下两个模糊的信息:

1)创建光标时,请确保查询返回标题为"_id"的字段.此字段无需显示在任何位置,但在选择列表项时它的值将被传回.

2)创建SimpleCursorAdapter时,需要提供将放置行文本的TextView布局ID.如果使用android提供的布局android.R.layout.simple_spinner_item,则需要使用的文本ID是android.R.id.text1.

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        ></Spinner>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

活动代码:

public class TesterActivity extends Activity {
public Context mContext;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // just for this example:
    // create database table with an id field and a text field and add some data
    class MyDBHelper extends SQLiteOpenHelper {
        public MyDBHelper(Context context) {
            super(context, "someDB", null, 2);
        }
        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("CREATE TABLE someTable (someIDF INTEGER, someTextF TEXT)");
        }
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS someTable");
            onCreate(db);
            db.execSQL("INSERT INTO someTable (someIDF, someTextF) VALUES (54, 'Some text')");
            db.execSQL("INSERT INTO someTable (someIDF, someTextF) VALUES (99, 'Some more text')");
            db.execSQL("INSERT INTO someTable (someIDF, someTextF) VALUES (173, 'Even more text')");
        }
    }
    SQLiteDatabase db = new MyDBHelper(this).getWritableDatabase();

    // get a cursor from the database with an "_id" field
    Cursor c = db.rawQuery("SELECT someIDF AS _id, someTextF FROM someTable", null);

    // make an adapter from the cursor
    String[] from = new String[] {"someTextF"};
    int[] to = new int[] {android.R.id.text1};
    SimpleCursorAdapter sca = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c, from, to);

    // set layout for activated adapter
    sca.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

    // get xml file spinner and set adapter 
    Spinner spin = (Spinner) this.findViewById(R.id.spinner1);
    spin.setAdapter(sca);

    // set spinner listener to display the selected item id
    mContext = this;
    spin.setOnItemSelectedListener(new OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id){
            Toast.makeText(mContext, "Selected ID=" + id, Toast.LENGTH_LONG).show();
        }
        public void onNothingSelected(AdapterView<?> parent) {}
        });
    }
}
Run Code Online (Sandbox Code Playgroud)


Com*_*are 5

尝试使用a SimpleCursorAdapter而不是手动将所有数据复制到ArrayAdapter.

  • 虽然这是简短的答案.实现SimpleCursorAdapter与Spinner一起使用的细节是什么?我是否需要抽象自己的ViewBinder,或者我可以将SimpleCursorAdapter直接与Spinner一起使用. (3认同)