填充城市微调后的状态微调已选定 [英] populate city spinner after state spinner has been selected
问题描述
这是我的问题:
我怎么有两个微调国家和城市,但直到用户首先选择一个国家的城市将是空的。
我建立我的纺纱动态使用JSON数据,你会在下面,我的code看到一旦国家微调值!= 0,那么我用的是国家微调的项目值,做一套数据库调用我的城市。
我唯一的错误显示当我创建新的ArrayAdapter举办城市的数据。我恨后我所有的code我的活动,但不知道在哪里我的问题。
公共类SearchActivity延伸活动{
私有静态最后字符串变量=MyApp的;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.search_layout);
最后的微调zipspinner =(微调)findViewById(R.id.zipspinner);
最后的微调cityspinner =(微调)findViewById(R.id.cityspinner);
JSONArray jsonArray;
最后JSONArray cityArray;
尝试 {
//获取状态值从DATACALL(数据库)
字符串spinnerContentType =国家;
字符串spinnerURL =getStoreState.php;
字符串spinner_data = DataCall.getJSON(spinnerURL,spinnerContentType);
jsonArray =新JSONArray(spinner_data);
最终的String [] array_spinner =新的String [jsonArray.length()];
的for(int i = 0; I< jsonArray.length();我++)
{
串styleValue = jsonArray.getJSONArray(ⅰ).getString(0);
array_spinner [我] = styleValue;
}
//添加状态值,以微调
ArrayAdapter<字符串>适配器=
新的ArrayAdapter<字符串> (本,
android.R.layout.simple_spinner_item,array_spinner);
adapter.setDropDownViewResource(R.layout.state_spinner_layout);
zipspinner.setAdapter(适配器);
zipspinner.setOnItemSelectedListener(新OnItemSelectedListener(){
公共无效onItemSelected(适配器视图<>为arg0,查看ARG1,
INT ARG2,长ARG3){
INT项目= zipspinner.getSelectedItemPosition();
//如果国有项目被选中现在得到旅游城市航班从DATABALL
如果(项目!= 0){
尝试 {
字符串item_value = array_spinner [项目]
字符串spinnerContentType =城市;
字符串spinnerURL =?getStoreCity.php状态=+ item_value;
字符串city_data = DataCall.getJSON(spinnerURL,spinnerContentType);
cityArray =新JSONArray(city_data);
最终的String [] city_spinner =新的String [cityArray.length()];
的for(int i = 0; I< cityArray.length();我++)
{
串styleValue = cityArray.getJSONArray(ⅰ).getString(0);
city_spinner [我] = styleValue;
}
//这是我的问题是尝试添加的城市及其SPNNER
ArrayAdapter<字符串>适配器2 =
新的ArrayAdapter<字符串> (本,
android.R.layout.simple_spinner_item,city_spinner);
adapter2.setDropDownViewResource(R.layout.city_spinner_layout);
cityspinner.setAdapter(适配器2);
cityspinner.setOnItemSelectedListener(新OnItemSelectedListener(){
公共无效onItemSelected(适配器视图<>为arg0,查看ARG1,
INT ARG2,长ARG3){
INT项目= cityspinner.getSelectedItemPosition();
如果(项目!= 0){
字符串item_value = array_spinner [项目]
字符串nameContentType =名;
字符串shopURL =?getStoreList.php城市=+ item_value;
字符串name_data = DataCall.getJSON(shopURL,nameContentType);
束束=新包();
bundle.putString(shopData,name_data);
Log.v(TAG,name_data);
/ **意图myIntent =新的意图(SearchActivity.this,ShowRestaurant.class);
myIntent.putExtras(包);
startActivityForResult(myIntent,0); * /
}
其他 {
// 完();
}
}
公共无效onNothingSelected(适配器视图<>为arg0){
}
});
}赶上(JSONException E){
// TODO自动生成的catch块
e.printStackTrace();
}
}
其他 {
// 完();
}
}
公共无效onNothingSelected(适配器视图<>为arg0){
}
});
}赶上(JSONException E){
// TODO自动生成的catch块
e.printStackTrace();
}
}
}
将所有的适配器
和字符串
阵列,然后再就叫 adapter.notifyDatasetChanged()
,而你得到了城市的数据。是这样的:
字符串city_values [] =新的String [] {请选择一个状态。};
ArrayAdapter<字符串>适配器2 =新的ArrayAdapter<字符串> (这一点,android.R.layout.simple_spinner_item,city_spinner);
adapter2.setDropDownViewResource(R.layout.city_spinner_layout);
cityspinner.setAdapter(适配器2);
为 zipspinner
实施 OnItemSelectedListener
。
zipspinner.setOnItemSelectedListener(新OnItemSelectedListener()
{
公共无效onItemSelected(适配器视图<>母公司视图中查看,INT POS,长ID){
字符串值= state_values [POS]
现在//获取对价值的城市名单。
city_values = yourWayOfGettingData(值);
adapter2.notifyDatasetChanged();
}
公共无效onNothingSelected(适配器视图父){
// 没做什么。
}
});
This is my question:
How do I have two spinners "State" and "City" but the city will be empty until the user selects a state first.
I am building my spinners dynamically using json data and you will see in my code below that once the state spinner value is != 0 then I use the item value of the state spinner and do another database call for my cities.
My only error is showing when I create my new ArrayAdapter to hold to the city data. I hate to post all of my code for my activity but not sure where my issue is.
public class SearchActivity extends Activity{
private static final String TAG = "MyApp";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_layout);
final Spinner zipspinner = (Spinner) findViewById(R.id.zipspinner);
final Spinner cityspinner = (Spinner) findViewById(R.id.cityspinner);
JSONArray jsonArray;
final JSONArray cityArray;
try {
//GET STATE VALUES FROM DATACALL (DATABASE)
String spinnerContentType = "state";
String spinnerURL = "getStoreState.php";
String spinner_data = DataCall.getJSON(spinnerURL,spinnerContentType);
jsonArray = new JSONArray(spinner_data);
final String[] array_spinner = new String[jsonArray.length()];
for (int i=0; i<jsonArray.length(); i++)
{
String styleValue = jsonArray.getJSONArray(i).getString(0);
array_spinner[i] = styleValue;
}
//ADD STATE VALUES TO SPINNER
ArrayAdapter<String> adapter =
new ArrayAdapter<String> (this,
android.R.layout.simple_spinner_item,array_spinner);
adapter.setDropDownViewResource(R.layout.state_spinner_layout);
zipspinner.setAdapter(adapter);
zipspinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
int item = zipspinner.getSelectedItemPosition();
//IF ITEM IN STATE IS SELECTED NOW GET CITIES FROM DATABALL
if(item != 0){
try {
String item_value = array_spinner[item];
String spinnerContentType = "city";
String spinnerURL = "getStoreCity.php?state=" + item_value;
String city_data = DataCall.getJSON(spinnerURL,spinnerContentType);
cityArray = new JSONArray(city_data);
final String[] city_spinner = new String[cityArray.length()];
for (int i=0; i<cityArray.length(); i++)
{
String styleValue = cityArray.getJSONArray(i).getString(0);
city_spinner[i] = styleValue;
}
//THIS IS WHERE MY ISSUE IS TRYING TO ADD THE CITIES TO THEIR SPNNER
ArrayAdapter<String> adapter2 =
new ArrayAdapter<String> (this,
android.R.layout.simple_spinner_item,city_spinner);
adapter2.setDropDownViewResource(R.layout.city_spinner_layout);
cityspinner.setAdapter(adapter2);
cityspinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
int item = cityspinner.getSelectedItemPosition();
if(item != 0){
String item_value = array_spinner[item];
String nameContentType = "name";
String shopURL = "getStoreList.php?city=" + item_value;
String name_data = DataCall.getJSON(shopURL,nameContentType);
Bundle bundle = new Bundle();
bundle.putString("shopData", name_data);
Log.v(TAG,name_data);
/** Intent myIntent = new Intent(SearchActivity.this, ShowRestaurant.class);
myIntent.putExtras(bundle);
startActivityForResult(myIntent, 0); */
}
else {
// finish();
}
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else {
// finish();
}
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Set all your Adapter
s and String
arrays first and then just call adapter.notifyDatasetChanged()
while you got the data for city. something like this:
String city_values[] = new String[]{"Please select a state."};
ArrayAdapter<String> adapter2 = new ArrayAdapter<String> (this,android.R.layout.simple_spinner_item, city_spinner);
adapter2.setDropDownViewResource(R.layout.city_spinner_layout);
cityspinner.setAdapter(adapter2);
for the zipspinner
implement a OnItemSelectedListener
.
zipspinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent,View view, int pos, long id) {
String value = state_values[pos];
// now get your city list against value.
city_values = yourWayOfGettingData(value);
adapter2.notifyDatasetChanged();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
});
这篇关于填充城市微调后的状态微调已选定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!