Android Spinner:根据第一个Spinner过滤器填充第二个和第三个Spinner

时间:2019-10-20 14:26:49

标签: sqlite android-sqlite

相同的问题标题可用,但要求不同。

应从数据库中加载第一个微调器选择,而第二个微调器和第三个微调器应基于一个第一个微调器选择进行更改。

当前

Spinner1 Display all records-Spinner2&Spinner3 display based on spinner1

正如我所看到的,我无法找到sql查询中的错误。

问题:

  1. Spinner1应该显示忽略重复,而不是两个P1,它应该显示为P1,P2,N1。
  2. Spinner2应该显示与P1相关的所有记录,而它仅显示一个记录

要求:

第二个旋转器和第三个旋转器应基于第一个旋转器填充数据,而挑战是第二个旋转器应显示与spinner1匹配的所有数据。

布局:SpinnerEx4Activity

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical">

  <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="8dip" android:text="@string/lblAcc" />

  <Spinner android:id="@+id/spinner4" android:layout_width="138dp" android:layout_height="wrap_content" android:drawSelectorOnTop="true" />

  <Spinner android:id="@+id/spinner1" android:layout_width="368dp" android:layout_height="wrap_content" android:drawSelectorOnTop="true" android:layout_toRightOf="@+id/spinner4" android:layout_alignBaseline="@+id/spinner4" />

  <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="8dip" android:text="@string/lblSubAcc" />

  <Spinner android:id="@+id/spinner2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10dip" android:layout_marginLeft="8dip" android:layout_marginRight="8dip" />

  <TextView android:id="@+id/textView2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Visit Day" />

  <CheckBox android:id="@+id/checkBox2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Sunday" />

  <CheckBox android:id="@+id/checkBox3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Monday" />

  <CheckBox android:id="@+id/checkBox4" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Tuesday" />

  <CheckBox android:id="@+id/checkBox5" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Wednesday" />

  <CheckBox android:id="@+id/checkBox6" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Thursday" />

  <CheckBox android:id="@+id/checkBox" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Saturday" />

  <CheckBox android:id="@+id/checkBox7" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Outlet is Closed" />
  <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal">

    <TextView android:id="@+id/textView6" android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollHorizontally="false" android:text="Total Outlet" android:textSize="10dp" android:textStyle="bold" android:layout_weight="1" />

    <TextView android:id="@+id/textView7" android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollHorizontally="false" android:text="T Outlet" android:textSize="19dp" android:textStyle="bold" android:layout_weight="1" />
    <TextView android:id="@+id/textView9" android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollHorizontally="false" android:text="Completed Outlet" android:textSize="10dp" android:textStyle="bold" android:layout_weight="1"
    />
    <TextView android:id="@+id/textView8" android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollHorizontally="false" android:text="C.Outlet" android:textSize="19dp" android:textStyle="bold" android:layout_weight="1" />
  </LinearLayout>

  <Spinner android:id="@+id/spinner3" android:layout_width="match_parent" android:layout_height="wrap_content" />

  <EditText android:id="@+id/input_label" android:layout_width="match_parent" android:layout_height="0dp" android:ems="10" android:inputType="textPersonName" />

  <Button android:id="@+id/btn_add" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Save" />
  <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal">

    <Button android:id="@+id/btnexport" android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollHorizontally="false" android:text="Download" android:textSize="15dp" android:textStyle="bold" android:layout_weight="1" />

    <Button android:id="@+id/btn_send_mail" android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollHorizontally="false" android:text="Send" android:textSize="15dp" android:textStyle="bold" android:layout_weight="1" />

  </LinearLayout>

  <Button android:id="@+id/btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollHorizontally="false" android:text="Import" android:textSize="15dp" android:textStyle="bold" android:layout_weight="1" />

</LinearLayout>

MainActivity.Java

public class SpinnerEx4Activity extends AppCompatActivity {

