我创建了一个应用程序,在该应用程序中,我想从第一个微调器中声明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();
}
答案 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;
}
}
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");
}
}