Dynamic Spinner Using SQLite Database

2019-08-24 02:58发布

问题:

I've been able to successfully link two spinners to a database using a SimpleCursorAdapter. But, I need to make the second spinner selection dependant on the first spinner selection.

Here is how I linked the data:

    public class epa_estimates_button extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.epa_estimates);
    final Cursor cYear;
    final Cursor cMake;
    final Spinner mMakeSpinner;
    final Spinner mModelSpinner;
    cYear = (Cursor) DataBaseHelper.getEPADataYear();
    this.startManagingCursor(cYear);

    SimpleCursorAdapter scaYear = new SimpleCursorAdapter(this, R.layout.spinner_layout,cYear,new String[] {DataBaseHelper.EPA_COLUMN_ONE},new int[]{R.id.text1});
    scaYear.setDropDownViewResource(R.layout.spinner_dropdown);
    mYearSpinner = (Spinner) findViewById(R.id.yearSpinner);
    mYearSpinner.setAdapter(scaYear);

    cMake = (Cursor) DataBaseHelper.getEPADataMake();
    this.startManagingCursor(cMake);

    SimpleCursorAdapter scaMake = new SimpleCursorAdapter(this, R.layout.spinner_layout,cMake,new String[] {DataBaseHelper.EPA_COLUMN_TWO},new int[]{R.id.text1});
    scaMake.setDropDownViewResource(R.layout.spinner_dropdown);
    mMakeSpinner = (Spinner) findViewById(R.id.makeSpinner);
    mMakeSpinner.setAdapter(scaMake);
}}

Here is my DataBaseHelper

    public class DataBaseHelper extends SQLiteOpenHelper {
private static String DB_PATH = "/data/data/org.application.ocdmpg/databases/";
private static String DB_NAME = "ocd_mpg.mp3";
private final int DB_VERSION = 1;
private static SQLiteDatabase myDataBase; 
private final Context myContext;
private static final String EPA_TABLE_NAME = "epa_data";
private static final String EPA_COLUMN_ID = "_id";
public DataBaseHelper(Context context) {

    super(context, DB_NAME, null, 1);
    this.myContext = context;
}
    public static Cursor getEPADataYear() 
{
    return myDataBase.query(EPA_TABLE_NAME, //table name
            new String[] {EPA_COLUMN_ID, EPA_COLUMN_ONE}, //list of columns to return
            null, //filter declaring which rows to return; formatted as SQL WHERE clause
            null,
            EPA_COLUMN_ONE, //filter declaring how to group rows; formatted as SQL GROUP BY clause
            null, //filter declaring which row groups to include in cursor; formatted as SQL HAVING clause
            null); //how to order rows; formatted as SQL ORDER BY clause
}
public static Cursor getEPADataMake() 
{
    return myDataBase.query(EPA_TABLE_NAME, new String[] {
            EPA_COLUMN_ID,
            EPA_COLUMN_TWO,
            }, 
            null, 
            null, 
            EPA_COLUMN_TWO, 
            null, 
            null);
}}

I've found code for setOnItemSelectedListener and tried to add my code for linking the data to the spinner but the startManagingCursor method and SimpleCursorAdapter constructors give me an error as undefined. Should I use an ArrayAdapter to populate my spinners? Or is there a way to correct the code below?

        mYearSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
            // your code here
            cMake = (Cursor) DataBaseHelper.getEPADataMake();
            this.startManagingCursor(cMake);

            SimpleCursorAdapter scaMake = new SimpleCursorAdapter(this, R.layout.spinner_layout,cMake,new String[] {DataBaseHelper.EPA_COLUMN_TWO},new int[]{R.id.text1});
            scaMake.setDropDownViewResource(R.layout.spinner_dropdown);
            mMakeSpinner = (Spinner) findViewById(R.id.makeSpinner);
            mMakeSpinner.setAdapter(scaMake);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parentView) {
            // your code here
        }

        });

回答1:

Updated based on your edit and comments

Your call to this.startManagingCursor(cMake) needs to be changed to either startManagingCursor(cMake) or epa_estimates_button.this.startManagingCursor(cMake), since it's being made from within an inner class (an OnItemSelectedListener). It works without 'this' (the former case) because, well, basically, Java figures out which this you meant. And it works with the classname because that makes explicit what Java would have figured out for itself. But the unqualified this implies that you're referring to the inner class this, and that wouldn't work.

Very similarly, in your onItemSelectedListener, your call to startManagingCursor needs to be rewritten as:

SimpleCursorAdapter scaMake = new SimpleCursorAdapter(epa_estimates_button.this,
    R.layout.spinner_layout,cMake,new String[] {DataBaseHelper.EPA_COLUMN_TWO},
    new int[]{R.id.text1});

(I think your database queries are going to need work -- you don't seem to use any sort of WHERE clause to restrict the 'make' results to the appropriate year. But if you do get stuck on that, please post it as another question.)