Android Spinner : Retrieving values from JSON array and displaying it in a spinner one after the other

StackOverflow https://stackoverflow.com/questions/23561977

  •  18-07-2023
  •  | 
  •  

Question

I have a problem of using json to retrieve the value and fetch in to the spinner selecting the time

JSON:

{
   "offer_detail":[
      {
         "movieid":"72",
         "date_value":"2014-05-11",
         "showtime":[
            "10:30 AM",
            "2:30 PM",
            "6:30 PM",
            "9:30 PM"
         ],
         "screen_no":"SCREEN 2",
         "id":"186"
      }
   ],
   "success":1,
   "message":"Successfully found "
}

I'm trying to display the showtime inside a spinner with respective times for ex - 10:30AM 2:30pm 6:30PM 9:30PM

but in my case, it displays the whole showtime array instead of displaying the times separately.

And this is what i've tried:

c = JSONfunctions
        .getJSONfromURL("http://192.168.1.104/OnlineTicketBooking/book_now1.php?movieid='72'");
try {

JSONArray jarray = c.getJSONArray("offer_detail");
ArrayList<String> xyz= new ArrayList<String>();
for(int i = 0; i < jarray.length(); i++){
 c = jarray.getJSONObject(i);
xyz.add(c.getString("showtime"));
Log.d("get value",c.getString("showtime"));


}

.

ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(Booking.this,      android.R.layout.simple_spinner_item,xyz);
dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(dataAdapter1);
String cl=spin.getSelectedItem().toString();
String filenameArray[] = cl.split(",");

int size =  filenameArray.length;
Log.d("count",""+size);
ArrayList<String> nw = new ArrayList<String>(); 

for (int j = 0; j < filenameArray.length; j++) {  
    JSONObject c = jarray.getJSONObject(j);
    nw.add(c.getString("showtime"));
     Log.d("getvalue",c.getString("showtime"));

}
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(Booking.this, android.R.layout.simple_spinner_item,nw);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(dataAdapter2);
spin.setOnItemSelectedListener(new OnItemSelectedListener() {

    public void onItemSelected(AdapterView<?> arg0, View arg1,
            int arg2, long arg3) {
        // TODO Auto-generated method stub

        Toast.makeText(getBaseContext(), spin.getSelectedItem().toString(),
            Toast.LENGTH_SHORT).show();
        String cl=spin.getSelectedItem().toString();
        Log.d("classname",cl);
    }

    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }
});
}

Thanks.

Was it helpful?

Solution 2

c = JSONfunctions
    .getJSONfromURL("http://192.168.1.104/OnlineTicketBooking/book_now1.php?movieid='72'");
try {

   JSONArray jarray = c.getJSONArray("offer_detail");
   ArrayList<String> xyz= new ArrayList<String>();
   for(int i = 0; i < jarray.length(); i++){
   JSONArray timeArray = jarray.getJSONObject(i).getJSONArray("showtime");
   for(int j = 0; j < timeArray .length(); j++){
   xyz.add(timeArray .getString(j));
 Log.d("get value",timeArray .getString(j));
  } 
 }

Try the above code

OTHER TIPS

Try it....

c = JSONfunctions
    .getJSONfromURL("http://192.168.1.104/OnlineTicketBooking/book_now1.php?movieid='72'");
try {

   JSONArray jarray = c.getJSONArray("offer_detail");
   ArrayList<String> xyz= new ArrayList<String>();
   for(int i = 0; i < jarray.length(); i++){
   JSONArray timeArray = jarray.getJSONObject(i).getJSONArray("showtime");
   for(int j = 0; j < timeArray .length(); j++){
   xyz.add(timeArray .getString(j));
 Log.d("get value",timeArray .getString(i));
  } 
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top