gpt4 book ai didi

java - 选择状态微调器后填充城市微调器

转载 作者:太空狗 更新时间:2023-10-29 14:30:45 26 4
gpt4 key购买 nike

这是我的问题:

我如何拥有两个微调器“State”和“City”,但在用户首先选择一个州之前城市将是空的。

我正在使用 json 数据动态构建微调器,您将在下面的代码中看到,一旦状态微调器的值为 != 0,我就会使用状态微调器的项目值并为我的城市执行另一个数据库调用。

我唯一的错误是在我创建新的 ArrayAdapter 以保存城市数据时显示。我讨厌为我的 Activity 发布所有代码,但不确定我的问题出在哪里。

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();
}





}

}

最佳答案

首先设置所有的 AdapterString 数组,然后在获取城市数据时调用 adapter.notifyDatasetChanged()。像这样:

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);

zipspinner 实现一个 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.
}

});

关于java - 选择状态微调器后填充城市微调器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7213115/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com