-1

I have a json file like this

{"hotels":[{"date_res":"03/12/2018","price":980,} {"date_res":"12/12/2018","price":496,}],

And I added the value of date_res to a spinner, What I want to do is change the price in TextView if the user select a date

Chawki Tazzi
  • 225
  • 3
  • 10

3 Answers3

0

You should post more code for better understanding. If your json data is something like this:

{
    "hotels": [{
        "date_res": "03/12/2018",
        "price": 980
    }, {
        "date_res": "12/12/2018",
        "price": 496
    }]
}

You can create a model class like:

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

}

}

Then use the List object to get the dates as well as the price to populate your spinner and textview. Hope this helps.

truespan
  • 189
  • 1
  • 2
  • 11
0

Here is how it does look

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

                }
            });
ZINE Mahmoud
  • 1,272
  • 1
  • 17
  • 32
  • im working with volley library, and this code gives me just the last result – Chawki Tazzi Dec 08 '18 at 11:06
  • then you have to post more information. post your code and where exactly you want the help . As an answer to your Question above this is a clean and direct answer . – ZINE Mahmoud Dec 08 '18 at 12:45
0

What you can do is

  1. Create Two different lists one for 'date_res' and another for 'price'.
  2. Add OnItemSelectedListenr to your spinner as shown here.
  3. On changing the item of the spinner change textview data. It all follows as below:

1. Creating two list for date and price from 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. Add OnItemSelectedListenr to your spinner

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

Kinjal Rathod
  • 499
  • 4
  • 12