列表中的最后一项覆盖了回收者视图项中的其他项

时间:2018-07-11 10:12:25

标签: java android android-recyclerview android-viewholder

我正在解析几个JSON,以填充cardview。问题是其中4个工作正常,但其中一个似乎覆盖了所有其他而不是附加。 enter image description here

从屏幕截图中检查医生数据,它重复最后添加的医生(Sylvester)。我做了一次敬酒,看看模型中的数据是否正确,如您所见,它正确地敬酒了所有不同的医生。可能是什么问题。 这是bind viewholder类:

 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);
    }

2 个答案:

答案 0 :(得分:1)

添加此语句:

 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);


                    }

答案 1 :(得分:0)

我知道了。

我在getJSONObject中传递了错误的参数

for (int a = 0; a < jsonArray2.length(); a++) {
                            JSONObject data2 = jsonArray2.getJSONObject(a);

应该是

for (int a = 0; a < jsonArray2.length(); a++) {
                            JSONObject data2 = jsonArray2.getJSONObject(i);

从父for循环中注意a而不是i。

谢谢大家的帮助。