Java 如果微调器getSelecteditem,如何更改TextView内容
我有一个类似这样的json文件 {“酒店”:[{“日期”:“2018年12月3日”,“价格”:980,}{“日期”:“2018年12月12日”,“价格”:496,}] 我将date_res的值添加到微调器中,Java 如果微调器getSelecteditem,如何更改TextView内容,java,android,json,Java,Android,Json,我有一个类似这样的json文件 {“酒店”:[{“日期”:“2018年12月3日”,“价格”:980,}{“日期”:“2018年12月12日”,“价格”:496,}] 我将date_res的值添加到微调器中, 我想做的是在TextView中更改价格,如果用户选择了日期您应该发布更多代码以更好地理解。 如果您的json数据如下所示: { "hotels": [{ "date_res": "03/12/2018", "price": 980 }, {
我想做的是在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;
}
}
}
公共类示例{
@序列化名称(“酒店”)
@暴露
私人列表酒店=null;
公共酒店名单{
返回酒店;
}
公共酒店(列表酒店){
这个。hotels=hotels;
}
公共静态级酒店{
@序列化名称(“日期”)
@暴露
私有字符串日期;
@序列化名称(“价格”)
@暴露
私人整数价格;
公共字符串getDateRes(){
返回日期;
}
公共void setDateRes(字符串dateRes){
this.dateRes=dateRes;
}
公共整数getPrice(){
退货价格;
}
公共定价(整数价格){
这个价格=价格;
}
}
}
然后使用列表对象获取日期和价格,以填充微调器和文本视图。希望这有帮助。
final TextView TextView=(TextView)findViewById(R.id.TextView);
最终微调器微调器=(微调器)findViewById(R.id.Spinner);
//哈希映射
final HashMap mapList=新HashMap();
//JSON字符串
字符串json=“{\'hotels\':[{\'date\'res\':\'03/12/2018\',\'price\':980},{\'date\'res\':\'12/12/2018\',\'price\':496}];
//JSON字符串到JSON对象到HashMap
试一试{
JSONObject jsonObj=新的JSONObject(json.toString());
JSONArray jsonObj2=jsonObj.getJSONArray(“酒店”);
for(int i=0;i
你能做的是
`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) {
}
`ArrayList date\u res=new ArrayList();
ArrayList价格=新的ArrayList();
试一试{
JSONObject json=新的JSONObject(响应);
JSONArray数组=json.getJSONArray(“酒店”)`
对于(int i=0;i parentView){
}
});
我正在使用volley library,这段代码只提供了最后一个结果,您需要发布更多信息。请发布您的代码,以及您希望得到帮助的确切位置。作为对您上述问题的回答,这是一个干净而直接的答案。
`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) {
}