0

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.

2 Answers 2

2

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));
  } 
 }
Sign up to request clarification or add additional context in comments.

2 Comments

Nice, but dude it is displaying the first value four times and not the other values.
ohh.. Please change xyz.add(timeArray .getString(i)); to xyz.add(timeArray .getString(j));
0
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

1 Comment

Please vote the answer so that others will also follow this code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.