    Spinner s1,s2,s3,s4;
    Button btnAdd;
    Button send;
    Button ok;
    Button btn_send_mail;
    private Button btnexport;
    EditText inputLabel;
    TextView tex,tex1;
    DatabaseHandler dbhndlr;

    Cursor spinner1csr, spinner2csr, spinner3csr;
    SimpleCursorAdapter sca, sca2,sca3;
    long spinner1_selected = 0;
    long spinner4_selected = 0;
    long spinner3_selected = 0;
    CheckBox ck1,ck2,ck3,ck4,ck5,ck6,ck7,ck8;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spinner_ex4);

        s1 = (Spinner)findViewById(R.id.spinner1);
        s2 = (Spinner)findViewById(R.id.spinner2);
        s3 = (Spinner)findViewById(R.id.spinner3);
        s4 = (Spinner)findViewById(R.id.spinner4);
        btnAdd = (Button) findViewById(R.id.btn_add);
        inputLabel = (EditText) findViewById(R.id.input_label);
        dbhndlr = new DatabaseHandler(this);
        ck1=(CheckBox) findViewById(R.id.checkBox);
        ck2=(CheckBox)findViewById(R.id.checkBox2);
        ck3=(CheckBox)findViewById(R.id.checkBox3);
        ck4=(CheckBox)findViewById(R.id.checkBox4);
        ck6=(CheckBox) findViewById(R.id.checkBox5);
        ck7=(CheckBox)findViewById(R.id.checkBox6);
        ck8=(CheckBox)findViewById(R.id.checkBox7);
        final Button btnexport = (Button) findViewById(R.id.btnexport);
        btn_send_mail = (Button) findViewById(R.id.btn_send_mail);
        ok = (Button) findViewById(R.id.btn);
        loadSpinnerData();
        TextView tex = (TextView) findViewById(R.id.textView7);
        TextView tex1 = (TextView) findViewById(R.id.textView8);
        if (DatabaseUtils.queryNumEntries(dbhndlr.getWritableDatabase(),DatabaseHandler.TABLE_LABELS) < 1) {
            dbhndlr.insertlabel("1" ,"P1","Henry","9001234");
            dbhndlr.insertlabel("2","P1","Malik","9004567");
            dbhndlr.insertlabel("3","P2","ermarket","900356");
            dbhndlr.insertlabel("4","N1","Veli","9003456");
        }
        if (DatabaseUtils.queryNumEntries(dbhndlr.getWritableDatabase(),DatabaseHandler.TABLE_LABELS1) < 2) {
            dbhndlr.insertlabel12("14","0001 Daily");
            dbhndlr.insertlabel12("21","0007 Weekly");
            dbhndlr.insertlabel12("34","0014 Days");
            dbhndlr.insertlabel12("44","0021 Days");
            dbhndlr.insertlabel12("54","0028 Days");

        }
        spinner1csr = dbhndlr.getAllLabelsAsCursor();

        s1.setAdapter(sca);

        sca3 = new SimpleCursorAdapter(this,
                android.R.layout.simple_list_item_1,spinner1csr,
                new String[]{DatabaseHandler.KEY_ID1},
                new int[]{android.R.id.text1},
                0
        );


        s4.setAdapter(sca3);

            class SendMail extends AsyncTask <String, Integer, Void> {

                private ProgressDialog progressDialog;

                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
                    progressDialog = ProgressDialog.show(SpinnerEx4Activity.this, "Please wait", "Sending mail", true, false);
                }

                @Override
                protected void onPostExecute(Void aVoid) {
                    super.onPostExecute(aVoid);
                    progressDialog.dismiss();
                }

                protected Void doInBackground(String... params) {
                    Mail m = new Mail("d.com", "t3e");


                    String[] toArr = {"fdm.bh"};
                    m.setTo(toArr);
                    m.setFrom("owMobile");
                    m.setSubject("This is emaster Data Application");
                    m.setBody("Find attached excoute,Thank you.");
                    try {


                        m.addAttachment(Environment.getExternalStorageDirectory()+"/android/data/com.example.ok.myapplication/files/w.csv");

                        if(m.send()) {
                            Toast.makeText(SpinnerEx4Activity.this, "Email was sent successfully.", Toast.LENGTH_LONG).show();
                        } else {
                            Toast.makeText(SpinnerEx4Activity.this, "Email was not sent.", Toast.LENGTH_LONG).show();
                        }
                    } catch(Exception e) {
                        Log.e("MailApp", "Could not send email", e);
                    }
                    return null;
                }

            }
        });



