我在 Android Spinner 上遇到问题。在我的应用程序中,我在主布局上创建了两个 Spinner。 “州”微调器和“地区”微调器。 Spinner 上的所有数据都存储在 SQLite 数据库中。我想根据第一个微调器中特定“州”的选择在第二个微调器上显示“地区”列表。 示例:假设当我在第一个微调器中选择卡纳塔克邦时,应用程序首先从与卡纳塔克邦相关的 SQLite 数据库中检索所有地区,然后它显示在第二个微调器上。
为此,我正确地执行了所有数据库 Activity ,这意味着创建两个表“state”和“district”,其中 district 表中的一列是外键,它被引用到“state table”的主键之一
db.execSQL("create table "+STATE_TABLE+" ("+
STATE_ID+" integer primary key autoincrement not null, "+
STATE_NAME+" text"+")");
db.execSQL("create table "+DISTRICT_TABLE+" ("+DISTRICT_ID+
" integer primary key autoincrement not null,"+DISTRICT_NAME
+" text,"+STATE_ID+" integer, FOREIGN KEY("
+STATE_ID+") REFERENCES "+STATE_TABLE
+"("+STATE_ID+")"+")");
现在在 Activity 类中:
spinnerState = (Spinner)findViewById(R.id.spinner1);
spinnerDistrict = (Spinner)findViewById(R.id.spinner2);
stateList = new ArrayList<String>();
districtList = new ArrayList<String>();
假设所有数据都已准备好存储在数据库中。
现在我需要从数据库中检索所有“状态”数据并将其添加到 ArrayList 状态列表中。
Cursor stateCursor = database.query(STATE_TABLE, new String[]{STATE_ID, STATE_NAME},
null, null, null, null, STATE_NAME);
stateCursor.moveToFirst();
if(! stateCursor.isAfterLast()){
do{
int id = stateCursor.getInt(0);
String stateName = stateCursor.getString(1);
stateList.add(stateName);
}while(stateCursor.moveToNext());
}
stateCursor.close();
在此之后我创建一个 ArrayAdapter 并将这个状态列表放入其中。
spinnerState.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, stateList));
接下来我将以下代码放入 Activity 类中:
spinnerState.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View v,
int pos, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
现在我的问题在这里:
- 如何获取 StateId 以执行选择查询以获取数据库中与特定州相关的所有地区。
- 适配器将如何为 District 生成。
- 我把所有这些代码放在哪里。
在从状态 Spinner 获取值后,我需要创建 districtList。
本网站前面提出的类似问题,他们做什么: 他们已经为两个微调器创建了两个适配器,然后应用 setOnItemSelectedListener。 请帮助我,因为在这里我的思想完全停止工作。 我引用了很多书和网站,但他们甚至没有提到这类问题。
最佳答案
我在这里看到了几个解决方案:
选项 1:
而不是声明 stateList
作为ArrayList<String>
, 创建自定义 POJO StateInfo
并将 stateList 指定为 ArrayList<StateInfo>
public class StateList {
private int id;
private String stateName;
//Constructors, getters and setters
}
然后,
if(! stateCursor.isAfterLast()){
do{
int id = stateCursor.getInt(0);
String stateName = stateCursor.getString(1);
stateList.add(new StateInfo(id, stateName));
}while(stateCursor.moveToNext());
}
现在,您可以这样做:
public void onItemSelected(AdapterView<?> parent, View v,
int pos, long id) {
int id = stateList.get(pos).getId();
//Use This ID to construct your next SQLite query; and populate the district spinner.
}
如果这样做,您将必须创建自定义 ArrayAdapter
并实现 getView()
方法。查看 Android 文档以了解如何执行此操作。
选项 2:
为什么还需要 stateID?我想您可以编写一个查询来获取仅给定州名称的地区列表。在这种情况下,您可以保留 stateList
作为ArrayList<String>
你甚至不需要 StateInfo
根本上课。只需这样做:
public void onItemSelected(AdapterView<?> parent, View v,
int pos, long id) {
String stateName = stateList.get(pos);
//Use this name to construct your next SQLite query; and populate the district spinner.
}
关于Android:一个微调器的值取决于另一个微调器的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8679170/