0

这是我的数组

{
 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"
}

该数组来自 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);

任何人都可以帮助我使用 json 在我的公共列表 getTestData() 上调用该数组吗?

公共类 ScheduleFragment 扩展片段 {

    公共 ScheduleFragment() {
    }
    RecyclerView scheduleRecyclerView;
    @覆盖
    public View onCreateView(LayoutInflater inflater, ViewGroup 容器, Bundle savedInstanceState) {
        查看视图 = inflater.inflate(R.layout.fragment_schedule, container, false);

        getActivity().setTitle("调度任务");
        scheduleRecyclerView= view.findViewById(R.id.schedule_list);
        线性布局管理器线性布局管理器=新线性布局管理器(getActivity());
        scheduleRecyclerView.setLayoutManager(linearLayoutManager);
        scheduleRecyclerView.setHasFixedSize(true);
        ScheduleAdapter mAdapter = new ScheduleAdapter(getActivity(), getTestData());
        scheduleRecyclerView.setAdapter(mAdapter);
        返回视图;
    }

    公共列表 getTestData() {
        最终列表 recentSchedule = new ArrayList();
        最近的Schedule.add(new ScheduleObject("拜访客户", "2018-01-18", "10:30","悉尼","销售拜访"));

        返回最近的时间表;
    }


}

我试着改变

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

但什么也没有发生

4

1 回答 1

0

在尝试找出导致我的列表视图出现问题的原因后,最终解决了这个问题。现在我可以调用我的列表视图片段上的所有数据。

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();
}
于 2017-12-06T08:15:34.767 回答