///////////////////////////////

        s4.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id1) {

                spinner4_selected = id1;
                spinner1csr = dbhndlr.getByRowid(spinner4_selected);
                spinner2csr = dbhndlr.getByRowid(spinner4_selected);
                sca.swapCursor(spinner1csr);
                sca2.swapCursor(spinner2csr);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        sca = new SimpleCursorAdapter(this,
                android.R.layout.simple_list_item_1,
                spinner1csr,
                new String[]{DatabaseHandler.KEY_NAME},
                new int[]{android.R.id.text1},
                0
        );


        sca2 = new SimpleCursorAdapter(this,
                android.R.layout.simple_list_item_1,
                spinner1csr,
                new String[]{DatabaseHandler.KEY_ID},
                new int[]{android.R.id.text1},
                0
        );

        s1.setAdapter(sca);
        s2.setAdapter(sca2);


    private void loadSpinnerData() {

        DatabaseHandler db = new DatabaseHandler(getApplicationContext());

        List<String> lables = db.getAllLabels();


        ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, lables);


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

        /
        s3.setAdapter(dataAdapter1);
    }
    @Override
    public void onDestroy() {
        spinner1csr.close();
        spinner2csr.close();
        spinner3csr.close();
        super.onDestroy();

    }



    protected Void doInBackground(String... params) {
        Mail m = new Mail("d.com", "4@");


        String[] toArr = {"d.gw"};
        m.setTo(toArr);
        m.setFrom("Mata");
        m.setSubject("This is tion");
        m.setBody("Find ute,Thank you.");

        try {


            m.addAttachment(Environment.getExternalStorageDirectory() + "/Android/data/com.example.ok.myapplication/files/ce.csv");

            if (m.send()) {
                Toast.makeText(SpinnerEx4Activity.this, "Email was sent successfully.", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(SpinnerEx4Activity.this, "Email was not sent.", Toast.LENGTH_LONG).show();
            }
        } catch (Exception e) {
            Log.e("MailApp", "Could not send email", e);
        }
        return null;
    }
}

