首页 > 解决方案 > Arrarylist 大小为 0,在 android 中具有静态 recyclerview 列表

问题描述

嗨,在下面的代码适配器中,我在调试代码时定义了一个列表,它给我的患者大小为 0。现在所有列表项都设置为 recyclerview 以显示项目列表。但没有显示

实际上模型类包含一个数据。但它给我的是0

任何人都可以帮助我重新爱上它。

list_item.java:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/bg"
    android:id="@+id/contact_card"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_anchorGravity="center"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" >


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/card_details">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:id="@+id/cards"
                android:background="@color/bg">


                <androidx.cardview.widget.CardView
                    android:id="@+id/cardView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:cardBackgroundColor="@color/slivergray"
                    app:cardCornerRadius="5dp"
                    app:cardElevation="4dp"
                    app:cardUseCompatPadding="true">
                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical"
                        android:id="@+id/suject_ids"
                        android:background="@color/white">
                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:orientation="horizontal"
                            android:padding="2dp"
                            android:layout_marginTop="1dp">
                            <de.hdodenhof.circleimageview.CircleImageView
                                android:id="@+id/doctor_profile"
                                android:layout_width="96dp"
                                android:layout_height="96dp"
                                android:src="@drawable/ic_round_account_circle_24"
                                app:civ_border_width="2dp"
                                app:civ_border_color="@color/theme"/>
                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_weight="1"
                                android:layout_marginTop="4dp"
                                android:orientation="vertical">
                                <LinearLayout
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:orientation="horizontal">
                                <TextView
                                    android:id="@+id/doctor_name"
                                    style="@style/Base.TextAppearance.AppCompat.Subhead"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_below="@+id/names"
                                    android:paddingLeft="16dp"
                                    android:text="Dr. Regina Joseph"
                                    android:layout_weight="1"
                                    android:textAllCaps="true"
                                    android:textColor="@color/theme"
                                    android:textSize="14sp"
                                    android:textStyle="bold"/>
                                    <TextView
                                        android:layout_width="wrap_content"
                                        android:layout_height="wrap_content"
                                        android:text="Booking : Scheduled"
                                        android:padding="2dp"
                                        android:textColor="@color/theme">

                                    </TextView>
                                </LinearLayout>
                                <LinearLayout
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:orientation="horizontal">
                                <TextView
                                    android:id="@+id/age"
                                    style="@style/Base.TextAppearance.AppCompat.Subhead"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_below="@+id/doctor_name"
                                    android:paddingLeft="16dp"
                                    android:text="Age : 26"
                                    android:textStyle="bold"
                                    android:layout_weight="1"
                                    android:textAllCaps="false"
                                    android:textColor="@color/theme"
                                    android:textSize="14sp" />
                                    <TextView
                                        android:layout_width="wrap_content"
                                        android:layout_height="wrap_content"
                                        android:text="Booking ID: 10"
                                        android:padding="2dp"
                                        android:textColor="@color/theme">
                                    </TextView>
                                </LinearLayout>
                                <TextView
                                    android:id="@+id/date"
                                    style="@style/Base.TextAppearance.AppCompat.Subhead"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_below="@+id/doctor_name"
                                    android:paddingLeft="16dp"
                                    android:text="Today,25 Dec"
                                    android:textStyle="bold"
                                    android:layout_weight="1"
                                    android:textAllCaps="false"
                                    android:textColor="@color/theme"
                                    android:textSize="14sp" />
                                <TextView
                                    android:id="@+id/time"
                                    style="@style/Base.TextAppearance.AppCompat.Subhead"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_below="@+id/doctor_name"
                                    android:paddingLeft="16dp"
                                    android:text="11:00 - 11:15 AM"
                                    android:textStyle="bold"
                                    android:layout_weight="1"
                                    android:textAllCaps="false"
                                    android:textColor="@color/theme"
                                    android:textSize="14sp" />

                                <LinearLayout
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:padding="5dp"
                                    android:weightSum="2"
                                    android:orientation="horizontal">
                                    <TextView
                                        android:layout_width="wrap_content"
                                        android:layout_height="wrap_content"
                                        android:text="View Details"
                                        android:layout_below="@+id/consultationFees"
                                        android:layout_weight="1"
                                        android:layout_gravity="center"
                                        android:gravity="center"
                                        android:textAllCaps="false"
                                        android:textColor="@color/theme">
                                    </TextView>
                                    <TextView
                                        android:layout_width="wrap_content"
                                        android:layout_height="wrap_content"
                                        android:text="Cancel Appointment"
                                        android:id="@+id/select"
                                        android:padding="5dp"
                                        android:layout_below="@+id/consultationFees"
                                        android:layout_weight="1"
                                        android:layout_gravity="right"
                                        android:gravity="right"
                                        android:textAllCaps="false"
                                        android:textColor="@color/theme">
                                    </TextView>
                                </LinearLayout>
                            </LinearLayout>

                        </LinearLayout>


                    </RelativeLayout>
                </androidx.cardview.widget.CardView>

            </LinearLayout>



        </LinearLayout>


    </FrameLayout>

</RelativeLayout>

患者适配器.java:

public class PatientAdapter extends RecyclerView.Adapter<PatientAdapter.MyViewHolder> {

    private Context mContext;
    private List<PatientModel> patientModels;

    public PatientAdapter(List<PatientModel> patientModels) {
        this.patientModels=patientModels;
    }

    @NonNull
    @Override
    public PatientAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view = inflater.inflate(R.layout.patient_list, parent, false);
        PatientAdapter.MyViewHolder holder = new PatientAdapter.MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull PatientAdapter.MyViewHolder holder, int position) {
        holder.doctor_name.setText(patientModels.get(position).getPatient_name());
    }

    @Override
    public int getItemCount() {
        Log.d("size of the patient", String.valueOf(patientModels.size()));
        return patientModels.size();

    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        TextView doctor_name;

        public MyViewHolder(View itemView) {
            super(itemView);
            doctor_name=itemView.findViewById(R.id.doctor_name);

        }
    }
}

查看PatientsFragment.java:

public class ViewPatientsFragment extends Fragment{
    private ArrayList<PatientModel> patientModelsList;
    public View_PatientList() {
        // Required empty public constructor
    }

    private RecyclerView recyclerPatient;

    private PatientAdapter patientAdapter;


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

        View rootView = inflater.inflate(R.layout.view_patientlist, container, false);

        recyclerPatient=rootView.findViewById(R.id.recyclerPatient);
        patientModelsList=new ArrayList<>();
        patientAdapter=new PatientAdapter(patientModelsList);
        recyclerPatient.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        recyclerPatient.setAdapter(patientAdapter);

        return rootView;
    }
}

标签: androidandroid-recyclerview

解决方案


问题是您没有向数组中插入任何东西,如您所见,您初始化数组,然后将其放入适配器中。

patientModelsList=new ArrayList<>(); <-- EMPTY
patientAdapter=new PatientAdapter(patientModelsList); <-- SENDING AN EMPTY LIST

--What you need--
patientModelsList.add(YOUR OBJECT HERE)
patientModelList.add(YOUR OBJECT HERE)
--And then call--
patientAdapter=new PatientAdapter(patientModelsList);

您需要做的是在将数组发送到适配器之前将其添加到数组中,或者在适配器中有一个方法,就像insertItems()您传递数据一样,记得做一个

list.clear()
list.addAll(items)
notifyDataSetChanged()

推荐阅读