我有一个像这样的json文件
{“酒店”:[{“ date_res”:“ 03/12/2018”,“价格”:980,} {“ date_res”:“ 12/12/2018”,“价格”:496,}] ,
然后我将date_res的值添加到微调器中, 我想做的是,如果用户选择一个日期,则在TextView中更改价格
答案 0 :(得分:0)
您应该发布更多代码以更好地理解。 如果您的json数据是这样的:
{
"hotels": [{
"date_res": "03/12/2018",
"price": 980
}, {
"date_res": "12/12/2018",
"price": 496
}]
}
您可以创建一个模型类,例如:
public class Example {
@SerializedName("hotels")
@Expose
private List<Hotel> hotels = null;
public List<Hotel> getHotels() {
return hotels;
}
public void setHotels(List<Hotel> hotels) {
this.hotels = hotels;
}
public static class Hotel {
@SerializedName("date_res")
@Expose
private String dateRes;
@SerializedName("price")
@Expose
private Integer price;
public String getDateRes() {
return dateRes;
}
public void setDateRes(String dateRes) {
this.dateRes = dateRes;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
}
}
然后使用List对象获取日期和价格,以填充微调框和textview。希望这会有所帮助。
答案 1 :(得分:0)
final TextView textView = (TextView) findViewById(R.id.textView);
final Spinner spinner=(Spinner) findViewById(R.id.spinner);
//HashMap
final HashMap<String,Integer> mapList=new HashMap<>();
//JSON String
String json = "{\"hotels\":[{\"date_res\":\"03/12/2018\",\"price\":980},{\"date_res\":\"12/12/2018\",\"price\":496}]}";
//JSON String to JSON Object to HashMap
try {
JSONObject jsonObj = new JSONObject(json.toString());
JSONArray jsonObj2=jsonObj.getJSONArray("hotels");
for (int i = 0 ; i < jsonObj2.length(); i++) {
JSONObject obj = jsonObj2.getJSONObject(i);
String date_res = obj.getString("date_res");
int price = obj.getInt("price");
mapList.put(date_res,price);
}
} catch (JSONException e) {
e.printStackTrace();
}
//Fill the Spinner ArrayList
final ArrayList<String> spinnerList = new ArrayList<String>();
for ( String key : mapList.keySet() ) {
Log.d("@@"," Yes" +key.toString());
spinnerList.add(key);
}
//Spinner Adapter
final ArrayAdapter arrayAdapterSpinner=new ArrayAdapter(this,android.R.layout.simple_spinner_item, spinnerList);
spinner.setAdapter(arrayAdapterSpinner);
//OnSelectedItem Change
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
final String key=(String)spinner.getSelectedItem();
if(mapList.containsKey(spinnerList.get(position))){
textView.setText(mapList.get(key).toString());
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
答案 2 :(得分:0)
您可以做的是
1。 从JSON创建日期和价格的两个列表。
`ArrayList<String> date_res = new ArrayList();
ArrayList<String> price = new ArrayList();
try {
JSONObject json = new JSONObject(response);
JSONArray array = json.getJSONArray("hotels");`
for (int i = 0; i<= array.length; i++) {
if(json.has("date_res")) {
String s_dateRes = json.getString("date_res");
date_res.put(s_dateRes);
}
if(json.has("price")) {
String s_price = json.getString("price");
price.put(s_price);
}
}
} catch(JSONException ex) {
}
将OnItemSelectedListenr添加到微调器中
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView,
int position, long id) {
// 3. Set textView data
textView.setText(price.get(position));
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
}
});