有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java回收器视图未在片段中显示项

正在为回收器视图提取数据。但在回收器视图中不显示任何项目。数据在数据库中可用,并通过OnCreate()方法获取。 但是当谈到onCreate()方法时,没有数据。我从截击中获得数据。当我将数据发送到recycler视图适配器时,它将作为空列表 我遗漏了什么吗

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) 个答案

  1. # 1 楼答案

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

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