数据库

    public class DatabaseHandler extends SQLiteOpenHelper {
        // Database Version
        public static final int DATABASE_VERSION = 1;

        public static final String DATABASE_NAME = "spinnerExample";
        private static String DB_PATH = "/data/data/com.example.ok.myapplication/databases/";
        private final Context myContext;
        private SQLiteDatabase myDataBase;
        // Database Name
        // Labels table name
        public static final String TABLE_LABELS = "labels"; //<<<< Made public
        public static final String TABLE_LABELS1= "labels1";
        public static final String TABLE_LABELS2= "labels2";
        // Labels Table Columns names
        public static final String KEY_ID4 = "input_label";
        public static final String KEY_ID12 = "id2";           //<<<< Made public
        public static final String KEY_ID = "id";
        public static final String KEY_99 = "sno";           //<<<< Made public//<<<< Made public
        public static final String KEY_NAME = "name";       //<<<< made public
        public static final String KEY_ID1 = "id1";           //<<<< Made public
        public static final String KEY_NAME1 = "name1";
        public static final String KEY_1 = "number";           //<<<< Made public
        public static final String KEY_2 = "outletname";       //<<<< made public
        public static final String KEY_3 = "sunday";           //<<<< Made public
        public static final String KEY_4 = "monday";
        public static final String KEY_5 = "tuesday";
        public static final String KEY_6 = "wednesday";
        public static final String KEY_7 = "thursday";
        public static final String KEY_8 = "saturday";
        public static final String KEY_9 = "closed";
        public static final String KEY_10 = "calling";
        public static final String KEY_11 = "id3";
        public DatabaseHandler(Context context) {

            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            myDataBase = this.getWritableDatabase();
            this.myContext = context;
            Log.e("Path 1", DB_PATH);
        }

        // Creating Tables


        public void openDataBase() throws SQLException {
            String myPath = DB_PATH + DATABASE_NAME;
            myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);

        }
        @Override
        public void onCreate(SQLiteDatabase db) {
            boolean dbExist = checkDataBase();
            // Category table create query
            String CREATE_CATEGORIES_TABLE = "CREATE TABLE " + TABLE_LABELS + "("+ KEY_99 + " INTEGER,"
                    + KEY_ID1 + " TEXT," + KEY_ID + " TEXT," + KEY_NAME + " TEXT)";
            String CREATE_CATEGORIES_TABLE1 = "CREATE TABLE " + TABLE_LABELS1 + "("
                    + KEY_ID1+ " TEXT," + KEY_NAME1+ " TEXT)";
            String CREATE_CATEGORIES_TABLE2 = "CREATE TABLE " + TABLE_LABELS2 + "("
                    + KEY_11+ " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_1+ " TEXT," + KEY_2+ " TEXT," + KEY_3+ " INTEGER," + KEY_4+ " INTEGER,"+ KEY_5+ " INTEGER," + KEY_6+ " INTEGER," + KEY_7+ " INTEGER," + KEY_8+ " INTEGER," + KEY_9+ " INTEGER," + KEY_10+ " TEXT)";
            db.execSQL(CREATE_CATEGORIES_TABLE);
            db.execSQL(CREATE_CATEGORIES_TABLE1);
            db.execSQL(CREATE_CATEGORIES_TABLE2);
        }
        private boolean checkDataBase() {
            SQLiteDatabase checkDB = null;
            try {
                String myPath = DB_PATH + DATABASE_NAME;
                checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
            } catch (SQLiteException e) {
            }
            if (checkDB != null) {
                checkDB.close();
            }
            return checkDB != null ? true : false;
        }
        private void copyDataBase() throws IOException {
            InputStream myInput = myContext.getAssets().open(DATABASE_NAME);
            String outFileName = DB_PATH + DATABASE_NAME;
            OutputStream myOutput = new FileOutputStream(outFileName);
            byte[] buffer = new byte[10];
            int length;
            while ((length = myInput.read(buffer)) > 0) {
                myOutput.write(buffer, 0, length);
            }
            myOutput.flush();
            myOutput.close();
            myInput.close();

        }
        // Upgrading database
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // Drop older table if existed
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_LABELS);
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_LABELS1);
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_LABELS2);
            // Create tables again
            onCreate(db);
        }

        // Added for adding new data
        public void insertlabel(String text,String id1,String id, String label) {
            SQLiteDatabase db = this.getWritableDatabase();
            ContentValues cv = new ContentValues();
            cv.put(KEY_99,text);
            cv.put(KEY_ID1,id1);
            cv.put(KEY_ID,id);
            cv.put(KEY_NAME,label);
            db.insert(TABLE_LABELS,null,cv);
            db.close();
        }
        public void insertlabel12(String id1, String label1) {
            SQLiteDatabase db = this.getWritableDatabase();
            ContentValues cv1 = new ContentValues();
            cv1.put(KEY_ID1,id1);
            cv1.put(KEY_NAME1,label1);
            db.insert(TABLE_LABELS1,null,cv1);
            db.close();
        }
        /**
         * Inserting new lable into lables table
         * */
            public void insertLabel(String message1, String message2,String message3,String message4,String message5,String message6,String message7,String message8,String message9,String message10){
                SQLiteDatabase db = this.getWritableDatabase();

                ContentValues values = new ContentValues();

                values.put(KEY_1, message1);
                values.put(KEY_2, message2);
                values.put(KEY_10,message10);
                values.put(KEY_4,message4);
                values.put(KEY_5,message5);
                values.put(KEY_6,message6);
                values.put(KEY_7,message7);
                values.put(KEY_3,message3);
                values.put(KEY_9,message9);
                values.put(KEY_8,message8);


                // Inserting Row
                db.insert(TABLE_LABELS2, null, values);
                db.close(); // Closing database connection
            }

        public void insertLabel1(String label){
            SQLiteDatabase db = this.getWritableDatabase();

            ContentValues values = new ContentValues();
            values.put(KEY_NAME1, label);

            // Inserting Row
            db.insert(TABLE_LABELS1, null, values);
            db.close(); // Closing database connection
        }
        public void insertLabel2(String label){
            SQLiteDatabase db = this.getWritableDatabase();


            ContentValues values = new ContentValues();
            values.put(KEY_NAME1, label);
            values.put(KEY_10, label);
            values.put(KEY_ID, label);

            db.insert(TABLE_LABELS2, null, values);
            db.close(); // Closing database connection
        }

        public List<String> getAllLabels(){
            List<String> labels = new ArrayList<String>();

            // Select All Query
            String selectQuery = "SELECT  * FROM " + TABLE_LABELS1;

            SQLiteDatabase db = this.getReadableDatabase();
            Cursor cursor = db.rawQuery(selectQuery, null);

            // looping through all rows and adding to list
            if (cursor.moveToFirst()) {
                do {
                    labels.add(cursor.getString(1));
                } while (cursor.moveToNext());
            }

            // closing connection
            cursor.close();
            db.close();

            // returning lables
            return labels;
        }
        public List<String> andrew(){
            List<String> labels = new ArrayList<String>();

            // Select All Query
            String selectQuery = "SELECT  * FROM " + TABLE_LABELS;

            SQLiteDatabase db = this.getReadableDatabase();
            Cursor cursor = db.rawQuery(selectQuery, null);

            // looping through all rows and adding to list
            if (cursor.moveToFirst()) {
                do {
                    labels.add(cursor.getString(1));
                } while (cursor.moveToNext());
            }

            // closing connection
            cursor.close();
            db.close();

            // returning lables
            return labels;
        }
        public Cursor getAllEntries(){
            String selectQuery = "SELECT  count (*) FROM " + TABLE_LABELS;
            SQLiteDatabase db = this.getReadableDatabase();

            Cursor cur = db.rawQuery(selectQuery, null);
            return  cur;

        }
        public Cursor getAllEntries1(){
            String selectQuery = "SELECT  count (*) FROM " + TABLE_LABELS2;
            SQLiteDatabase db = this.getReadableDatabase();

            Cursor cur1 = db.rawQuery(selectQuery, null);
            return  cur1;

        }

        public List<String> getAllLabels1(){
            List<String> labels = new ArrayList<String>();

            // Select All Query
            String selectQuery = "SELECT  count (*) FROM " + TABLE_LABELS;

            SQLiteDatabase db = this.getReadableDatabase();
            Cursor cursor1 = db.rawQuery(selectQuery, null);
            final ArrayList<String> row1 = new ArrayList<String>();
            // looping through all rows and adding to list
            if (cursor1.moveToFirst()) {
                do {
                    labels.add(cursor1.getString(1));
                } while (cursor1.moveToNext());
            }

            // closing connection
            cursor1.close();
            db.close();

            // returning lables
            return labels;
        }




        // Added to get Cursor for Simple CursorAdapter
        public Cursor getAllLabelsAsCursor() {
            String[] columns = new String[]{"rowid AS _id, *"}; // Need _id column for SimpleCursorAdapter
            return this.getWritableDatabase().query(TABLE_LABELS,columns,null,null,null,null,null);
        }

        public Cursor getAllLabelsExceptedSelected(long selected) {
            String[] columns = new String[]{"rowid AS _id, *"};
            String whereclause = "rowid <> ?";
            String[] whereargs = new String[]{String.valueOf(selected)};
            return this.getWritableDatabase().query(TABLE_LABELS,
                    columns,
                    whereclause,
                    whereargs,
                    null,
                    null,
                    null
            );
        }

        public Cursor getByRowid(long id) {
            String[] columns = new String[]{"rowid AS _id, *"};
            return this.getWritableDatabase().query(TABLE_LABELS, columns, "rowid=?", new String[]{String.valueOf(id)},
                    null, null, null

            );
        }
    }

