java - 列表中的最后一项覆盖回收站 View 项中的其他项

标签 java android android-recyclerview android-viewholder

我正在解析多个 JSON 以填充卡片 View 。问题是它们中的 4 个工作得很好,但一个似乎只是覆盖了所有其他的而不是追加。 enter image description here

从屏幕截图中查看医生数据。它重复了最后添加的医生(Sylvester)。我做了一个 toast ,看看模型中的数据是否正确,正如你所看到的,它正确地 toast 了所有不同的医生。可能是什么问题。 这是绑定(bind)查看器类:

 public void onBindViewHolder(final AppointmentsAdapter.myViewHolder myViewHolder, final int position) {
        appointmentsModel = new AppointmentsModel();
        appointmentsModel = appointmentsModelList.get(position);

        myViewHolder.appointments_date_TV.setText(appointmentsModel.getDate());
        myViewHolder.appointments_subject.setText(appointmentsModel.getSubject());
        myViewHolder.appointments_time.setText(appointmentsModel.getTime());
        myViewHolder.appointments_hospital.setText(appointmentsModel.getHospital());
        myViewHolder.appointments_doctor.setText(appointmentsModel.getDoctor());

        myViewHolder.appointments_county.setText(appointmentsModel.getCounty());
        myViewHolder.appointments_specialization.setText(appointmentsModel.getSpecialization());
        myViewHolder.denied.setText(appointmentsModel.getStatus());
        myViewHolder.pending.setText(appointmentsModel.getStatus());


        myViewHolder.approved.setText(appointmentsModel.getStatus());

        myViewHolder.overflow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast.makeText(view.getContext(), "Item is clicked", Toast.LENGTH_SHORT).show();
                // showPopupMenu(myViewHolder.overflow);
                //creating a popup menu
                PopupMenu popup = new PopupMenu(ctx, myViewHolder.overflow);
                //inflating menu from xml resource
                popup.inflate(R.menu.card_view_options);
                //adding click listener
                popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        switch (item.getItemId()) {
                            case R.id.action_chat:
                                //get doctor id from sqlite
                                HashMap<String, String> user = db.getUserDetails();
                                String loggedInPatient= user.get("id");

                                //get specifics patient id from the model
                                String clickedDoctor = appointmentsModel.getDaktari();


                                //pass them to the chats activy to initiate chats for this particular patient
                                //with this particular doctor
                                Intent intent = new Intent(ctx, ChatsActivity.class);
                                intent.putExtra("doctor", clickedDoctor);
                                intent.putExtra("patient",loggedInPatient);
                                ctx.startActivity(intent);

                                break;
                            case R.id.action_delete:
                                String doctorPhone = appointmentsModel.getPhoneNumber();
                                Intent intent2 = new Intent(ctx, MakePhoneCall.class);
                                intent2.putExtra("phone", doctorPhone);
                                break;
                        }
                        return false;
                    }
                });
                //displaying the popup
                popup.show();

            }
        });



        String a = appointmentsModel.getStatus().toString();

        if (a.contains("approved")) {
            myViewHolder.approved.setVisibility(View.VISIBLE);
            myViewHolder.pending.setVisibility(View.INVISIBLE);
            myViewHolder.denied.setVisibility(View.INVISIBLE);


        } else if (a.contains("declined")) {
            myViewHolder.denied.setVisibility(View.VISIBLE);
            myViewHolder.pending.setVisibility(View.INVISIBLE);
            myViewHolder.approved.setVisibility(View.INVISIBLE);


        } else if (a.contains("pending")) {
            myViewHolder.pending.setVisibility(View.VISIBLE);
            myViewHolder.approved.setVisibility(View.INVISIBLE);
            myViewHolder.denied.setVisibility(View.INVISIBLE);

        }


        int len = appointmentsModel.getDate().length();
        String to_cut = appointmentsModel.getDate().substring(0, 10);

        myViewHolder.appointments_date_TV.setText(to_cut);


    }

