我需要从android
中的sqlite数据库中获取所选微调器值的idpublic void onItemSelected(AdapterView<?> parent, View view, int position,long id) {
String label = parent.getItemAtPosition(position).toString();
Log.d("label:", label);
}
我需要返回所选值的id的id
答案 0 :(得分:0)
试试此代码
int getId(String label)
{
SqliteDatabase db=this.getWritableDatabase();
Cursor cur=db.rawQuery("SELECT ID FROM TABLENAME WHERE NAME=\""+label+"\"");
cur.moveToFirst();
String ID=cur.getString(0);
return Integer.valueOf(ID);
}
答案 1 :(得分:0)
在您的MAin活动中,请为您的微调器
尝试此代码 spin_category.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3)
{
category_id=arg2;
int my_id=db.getID(category_id);
}
public void onNothingSelected(AdapterView<?> arg0)
{
}
});
在您的数据库类中编写此函数
public int getID(int id)
{
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, new String[] { KEY_ID, KEY_NAME}, KEY_ID+"=?",
new String[] { String.valueOf(id) }, null);
if(cursor!=null)
if (cursor.getCount() > 0)
{
cursor.moveToFirst();
}
int s = cursor.getInt(cursor.getColumnIndex(KEY_ID));
cursor.close();
db.close();
return s;
}
第一个函数将在spinner中获取所选项的id,并将其传递给数据库方法,第二个函数将搜索该id并返回该id的id。因为我对你的要求不是很清楚。如果您想以不同的方式进行评论,请发表评论。
答案 2 :(得分:0)
如果你的适配器是SimpleCursorAdapter,你已经拥有参数'id'提供的选定id的id。如果它是自定义适配器,请覆盖适配器的getItemId()方法以返回所选行的正确ID。
你真的不需要这样做:
String label = parent.getItemAtPosition(position).toString();
...因为参数'view'已经在父级中提供了所选项目,即你可以这样做:
String label = view.toString();
答案 3 :(得分:0)
public void onItemSelected(AdapterView<?> parent, View view, int position,long id) {
String label = parent.getItemAtPosition(position).toString();
Log.d("label:", label);
}
spinner_referrer_to_type.getSelectedItem().toString();