无法将数据从Volley加载到Fragment(选项卡布局)

时间:2019-06-08 12:02:48

标签: android android-fragments android-volley android-tablayout

我正尝试使用齐射将数据从mysql数据库加载到Fragment In Tab布局中。问题在于该函数正在从数据库获取数据,但未将其加载到片段中。

当我尝试在列表中添加硬编码数据时,它可以工作并将数据加载到片段中。但是它不能与凌空工作。请帮助

这是我的片段代码:

public class DriverFragment extends Fragment {
    View v;
    private RecyclerView mRecyclerView;
    Context ctx;
    private static final String URL_PRODUCTS = "http://192.168.10.11/AutoBus/show_driver.php";
    List<DriverModel> mData;
    public DriverFragment() {
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        v = inflater.inflate(R.layout.driver_frgment, container, false);

        mRecyclerView = (RecyclerView) v.findViewById(R.id.mon_data);
        DriverAdapter recyclerViewAdapter = new DriverAdapter(getContext(),mData);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerView.setAdapter(recyclerViewAdapter);
        return v;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mData = new ArrayList<>();

        /*mData.add(new DriverModel("Asad","092 4587455","Asss","ssss","dddddd"));
        mData.add(new DriverModel("Asad","092 4587455","Asss","ssss","dddddd"));
        mData.add(new DriverModel("Asad","092 4587455","Asss","ssss","dddddd"));
        mData.add(new DriverModel("Asad","092 4587455","Asss","ssss","dddddd"));
*/

        loadDetails();
    }

    private void loadDetails() {


        /*
         * Creating a String Request
         * The request type is GET defined by first parameter
         * The URL is defined in the second parameter
         * Then we have a Response Listener and a Error Listener
         * In response listener we will get the JSON response as a String
         * */
        StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_PRODUCTS,
                new Response.Listener<String>() {

                    @Override
                    public void onResponse(String response) {
                        try {
                            //converting the string to json array object
                            JSONArray array = new JSONArray(response);

                            //traversing through all the object
                            for (int i = 0; i < array.length(); i++) {

                                //getting product object from json array
                                JSONObject data = array.getJSONObject(i);

                                //adding the product to product list
                                mData.add(new DriverModel(
                                        data.getString("bus_number"),
                                        data.getString("driver_uname"),
                                        data.getString("driver_password"),
                                        data.getString("driver_id"),
                                        data.getString("driver_phone")
                                ));
                                Toast.makeText(getActivity(), ""+data, Toast.LENGTH_LONG).show();
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                            Toast.makeText(getActivity(), "Error" + e.toString(), Toast.LENGTH_SHORT).show();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(getActivity(), "Error" + error.toString(), Toast.LENGTH_SHORT).show();


                    }
                });

        //adding our stringrequest to queue
        Volley.newRequestQueue(Objects.requireNonNull(getActivity())).add(stringRequest);
    }

}

2 个答案:

答案 0 :(得分:0)

在列表上设置数据之后,在凌空请求的onResponse(String response)方法内调用适配器类的notifyDataSetChanged()方法

  // make global object DriverAdapter class
    DriverAdapter recyclerViewAdapter;


     for (int i = 0; i < array.length(); i++) {
        //getting product object from json array
        JSONObject data = array.getJSONObject(i);
        //adding the product to product list
        mData.add(new DriverModel(data.getString("bus_number"),
        data.getString("driver_uname"),
        data.getString("driver_password"),
        data.getString("driver_id"),
        data.getString("driver_phone")));
     Toast.makeText(getActivity(), ""+data, Toast.LENGTH_LONG).show();
    }

  //after set data on list,call here notifiyDataSetChanged() method
  recyclerViewAdapter.notifyDataSetChanged();

答案 1 :(得分:0)

 //try getParam then test it    

}){
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    Map<String, String> params = new HashMap<>();
                    params.put("id", UseId);
                    return params;
                }