简体   繁体   中英

Spinner values from Database Gives error

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

    }
}

Error is

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

You are setting a Spinner id to setDropDownViewResource . though it Requires a layout ID

sr.setDropDownViewResource(R.id.spinner3);

use this instead:

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

Full code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM