Android:一个微调器的值取决于其他微调器的值
我在 Android Spinner 上遇到问题。在我的应用程序中,我在主布局上创建了两个 Spinner。 “州”旋转器和“区”旋转器。 Spinner 上的所有数据都存储在 SQLite 数据库中。我想根据第一个微调器中特定“州”的选择在第二个微调器上显示“地区”列表。 示例:假设当我在第一个微调器中选择卡纳塔克邦时,应用程序首先从 SQLite 数据库中检索与卡纳塔克邦相关的所有地区,然后将其显示在第二个微调器上。
为此,我正确执行所有数据库活动意味着创建两个表“州”和“区”,其中区表中的一列是外键,该外键引用“州表”的主键之一
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+")"+")");
现在在活动类中:
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));
接下来,我将以下代码放入活动类中:
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。 请帮助我,因为在这里我的思维完全停止工作。 我参考了很多书籍和网站,但他们甚至没有提到此类问题。
I have a problem on Android Spinner.In my application I created two Spinner on main layout. 'State' Spinner and 'District' Spinner. All the data on Spinner are stored in SQLite database. I want do display list of 'District' on second spinner depending on selection of particular 'State' in the first spinner.
Example: Suppose when I select Karnataka in the first spinner then application first retrieve all the district from SQLite database related to karnataka state and then it display on second Spinner.
For this I do all the database activity correctly means creating two table 'state' and 'district' in which one column in district table is foreign key which is refereed to one of the primary key of '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+")"+")");
Now in the Activity Class:
spinnerState = (Spinner)findViewById(R.id.spinner1);
spinnerDistrict = (Spinner)findViewById(R.id.spinner2);
stateList = new ArrayList<String>();
districtList = new ArrayList<String>();
Suppose all the data are all ready stored in database.
Now I need to retrieve all the 'State' data from database and add it in the statelist which is 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();
after this I create one ArrayAdapter and put this state list into this.
spinnerState.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, stateList));
Next i put the following code in the activity class:
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
}
});
Now My problem is here:
- How I get the StateId for executing the select query for taking all the district related to particular state in database.
- How the adapter will generate for District.
- where I put all these code.
Here what I need creating the districtList after getting the value from state Spinner.
Similar Question which asked earlier in this website, what they do:
they already create two adapter for two spinner and then apply setOnItemSelectedListener.
Please Help me because here my mind totally stop working.
I refer lot of book and website but not they even mention these type of problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在这里看到了几个解决方案:
选项 1:
不要将
stateList
声明为ArrayList
,而是创建一个自定义 POJOStateInfo
并指定stateList asArrayList
然后,
现在,您可以执行此操作:
如果执行此操作,您将必须创建一个自定义
ArrayAdapter
并实现getView()
方法。请参阅 Android 文档了解如何执行此操作。选项 2:
为什么还需要 stateID?我想您可以编写一个查询来获取仅给定州名称的地区列表。在这种情况下,您可以将
stateList
保留为ArrayList
,甚至根本不需要StateInfo
类。只需这样做:I see a couple of solutions here:
Option 1:
Instead of declaring
stateList
as anArrayList<String>
, create a custom POJOStateInfo
and designate stateList asArrayList<StateInfo>
Then,
Now, you can do this:
If you do this, you will have to create a custom
ArrayAdapter
and implement thegetView()
method. Look at the Android docs for how to do this.Option 2:
Why do you even need the stateID? I suppose you can write a Query to get the districts list given a state Name only. In that case, you can retain the
stateList
as anArrayList<String>
and you don't even need theStateInfo
class at all. Just do this:对于问题1:
你只需要跟踪用户在旋转器中选择了哪个州,并根据它的位置,你必须找到它的相关id。然后你可以相应地查询地区。显然你会必须使用该微调器的 onItemSelectedListener 来获取所选状态的位置。
对于问题 2:
您需要在帮助程序类中创建一个方法,该方法接受 stateId(您将按照我上面所说的方式计算)并根据字符串数组中的地区名称返回您。所以在您的主要活动中,在状态微调器的 onItemSelectedListener 中,您必须获取该字符串数组并为其准备一个适配器。在那里,您必须为您的地区微调器设置适配器。
这将始终为您提供区域微调器,以更新用户在状态数组中选择的内容。
对于问题 3:
几乎所有代码都将仅位于您的州微调器的 onItemSelectedListener 中。仅获取相关地区名称的方法将位于您的辅助类中。
For problem 1:
You just need to keep track of which state user selects into the spinner and according to its position,you have to find its related id.then you can query for district accordingly.obviously you will have to use onItemSelectedListener of that spinner to get position of state selected.
For problem 2:
You need to make a method in helper class which accepts stateId(which you would compute as i said above) and return you according district names in a String array.So in your main activity,in onItemSelectedListener of a state spinner,you will have to grab that string array and prepare a adapter for the same. There,you will have to set adapter to your district spinner.
This will always give you your district spinner updated with what user selects in state array.
For problem 3:
Almost all code would be in onItemSelectedListener of your state spinner only.Just a method to fetch related district names would be in your helper class.