I have two spinner, the first spinner is related to the second spinner.
I'm using JSON to fill the adapter for both of them.
When i start the activity, both of adapter is already filled with a correct data(the first and second adapter is related).
But, when i try to change item position of the first spinner, the second spinner's adapter still contain first loaded data.
What i want is, the second spinner's adapter can dynamically contain a data that related from first spinner, when an item position from first spinner has changed.
I don't know what's the problem, i really need some help.
here's my code :
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected (AdapterView < ? > parent, View view,int position, long id){
try {
JSONObject json = result.getJSONObject(position);
result = json.getJSONArray("all_state");
state.clear();
for (int i = 0; i < result.length(); i++) {
try {
JSONObject json2 = result.getJSONObject(i);
state.add(json2.getString("state"));
} catch (JSONException e) {
e.printStackTrace();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
adapter = new ArrayAdapter<>(ViewState.this, R.layout.spinner_state, state);
adapter.notifyDataSetChanged();
spinner2.setAdapter(adapter);
}
@Override
public void onNothingSelected (AdapterView < ? > parent){
}
});
It's solved, the problem is because i put the JSONObject and JSONArray into the same variable "result".
So here's the right code :
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected (AdapterView < ? > parent, View view,int position, long id){
try {
JSONObject json = result.getJSONObject(position);
result2 = json.getJSONArray("all_state");
state.clear();
for (int i = 0; i < result2.length(); i++) {
try {
JSONObject json2 = result2.getJSONObject(i);
state.add(json2.getString("state"));
} catch (JSONException e) {
e.printStackTrace();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
adapter = new ArrayAdapter<>(ViewState.this, R.layout.spinner_state, state);
adapter.notifyDataSetChanged();
spinner2.setAdapter(adapter);
}
@Override
public void onNothingSelected (AdapterView < ? > parent){
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.