By Spider Lynxz


2017-12-04 05:34:36 8 Comments

Here my array

{
 sales_schedule_name: "Visit Client",
 sales_schedule_date: "2018-01-18",
 sales_schedule_time: "10:30:00",
 sales_schedule_address: "Sydney",
 sales_schedule_description: "Sales Visit"
}

That array came from schedule.php

 $query = "select sales_schedule_name,sales_schedule_date,sales_schedule_time,sales_schedule_address,sales_schedule_description from sales_schedule";

if ($stmt = $conn->prepare($query)) {
 $stmt->execute();
 $stmt->bind_result($sales_schedule_name, $sales_schedule_date, $sales_schedule_time, $sales_schedule_address, $sales_schedule_description);

 $schedule = array(); 

 while ($stmt->fetch()) {
     //printf("%s, %s, %s, %s, %s\n", $sales_schedule_name, $sales_schedule_date, $sales_schedule_time, $sales_schedule_address, $sales_schedule_description);
     $temp = array();
     $temp['sales_schedule_name'] = $sales_schedule_name; 
     $temp['sales_schedule_date'] = $sales_schedule_date; 
     $temp['sales_schedule_time'] = $sales_schedule_time; 
     $temp['sales_schedule_address'] = $sales_schedule_address; 
     $temp['sales_schedule_description'] = $sales_schedule_description; 
     array_push($schedule, $temp);

 }
 $stmt->close();
 }

 //displaying the result in json format 
 echo json_encode($schedule);

Anyone can help me to call that array on my public list getTestData() using json?

public class ScheduleFragment extends Fragment {

    public ScheduleFragment() {
    }
    RecyclerView scheduleRecyclerView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_schedule, container, false);

        getActivity().setTitle("Schedule Task");
        scheduleRecyclerView= view.findViewById(R.id.schedule_list);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        scheduleRecyclerView.setLayoutManager(linearLayoutManager);
        scheduleRecyclerView.setHasFixedSize(true);
        ScheduleAdapter mAdapter = new ScheduleAdapter(getActivity(), getTestData());
        scheduleRecyclerView.setAdapter(mAdapter);
        return view;
    }

    public List getTestData() {
        final List recentSchedule = new ArrayList();
        recentSchedule.add(new ScheduleObject("Visit Client", "2018-01-18", "10:30","Sydney","Sales Visit"));

        return recentSchedule;
    }


}

i was try change with

public List<ScheduleObject> getTestData() {

    //recentSchedule.add(new ScheduleObject("Visit Client", "11/10/18", "10:30","Sydney","Sales Visit"));


    RequestQueue rq = Volley.newRequestQueue(getActivity().getApplicationContext());

    String url= AppConfig.URL_SCHEDULELIST;

    StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    // Do something with the response

                    try{

                        JSONObject o = new JSONObject(response);
                        JSONArray values=o.getJSONArray("salesScheduleName");

                        for ( int i=0; i< values.length(); i++) {

                            JSONObject schedules = values.getJSONObject(i);

                            String salesScheduleName = schedules.getString("salesScheduleName");
                            String salesScheduleDate = schedules.getString("salesScheduleDate");
                            String salesScheduleTime = schedules.getString("salesScheduleTime");
                            String salesScheduleAddress = schedules.getString("salesScheduleAddress");
                            String salesScheduleDescription = schedules.getString("salesScheduleDescription");


                            //Use the title and id as per your requirement
                            recentSchedule.add(new ScheduleObject(salesScheduleName, salesScheduleDate, salesScheduleTime, salesScheduleAddress, salesScheduleDescription));
                        }




                    }  catch (JSONException ex){}

                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    // Handle error
                }
            });
    return recentSchedule;
}

But nothing happen

1 comments

@Spider Lynxz 2017-12-06 08:15:34

After try to figure out which cause the problem on my listview, finaly this problem solved. Now i can call all data on my listview fragment.

public List<ScheduleObject> getTestData()   {

   try {

        JSONArray jsonArray=new JSONArray(getURL(AppConfig.URL_SCHEDULELIST));
        //iterate loop
        for(int i=0;i<jsonArray.length();i++){

            //get the JSON Object
            JSONObject schedules=jsonArray.getJSONObject(i);


            //prepare data to schedule list
            String salesschedulename = schedules.getString("salesschedulename");
            String salesscheduledate = schedules.getString("salesscheduledate");
            String salesscheduletime = schedules.getString("salesscheduletime");
            String salesscheduleaddress = schedules.getString("salesscheduleaddress");
            String salesscheduledescription = schedules.getString("salesscheduledescription");

            //adding the schedule to schedule list
            recentSchedule.add(new ScheduleObject(salesschedulename, salesscheduledate, salesscheduletime, salesscheduleaddress, salesscheduledescription));

        }

    } catch (Exception e) {

        System.out.println(e.getMessage());

        Toast toast = Toast.makeText(
                //getActivity(),"Custom Toast From Fragment",Toast.LENGTH_LONG
                getActivity().getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG
        );
        // Set the Toast display position layout center
        toast.setGravity(Gravity.CENTER,0,0);
        // Finally, show the toast
        toast.show();

    }

    return recentSchedule;
}


public static String getURL(String url) throws Exception {
    URL website = new URL(url);
    URLConnection connection = website.openConnection();
    BufferedReader in = new BufferedReader( new InputStreamReader(connection.getInputStream(),"UTF8"));

    StringBuilder response = new StringBuilder();
    String inputLine;

    while ((inputLine = in.readLine()) != null)
        response.append(inputLine);

    in.close();

    return response.toString();
}

Related Questions

Sponsored Content

48 Answered Questions

[SOLVED] How to check if an object is an array?

50 Answered Questions

93 Answered Questions

[SOLVED] How do I remove a particular element from an array in JavaScript?

  • 2011-04-23 22:17:18
  • Walker
  • 6264544 View
  • 7824 Score
  • 93 Answer
  • Tags:   javascript arrays

17 Answered Questions

[SOLVED] How to insert an item into an array at a specific index (JavaScript)?

28 Answered Questions

[SOLVED] How do I determine whether an array contains a particular value in Java?

  • 2009-07-15 00:03:21
  • Mike Sickler
  • 1910996 View
  • 2219 Score
  • 28 Answer
  • Tags:   java arrays

33 Answered Questions

[SOLVED] findViewById in Fragment

54 Answered Questions

[SOLVED] How can I pretty-print JSON in a shell script?

30 Answered Questions

[SOLVED] How to append something to an array?

18 Answered Questions

[SOLVED] How do I empty an array in JavaScript?

  • 2009-08-05 09:08:39
  • akano1
  • 2490839 View
  • 2198 Score
  • 18 Answer
  • Tags:   javascript arrays

Sponsored Content