1 个答案:

答案 0 :(得分:2)

以下是基于问题中某些代码的示例,该示例具有3个微调器,第二个列表值是根据spinner1选择的内容确定的。同样,由spinner2选择的内容确定的第三个列表值。

简而言之,每个微调器都有一个 manageSprinner?方法(?分别为1、2和3)。

manageSpinner?方法检索相应的数据。

  • 对于2和3,根据传递的值确定各自的数据,该值是层次结构中位于其上方的微调器中的选定项(即,对于spinner2,则是spinner1中的选定项,对于spinner3,则是spinner2中的选定项) 。

如果未实例化适配器,它们是 managedSpinner?方法实例化,并且对于Spinners 1和2,添加了onIstemSelected侦听器,该侦听器调用下面的Spinner的 managerSpinner?方法在层次结构中。

如果相应的适配器已被实例化,则它将交换光标以刷新微调器显示的数据。

  • 注意,因为 manageSpinner1 调用 managerSpinner2 ,而 manageSpinner2 调用 manageSpinner3 。只需调用 manageSpinner1 。总是有一个被选中的项目,因此从一开始就调用onItemSelected方法。

活动 activity_main.xml 的布局:-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <Spinner
        android:id="@+id/spinner2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <Spinner
        android:id="@+id/spinner3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</LinearLayout>

