如果Spinner getSelecteditem,如何更改TextView内容



我有一个类似的json文件

{"酒店":[{"date_res":"2018年12月3日","价格":980,}{"date_res":"2018年2月12日",《价格》:496,}],

我将date_res的值添加到微调器中,如果用户选择日期,我想在TextView中更改价格

为了更好地理解,您应该发布更多的代码。如果你的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对象获取日期和价格,以填充微调器和文本视图。希望这能有所帮助。

以下是的外观

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

你能做的就是

  1. 创建两个不同的列表,一个用于'date_res',另一个用于'sprice'
  2. 将OnItemSelectedListnr添加到微调器中,如下所示
  3. 在更改微调器的项目时,更改文本视图数据。具体如下:

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) {
}
  1. 将OnItemSelectedListnr添加到微调器

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

最新更新