来自Database Gives错误的微调器值

时间:2017-03-08 08:16:16

标签: java android spinner simplecursoradapter

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

1 个答案:

答案 0 :(得分: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);