这是获取 JSON 并解析每个 JSON 的类:最后一个 for 循环是有问题的 JSON。

 private void postLoginDetails(final String email, final String password) {
        // Tag used to cancel the requestl
        String tag_string_req = "req_register";


        StringRequest strReq = new StringRequest(Request.Method.POST,
                Configs.URL_LOGIN +Configs.LIST_APPOINTMENTS, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {


                try {
                    appointmentsList = new ArrayList<>();

                    JSONObject jsonObject = new JSONObject(response);
                    JSONArray jsonArray = jsonObject.getJSONArray("myAppointments");
                    JSONArray jsonArray2 = jsonObject.getJSONArray("myDoctor");
                    JSONArray jsonArray3 = jsonObject.getJSONArray("myCounty");
                    JSONArray jsonArray4 = jsonObject.getJSONArray("mySpecialization");
                    JSONArray jsonArray5 = jsonObject.getJSONArray("myHospital");

                    if(jsonArray.length()<1 || jsonArray2.length()<1 ||jsonArray3.length()<1 ||jsonArray4.length()<1 ||jsonArray.length()<1) {
                        appointmentsList.clear();
                        no_data.setVisibility(View.VISIBLE);

                    }else if( jsonArray.length()>0) {
                        if (no_data.getVisibility() == View.VISIBLE) {
                            no_data.setVisibility(View.GONE);
                        }
                    }


                    for(int i=0 ; i<jsonArray.length() ; i++) {
                        JSONObject data = jsonArray.getJSONObject(i);
                        appointmentsModel = new AppointmentsModel();
                        appointmentsModel.setDate(data.getString("appointmentDate"));
                        appointmentsModel.setTime(data.getString("time"));
                        appointmentsModel.setSubject(data.getString("subject"));
                        appointmentsModel.setStatus(data.getString("status"));
                        appointmentsList.add(appointmentsModel);

                        for (int b = 0; b < jsonArray5.length(); b++) {
                            JSONObject data5 = jsonArray5.getJSONObject(i);
                            appointmentsModel.setHospital(data5.getString("hospitalName"));
                        }

                        for (int c = 0; c < jsonArray3.length(); c++) {
                            JSONObject data3 = jsonArray3.getJSONObject(i);
                            appointmentsModel.setCounty(data3.getString("countyName"));
                        }
                        for (int d = 0; d < jsonArray4.length(); d++) {
                            JSONObject data4 = jsonArray4.getJSONObject(i);
                            appointmentsModel.setSpecialization(data4.getString("specializationName"));
                        }
                        for (int a = 0; a < jsonArray2.length(); a++) {
                            JSONObject data2 = jsonArray2.getJSONObject(a);
                            appointmentsModel.setDoctor(data2.getString("firstName"));
                            //Toast to test if data set is okay
                            String test = data2.getString("firstName");
                            Toast.makeText(Appointments.this,test, Toast.LENGTH_SHORT).show();




                        }


                    }



                    appointmentsAdapter = new AppointmentsAdapter(Appointments.this,appointmentsList);

                    linearLayoutManager = new LinearLayoutManager(Appointments.this);
                    appointments_RV.setLayoutManager(linearLayoutManager);
                    appointments_RV.setItemAnimator(new DefaultItemAnimator());
                    appointments_RV.setAdapter(appointmentsAdapter);
                    appointments_RV.setSaveEnabled(true);
                    appointments_RV.setSaveFromParentEnabled(true);
                    appointmentsAdapter.notifyDataSetChanged();




                } catch (JSONException e) {

                    System.out.print("error" + e.getMessage());
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {

            @Override
            protected Map<String, String> getParams() {
                // Posting params to login_url
                Map<String, String> params = new HashMap<String, String>();
                params.put("email", email);
                params.put("password", password);
                params.put("action", "login");

                return params;
            }
        };
        AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
    }

这是相同的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:padding="4dp"
    android:background="#e9dedbdb"
    >


    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="4dp"

        >




        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="2dp"
                android:id="@+id/card_view_contents"
                android:orientation="horizontal"
                android:padding="2dp">


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:layout_margin="8dp">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        >

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:text="Subject : "
                            android:textColor="@color/artson_blue"
                            android:textSize="16dp"
                            />

                        <TextView
                            android:id="@+id/appointments_subject__TV"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Diabetes Check up"
                            android:textAppearance="@android:style/TextAppearance.DeviceDefault.Medium"
                            android:textColor="@color/colorAccent" />
                        <Space
                            android:layout_width="0dp"
                            android:layout_height="0dp"
                            android:layout_weight="1" />
                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:id="@+id/card_view_overflow"
                            android:src="@drawable/blueoverflow"
                            android:paddingRight="20dp"
                            />

                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal">


                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:text="Date : "
                            android:textColor="@color/artson_blue"
                            android:textSize="16dp" />



                    <TextView
                        android:id="@+id/appointments_date_TV"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="start"
                        android:text=" 12.04.2017"
                        android:textAlignment="textStart"
                        android:textAppearance="@android:style/TextAppearance.DeviceDefault.Medium"
                        android:textColor="@color/colorAccent" />

                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal">


                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:text="Time : "
                            android:textColor="@color/artson_blue"
                            android:textSize="16dp" />

                    <TextView
                        android:id="@+id/appointments_time_TV"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_gravity="center"
                        android:text=" 11:00 AM"
                        android:textAppearance="@android:style/TextAppearance.DeviceDefault.Medium"
                        android:textColor="#000" />

                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal">


                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:text="Hospital : "
                            android:textColor="@color/artson_blue"
                            android:textSize="16dp" />

                    <TextView
                        android:id="@+id/appointments_hospital_TV"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:layout_gravity="center"
                        android:text=" Hema"
                        android:textAppearance="@android:style/TextAppearance.DeviceDefault.Medium"
                        android:textColor="#000" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text=" ("
                            android:textColor="#000"
                             />
                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:id="@+id/appointments_county_TV"
                            android:text=""
                            android:textAppearance="@android:style/TextAppearance.DeviceDefault.Medium"
                            android:textColor="#000"
                            />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text=")"
                            android:textColor="#000"
                            />

                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal">


                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Doctor : "
                            android:textColor="@color/artson_blue"
                            android:textSize="16dp" />

                    <TextView
                        android:id="@+id/appointments_Doctor_TV"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:layout_gravity="center"
                        android:text=" Dr. Haji"
                        android:textAppearance="@android:style/TextAppearance.DeviceDefault.Medium"
                        android:textColor="#000" />


                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text=" ("
                            android:textColor="#000"
                            />
                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:id="@+id/appointments_specialization_TV"
                            android:text=""
                            android:textAppearance="@android:style/TextAppearance.DeviceDefault.Medium"
                            android:textColor="#000"
                            />


                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text=")"
                            android:textColor="#000"
                            />

                    </LinearLayout>


                </LinearLayout>



            </LinearLayout>




                    <Button
                        android:id="@+id/btn_pending"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@drawable/button_pending"
                        android:text="Pending"
                        android:visibility="visible"
                        android:textAlignment="center"
                        android:textColor="#ffff"
                        android:textSize="15sp"
                        android:layout_below="@+id/card_view_contents" />

                    <Button
                        android:id="@+id/btn_approved"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"

                        android:background="@drawable/button_approved"
                        android:visibility="invisible"
                        android:text="Approved"
                        android:textAlignment="center"
                        android:textColor="#ffff"
                        android:textSize="15sp"
                        android:layout_below="@+id/card_view_contents"/>

                    <Button
                        android:id="@+id/btn_declined"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@drawable/button_declined"
                        android:visibility="invisible"
                        android:text="Declined"
                        android:textAlignment="center"
                        android:textColor="#ffffff"
                        android:textSize="15sp"
                        android:layout_below="@+id/card_view_contents"/>






        </RelativeLayout>

    </android.support.v7.widget.CardView>


</LinearLayout>

和 onCreateViewHolder

 public AppointmentsAdapter.myViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View my_view = inflater.inflate(R.layout.appointments_item, viewGroup, false);
        return new myViewHolder(my_view);
    }

最佳答案

添加这条语句:

 appointmentsList.add(appointmentsModel);

在最后一个内部 for 循环之后。像这样:

 for(int i=0 ; i<jsonArray.length() ; i++) {
                        JSONObject data = jsonArray.getJSONObject(i);
                        appointmentsModel = new AppointmentsModel();
                        appointmentsModel.setDate(data.getString("appointmentDate"));
                        appointmentsModel.setTime(data.getString("time"));
                        appointmentsModel.setSubject(data.getString("subject"));
                        appointmentsModel.setStatus(data.getString("status"));

                        for (int b = 0; b < jsonArray5.length(); b++) {
                            JSONObject data5 = jsonArray5.getJSONObject(i);
                            appointmentsModel.setHospital(data5.getString("hospitalName"));
                        }

                        for (int c = 0; c < jsonArray3.length(); c++) {
                            JSONObject data3 = jsonArray3.getJSONObject(i);
                            appointmentsModel.setCounty(data3.getString("countyName"));
                        }
                        for (int d = 0; d < jsonArray4.length(); d++) {
                            JSONObject data4 = jsonArray4.getJSONObject(i);
                            appointmentsModel.setSpecialization(data4.getString("specializationName"));
                        }
                        for (int a = 0; a < jsonArray2.length(); a++) {
                            JSONObject data2 = jsonArray2.getJSONObject(a);
                            appointmentsModel.setDoctor(data2.getString("firstName"));
                            //Toast to test if data set is okay
                            String test = data2.getString("firstName");
                            Toast.makeText(Appointments.this,test, Toast.LENGTH_SHORT).show();




                        }
// add appointment here
appointmentsList.add(appointmentsModel);


                    }

关于java - 列表中的最后一项覆盖回收站 View 项中的其他项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51282803/

相关文章:

java泛型通配符

java - 尝试在 Android 中发送电子邮件而无需用户干预时出现主线程网络异常

java - 无法读取 com.sun.xml.ws 的工件描述符 :jaxws-rt:jar:2. 2.8 : UnresolvableModelException: Failure to transfer com. sun.xml.ws:bundles:pom

Android 将当前包的签名与 debug.keystore 进行比较

android - 是否可以创建一个动态的 RecyclerView 适配器来接受任何对象列表 <Object> 的列表?

java - 使用 VS Code 编译 Java,收到 "restriction on required library"错误消息

android - 有没有一种通用的方法可以找到适用于 View 的样式?

android - 我想知道已从通话记录中删除的通话电话号码

android - RecyclerView - 正确实现 SnackBar UNDO 删除 (Kotlin)

android - 编辑文本奇怪的行为