我得到了JSON响应,我必须将数据存储在微调器中,但是当我选择Alabama时," AL"在toast msg它应该来。我该怎么办?
{
"status" : "success",
"data" : {
"AL" : "Alabama",
"AK" : "Alaska"
}
}
先谢谢你
答案 0 :(得分:1)
试试我的朋友
/system/build.prop
答案 1 :(得分:1)
你好尝试这个,如果它有帮助
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
List<City>cityList= new ArrayList<>();
List<String> cityListName= new ArrayList<>();
Spinner spnCity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spnCity=(Spinner)findViewById(R.id.spnCity);
String response="{\"status\":\"success\",\"data\":{\"AL\":\"Alabama\",\"AK\":\"Alaska\"}}";
try {
JSONObject iObject=new JSONObject(response);
JSONObject data=iObject.getJSONObject("data");
Iterator<String> iter = data.keys();
while (iter.hasNext()) {
String key = iter.next();
try {
Object value = data.get(key);
cityListName.add(value.toString());
cityList.add(new City(key,value.toString()));
} catch (JSONException e) {
}
}
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, cityListName);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spnCity.setAdapter(dataAdapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String item = adapterView.getItemAtPosition(i).toString();
for (int j = 0; j < cityList.size(); j++) {
if(cityList.get(j).getCityName().equals(item)){
Toast.makeText(adapterView.getContext(), "Selected: " + item +" "+cityList.get(j).getCityName(), Toast.LENGTH_LONG).show();
}
}
// Showing selected spinner item
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
答案 2 :(得分:1)
尝试
JSONObject jsonObject = new JSONObject(jsonString);
String status = jsonObject.getString("status");
List<String> data = new ArrayList<String>();
JSONArray array = jsonObject.getJSONArray("data");
Iterator<String> keys = jsonObject.keys();
while (keys.hasNext()) {
String key = keys.next();
String value = jsonObject.getString(key);
}