我如何基于1个微调器从sqlite获取数据?

时间:2019-05-30 17:52:02

标签: android android-sqlite

我创建了一个应用程序,在该应用程序中,我想从第一个微调器中声明2个微调器,我想从SQLite数据库中获取用户名,在第二个微调器中,我希望根据选择的微调器一个数据来获取姓氏。在第一个微调器中,我能够获取用户名,但无法基于名称获取用户的姓氏(微调器1)。

我的微调代码

     private void loadSpinnerData() {
        List<String> lables = myDb.getAllLabels();
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                layout.simple_spinner_item, lables);
        dataAdapter           .setDropDownViewResource(layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);

//spinner 2 for last name
 List<String> lables1 = myDb.getAllLabels_one(name);//passinng name for serch in database 

        ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(this,
                layout.simple_spinner_item, lables1);

        dataAdapter1            .setDropDownViewResource(layout.simple_spinner_dropdown_item);


        spinner1.setAdapter(dataAdapter1);

     }

    public class myOnItemSelectedListener implements AdapterView.OnItemSelectedListener {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position,
                                   long id) {
            switch (parent.getId()) {
                case R.id.spinner:
                    name = parent.getItemAtPosition(position).toString();
                    spinner1.setEnabled(true);
                    break;
                case R.id.spinner1:
                    surname=parent.getItemAtPosition(position).toString();
            }

1 个答案:

答案 0 :(得分:1)

这是一个简单的完整工作示例:-

数据库帮助器 DBHelper.java

public class DBHelper extends SQLiteOpenHelper {

    public static final String DBNAME = "mydb";
    public static final int DBVERSON = 1;

    public static final String TBL_MYTABLE = "mytable";
    public static final String COl_MYTABLE_ID = BaseColumns._ID;
    public static final String COl_MYTABLE_NAME = "name";
    public static final String COl_MYTABLE_LASTNAME = "lastname";


    public DBHelper(@Nullable Context context) {
        super(context, DBNAME, null, DBVERSON);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE IF NOT EXISTS " + TBL_MYTABLE +
                "(" +
                COl_MYTABLE_ID + " INTEGER PRIMARY KEY," +
                COl_MYTABLE_NAME + " TEXT," +
                COl_MYTABLE_LASTNAME + " TEXT" +
                ")"
        );
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public long addRow(String name, String lastname) {
        ContentValues cv = new ContentValues();
        cv.put(COl_MYTABLE_NAME,name);
        cv.put(COl_MYTABLE_LASTNAME,lastname);
        SQLiteDatabase db = this.getWritableDatabase();
        return db.insert(TBL_MYTABLE,null,cv);
    }

    public ArrayList<String> getRows(String NameFilter, boolean return__name) {
        ArrayList<String> rv = new ArrayList<>();
        String whereclause = null;
        String[] whereargs = null;
        String groupby = null;
        if (NameFilter != null && NameFilter.length() > 0) {
            whereclause = COl_MYTABLE_NAME + "=?";
            whereargs = new String[]{NameFilter};
        }
        if (return__name) {
            groupby = COl_MYTABLE_NAME;
        }
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor csr = db.query(TBL_MYTABLE,null,whereclause,whereargs,groupby,null,COl_MYTABLE_NAME);
        while (csr.moveToNext()) {
            if (return__name) {
                rv.add(csr.getString(csr.getColumnIndex(COl_MYTABLE_NAME)));
            } else {
                rv.add(csr.getString(csr.getColumnIndex(COl_MYTABLE_LASTNAME)));
            }
        }
        csr.close();
        return rv;
    }
}
  • getRows方法采用两个参数
    • 第一个名称为“”或null的字符串等于获取所有内容。
    • 第二个布尔值,表示是否返回名称(true),否则返回姓氏(false)。
    • 第二个,如果为true,则引入GROUP BY name,因此不会列出重复的名称。

活动 MainActivity.java

public class MainActivity extends AppCompatActivity {

    Spinner mNameSpinner, mLastNameSpinner;
    ArrayAdapter<String> mNameAdapter, mLastNameAdapter;
    ArrayList<String> mNameList, mLastNameList;
    DBHelper mDBHlpr;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mNameSpinner = this.findViewById(R.id.namespinner);
        mLastNameSpinner = this.findViewById(R.id.lastnamespinner);
        mDBHlpr = new DBHelper(this);
        addSomeTestingData(); //Add some data
        manageNameSpinner(); //Manage the Name Spinner (which will manage the Lastname Spinner)
    }

    //Manage the Name Spinner - Called every-time the Spinner needs to be displayed or re-displayed
    private void manageNameSpinner() {
        mNameList = mDBHlpr.getRows(null, true);
        if (mNameAdapter == null) {
            mNameAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,mNameList);
            mNameSpinner.setAdapter(mNameAdapter);
            //NOTE if a selection is made then the LastName Spinner is managed
            mNameSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    manageLastNameSpinner();
                }

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

                }
            });
        } else {
            mNameAdapter.clear();
            mNameAdapter.addAll(mNameList);
            mNameAdapter.notifyDataSetChanged();
        }

    }

    //Manage the LastName Spinner - Called every-time the Spinner needs to be displayed or re-displayed
    private void manageLastNameSpinner() {
        if (mNameAdapter == null) return;
        mLastNameList = mDBHlpr.getRows(mNameSpinner.getSelectedItem().toString(),false);
        if (mLastNameAdapter == null) {
            mLastNameAdapter = new ArrayAdapter<>(this,android.R.layout.simple_dropdown_item_1line,mLastNameList);
            mLastNameSpinner.setAdapter(mLastNameAdapter);
        } else {
            mLastNameAdapter.clear();
            mLastNameAdapter.addAll(mLastNameList);
            mLastNameAdapter.notifyDataSetChanged();
        }
    }

    //Add some data to the database if none exists
    private void addSomeTestingData() {
        if (DatabaseUtils.queryNumEntries(mDBHlpr.getWritableDatabase(),DBHelper.TBL_MYTABLE) > 0) return;
        mDBHlpr.addRow("Fred","Bloggs");
        mDBHlpr.addRow("Joe","Smith");
        mDBHlpr.addRow("Joe","Bloggs");
        mDBHlpr.addRow("Fred","Smith");
        mDBHlpr.addRow("Mary", "Jones");
        mDBHlpr.addRow("Mary", "Taylor");
        mDBHlpr.addRow("Mary", "QuiteContrary");
        mDBHlpr.addRow("Anne","Boleyn");
        mDBHlpr.addRow("Anne","Hathaway");
        mDBHlpr.addRow("Annabel","Jones");
    }
}
  • 评论应该解释

结果

首次运行并选择名称微调器:-

enter image description here

从“名称”微调器中选择“安妮”,然后选择“姓氏”微调器:-

enter image description here