2017-03-08 83 views
1
public class MainActivity extends AppCompatActivity { 

    Cursor c; 
    Button b1; 
    EditText e1,e2,e3; 
    Spinner s1,s2; 
    SQLiteDatabase db; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Open_Helper helper=new Open_Helper(getApplicationContext()); 
     db=helper.getWritableDatabase(); 
     c=db.rawQuery("select mid as _id,* from menu",null); 
     s1= (Spinner) findViewById(R.id.spinner3); 
     SimpleCursorAdapter sr=new SimpleCursorAdapter(getApplicationContext(),R.layout.activity_main,c,new String[]{"_id","mname"},new int[]{R.id.spinner3,R.id.spinner4},0); 
     sr.setDropDownViewResource(R.id.spinner3); 
     s1.setAdapter(sr); 

    } 
} 

錯誤是微調值給出錯誤

java.lang.IllegalStateException: android.widget.Spinner is not a view 
    that can be bounds by this SimpleCursorAdapter 
+0

你看了(HTTP [使用SimpleCursorAdapter有微調?]://計算器。 com/a/26515766/4391450) – AxelH

回答

3

您設置Spinner id來setDropDownViewResource。但它需要一個layout ID

sr.setDropDownViewResource(R.id.spinner3); 

用這個代替:

sr.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

全碼:

String[] columns = new String[] { "_id","mname"}; 
int[] to = new int[] { R.id.spinner3,R.id.spinner4}; 

SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c , columns, to); 
mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
Spinner spinner = (Spinner) findViewById(R.id.spinner_id); 
spinner.setAdapter(mAdapter); 
+0

我嘗試設置佈局代替id,但它仍然無法正常工作。 –

+0

檢查編輯... – rafsanahmad007