繁体   English   中英

回收站视图未显示片段中的项目

[英]Recycler view not showing items in fragment

在此处获取回收站视图的数据。但在回收站视图中未显示任何项目。 数据在数据库中可用并在 OnCreate() 方法中获取。 但是当谈到 onCreate() 方法时,没有数据。 我从凌空获取数据。 当我将数据发送到回收器视图适配器时,它会显示为空列表我是否遗漏了任何东西

public class HomeFragment extends Fragment {

View view;
RecyclerView recyclerView;
private List<Appointment> appList = new ArrayList<>();


String url;

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

    view = inflater.inflate(R.layout.fragment_home, container, false);

    recyclerView = view.findViewById(R.id.rv_appointments);
    MyAppointmentAdapter adapter = new MyAppointmentAdapter(getActivity(), appList);
    //here data is not available for appList
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    recyclerView.setAdapter(adapter);

    return view;
}



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


    getData();

}




void getData() {
    appList = new ArrayList<>();
    RequestQueue requestQueue = Volley.newRequestQueue(getContext());
    url = "https://newswiftsalon.000webhostapp.com/appointment.php?salonNo=1";

    JsonArrayRequest request = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {

        @Override
        public void onResponse(JSONArray response) {

            System.out.println(response.toString());
            JSONObject jsonObject = null;

            for(int i = 0; i < response.length(); i++) {
                try {
                    jsonObject = response.getJSONObject(i);
                    Appointment appointment = new Appointment();
                    appointment.setAppNo(jsonObject.getInt("appNo"));
                    appointment.setFirstName(jsonObject.getString("firstName"));
                    appointment.setStylist(jsonObject.getString("name"));
                    appointment.setTime(jsonObject.getString("time"));
                    appointment.setDate(jsonObject.getString("date"));
                    appointment.setMobileNo(jsonObject.getString("mobileNo"));
                    appointment.setImage(jsonObject.getString("image"));
                    appointment.setHsNo(jsonObject.getString("hsNo"));
                    appointment.setStatus(jsonObject.getString("status"));
                    appointment.setSalonNo(jsonObject.getString("salonNo"));

                    appList.add(appointment);
                    //here data available for appList
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getActivity(), error.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });

    requestQueue.add(request);

}

}

在返回数据查询之前,您已经为适配器提供了数据集。 查询完成后,您需要执行以下操作:

((MyAppointmentAdapter)recyclerView.getAdapter()).data = appList;
((MyAppointmentAdapter)recyclerView.getAdapter()).notifyDataSetChanged();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM