2013-12-13 28 views

回答

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); 
} 
0

在主要活動試試這個代碼,您的微調

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; 
} 

的第一個函數將獲得所選項目的ID在微調,將它傳遞給數據庫的方法和二級功能將搜索ID,並返回該id ID。因爲我對你的要求不是很清楚。請評論,如果你想以不同的方式。

0

如果您的適配器是SimpleCursorAdapter,則您已經擁有由參數'id'提供的選定ID。如果它是自定義適配器,請覆蓋適配器的getItemId()方法以返回選定行的正確標識。

你並不真的需要做到這一點無論:

String label = parent.getItemAtPosition(position).toString(); 

...因爲參數「視圖」已經在父提供了選擇的項目,即你可以這樣做,而不是:

String label = view.toString(); 
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(); 
相關問題