数据库助手 Databasehandler.java

public class DatabaseHandler extends SQLiteOpenHelper {
    // Database Version
    public static final int DATABASE_VERSION = 1;

    public static final String DATABASE_NAME = "spinnerExample";
    private final Context myContext;
    private SQLiteDatabase myDataBase;
    // Database Name
    // Labels table name
    public static final String TABLE_LABELS = "labels"; //<<<< Made public
    public static final String TABLE_LABELS1= "labels1";
    public static final String TABLE_LABELS2= "labels2";
    // Labels Table Columns names
    public static final String KEY_ID4 = "input_label";
    public static final String KEY_ID12 = "id2";           //<<<< Made public
    public static final String KEY_ID = "id";
    public static final String KEY_99 = "sno";           //<<<< Made public//<<<< Made public
    public static final String KEY_NAME = "name";       //<<<< made public
    public static final String KEY_ID1 = "id1";           //<<<< Made public
    public static final String KEY_NAME1 = "name1";
    public static final String KEY_1 = "number";           //<<<< Made public
    public static final String KEY_2 = "outletname";       //<<<< made public
    public static final String KEY_3 = "sunday";           //<<<< Made public
    public static final String KEY_4 = "monday";
    public static final String KEY_5 = "tuesday";
    public static final String KEY_6 = "wednesday";
    public static final String KEY_7 = "thursday";
    public static final String KEY_8 = "saturday";
    public static final String KEY_9 = "closed";
    public static final String KEY_10 = "calling";
    public static final String KEY_11 = "id3";
    public DatabaseHandler(Context context) {

        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        myDataBase = this.getWritableDatabase();
        this.myContext = context;
    }

    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        //boolean dbExist = checkDataBase();
        // Category table create query
        String CREATE_CATEGORIES_TABLE = "CREATE TABLE " + TABLE_LABELS + "("+ KEY_99 + " INTEGER,"
                + KEY_ID1 + " TEXT," + KEY_ID + " TEXT," + KEY_NAME + " TEXT)";
        String CREATE_CATEGORIES_TABLE1 = "CREATE TABLE " + TABLE_LABELS1 + "("
                + KEY_ID1+ " TEXT," + KEY_NAME1+ " TEXT)";
        String CREATE_CATEGORIES_TABLE2 = "CREATE TABLE " + TABLE_LABELS2 + "("
                + KEY_11+ " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_1+ " TEXT," + KEY_2+ " TEXT," + KEY_3+ " INTEGER," + KEY_4+ " INTEGER,"+ KEY_5+ " INTEGER," + KEY_6+ " INTEGER," + KEY_7+ " INTEGER," + KEY_8+ " INTEGER," + KEY_9+ " INTEGER," + KEY_10+ " TEXT)";
        db.execSQL(CREATE_CATEGORIES_TABLE);
        db.execSQL(CREATE_CATEGORIES_TABLE1);
        db.execSQL(CREATE_CATEGORIES_TABLE2);
    }
    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_LABELS);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_LABELS1);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_LABELS2);
        // Create tables again
        onCreate(db);
    }

    /**
     * Inserting new lable into lables table
     * */
    public void insertLabel(String message1, String message2,String message3,String message4,String message5,String message6,String message7,String message8,String message9,String message10){
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_1, message1);
        values.put(KEY_2, message2);
        values.put(KEY_10,message10);
        values.put(KEY_4,message4);
        values.put(KEY_5,message5);
        values.put(KEY_6,message6);
        values.put(KEY_7,message7);
        values.put(KEY_3,message3);
        values.put(KEY_9,message9);
        values.put(KEY_8,message8);
        // Inserting Row
        db.insert(TABLE_LABELS2, null, values);
        //db.close(); // Closing database connection
    }

    public void insertLabel1(String label){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(KEY_NAME1, label);
        db.insert(TABLE_LABELS1, null, values);
        //db.close(); // Closing database connection
    }

    public void insertLabel2(String label){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(KEY_NAME, label);
        db.insert(TABLE_LABELS, null, values);
        db.close(); // Closing database connection
    }


    public Cursor getAllLabelsForSpinner1AsCursor() {
        String[] columns = new String[]{"rowid AS _id, *"}; // Need _id column for SimpleCursorAdapter
        return this.getWritableDatabase().query(TABLE_LABELS2,columns,null,null,null,null,null);
    }


    public Cursor getAllLabelsForSpinner2AsCursor(String keyFromSinner1) {
        String[] columns = new String[]{"rowid AS _id, *"}; // Need _id column for SimpleCursorAdapter
        return this.getWritableDatabase().query(
                TABLE_LABELS1,columns,
                DatabaseHandler.KEY_NAME1 + " LIKE ?",
                new String[]{keyFromSinner1+"%"},
                null,null,null
        );
    }

    public Cursor getAllLabelsForSpinner3AsCursor(String keyFromSpinner2) {
        String[] columns = new String[]{"rowid AS _id, *"}; // Need _id column for SimpleCursorAdapter
        return this.getWritableDatabase().query(
                TABLE_LABELS,columns,
                DatabaseHandler.KEY_NAME + " LIKE ?",
                new String[]{keyFromSpinner2 + "%"},
                null,null,null);
    }
}
  • 已删除了该示例不需要的代码

