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

时间:2019-11-11 19:06:00

标签: java android android-fragments android-recyclerview

此处正在为“回收者”视图获取数据。但是在“回收者”视图中未显示任何项目。数据在数据库中可用,并通过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);

}

}

1 个答案:

答案 0 :(得分:0)

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

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