活动 MainActivity.java

public class MainActivity extends AppCompatActivity {

    DatabaseHandler mDH;
    Spinner mSpinner1,mSpinner2,mSpinner3;
    Cursor mSpinner1Csr,mSpinner2Csr,mSpinner3Csr;
    SimpleCursorAdapter mSpinner1Adapter,mSpinner2Adapter,mSpinner3Adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mSpinner1 = this.findViewById(R.id.spinner1);
        mSpinner2 = this.findViewById(R.id.spinner2);
        mSpinner3 = this.findViewById(R.id.spinner3);
        mDH = new DatabaseHandler(this);
        addSomeTestingData(); // ADD testing data if none
        manageSpinner1(); // Manages spinner1 not that spinner 1 invokes manage spinner2 and spinnr manages spinner3

    }

    private void addSomeTestingData() {
        if(DatabaseUtils.queryNumEntries(mDH.getWritableDatabase(),DatabaseHandler.TABLE_LABELS) > 0) return;

        // Data for LABELS2 table (spinner 1 (note 1st column listed in spinner))
        mDH.insertLabel("A1","A2","A3","A4","A5","A6","A7","A8","A9","A10");
        mDH.insertLabel("B1","B2","B3","B4","B5","B6","B7","B8","B9","B10");
        mDH.insertLabel("L1","L2","L3","L4","L5","L6","L7","L8","L9","L10");

        // Data for LABELS1 table (spinner 2)
        mDH.insertLabel1("A1EXTRA1");
        mDH.insertLabel1("A1EXTRA2");
        mDH.insertLabel1("B1EXTRA1");
        mDH.insertLabel1("B1EXTRA2");
        mDH.insertLabel1("L1EXTRA1");
        mDH.insertLabel1("L1EXTRA2");

        // Data for LABELS table (spinner 3)
        mDH.insertLabel2("A1EXTRA1MORE1");
        mDH.insertLabel2("A1EXTRA1MORE2");
        mDH.insertLabel2("A1EXTRA2MORE1");
        mDH.insertLabel2("A1EXTRA2MORE2");
        mDH.insertLabel2("B1EXTRA1MORE1");
        mDH.insertLabel2("B1EXTRA1MORE2");
        mDH.insertLabel2("B1EXTRA2MORE1");
        mDH.insertLabel2("B1EXTRA2MORE2");
        mDH.insertLabel2("L1EXTRA1MORE1");
        mDH.insertLabel2("L1EXTRA1MORE2");
        mDH.insertLabel2("L1EXTRA2MORE1");
        mDH.insertLabel2("L1EXTRA2MORE2");

    }

    private void manageSpinner1() {
        mSpinner1Csr = mDH.getAllLabelsForSpinner1AsCursor();
        if (mSpinner1Adapter == null) {
            mSpinner1Adapter = new SimpleCursorAdapter(
                    this,
                    android.R.layout.simple_list_item_1,
                    mSpinner1Csr,
                    new String[]{DatabaseHandler.KEY_1},
                    new int[]{android.R.id.text1},
                    0
            );
            mSpinner1.setAdapter(mSpinner1Adapter);
            mSpinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    manageSpinner2(mSpinner1Csr.getString(mSpinner1Csr.getColumnIndex(DatabaseHandler.KEY_1)));
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                }
            });
        } else {
            mSpinner1Adapter.swapCursor(mSpinner1Csr);
        }
    }
    private void manageSpinner2(String keyFromSpinner1) {
        mSpinner2Csr = mDH.getAllLabelsForSpinner2AsCursor(keyFromSpinner1);
        if (mSpinner2Adapter == null) {
            mSpinner2Adapter = new SimpleCursorAdapter(
                    this,
                    android.R.layout.simple_list_item_1,
                    mSpinner2Csr,
                    new String[]{DatabaseHandler.KEY_NAME1},
                    new int[]{android.R.id.text1},
                    0
            );
            mSpinner2.setAdapter(mSpinner2Adapter);
            mSpinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    manageSpinner3(mSpinner2Csr.getString(mSpinner2Csr.getColumnIndex(DatabaseHandler.KEY_NAME1)));
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });
        } else {
            mSpinner2Adapter.swapCursor(mSpinner2Csr);
        }
    }

    private void manageSpinner3(String keyForSpinner3) {
        mSpinner3Csr = mDH.getAllLabelsForSpinner3AsCursor(keyForSpinner3);
        if (mSpinner3Adapter == null) {
            mSpinner3Adapter = new SimpleCursorAdapter(
                    this,
                    android.R.layout.simple_list_item_1,
                    mSpinner3Csr,
                    new String[]{DatabaseHandler.KEY_NAME},
                    new int[]{android.R.id.text1},
                    0
            );
            mSpinner3.setAdapter(mSpinner3Adapter);
        } else {
            mSpinner3Adapter.swapCursor(mSpinner3Csr);
        }
    }
}

注意

以上内容仅是对该技术的演示,因此需要将该技术进行合并,因此代码必须作为合并的一部分进行修改