1 year ago

#69555

test-img

tintin

Data missing in multiple View in RecyclerView using retrofit response

I am facing an issue in multiple view type recycler view i think the code in myadapter is lacking something which i am not able to figure out pls help. Here is the detailed description :

I have a response list from retrofit :

public class DateResponse {

@SerializedName("RecordEntryON")
@Expose
private String record_EntryOn;

@SerializedName("PatientID")
@Expose
private int Patient_ID;

@SerializedName("DiscountedAmount")
@Expose
private double discounted_Amount;

@SerializedName("ClientID")
@Expose
private int client_ID;

@SerializedName("TestType")
@Expose
private String test_Type;

@SerializedName("TestName")
@Expose
private String test_Name;

@SerializedName("FetchingTime")
@Expose
String fetching_Time;

@SerializedName("ClientTestCount")
@Expose
int clienttest_Count;

@SerializedName("CentreName")
@Expose
String center_Name;

getter and setter to follow (not writing here). I have another request model which i am passing into retrofit request here is the code for request model :

public class Date_Request {


@SerializedName("IsSuccess")
boolean is_success;

@SerializedName("Message")
String _message;

@SerializedName("ResponseData")
List<DateResponse> response_data;

code for retrofit in MyActivity :

 authToken = "Basic " + Utils.getAuthToken(Dashboard.this);
    restClient.getService().getByCentre(authToken, DEV_ID,defaultDate).enqueue(new 
Callback<Date_Request>() {
        @Override
        public void onResponse(Call<Date_Request> call, Response<Date_Request> response) {

         //   List<DateResponse> listAll;
            if (response.isSuccessful())
                try {
                   
                    boolean success = response.body().isIs_success();
                    String message = response.body().get_message();

                    if (success) {
                        listAll = response.body().getResponse_data();
                        List<DateResponse> centerList = new ArrayList<>();

                        int i;
                            for(i=0;i<5;i++){

                                String centerName = listAll.get(i).getCenter_Name();
                                int patientCount = listAll.get(i).getPatient_ID();
                                double amount = listAll.get(i).getDiscounted_Amount();

                                DateResponse topList = new DateResponse();


                       
                                topList.setPatient_ID(patientCount);
                                topList.setDiscounted_Amount(amount);
                                centerList.add(topList);

                              
  dateViewAdapter = new DateViewAdapter(centerList, Dashboard.this);
                                recyclerView.setAdapter(dateViewAdapter);
                                dateViewAdapter.notifyDataSetChanged();

                            }

Procedure : I am selecting an option from Spinner there are three options: center,date,test. On select, a calendar opens user select the date and the corresponding list is shown. APIs for all three options are different with little different data. However, in model i am using only one response model and one request.

Now the issue is if I am running this code than no of item matches the for loop ie 5(or whatever i set here 10 15) and displayed in list if i remove the for loop and directly add the list wo setting getting anything here in activity than i still get list with 10 items as I fixed the list to show 10 items only using getItemCount method in adapter

  1. whatever I use center name is not showing(blank) + TextView hardcoded text of xml layout is not showing + I am unable to concatenate anything before center name.
  2. The data--amount and patientCount is showing but no hardcoded text or cant concatenate anything with these fields
  3. In the test or date list in the same recycler view test name is showing but not test type(blank) instead it is showing patient count in test list and date list also

In centre list:

centre name not showing Patient Count and Amount showing but hardcoded text in textview missing in addition cant concatenate string Patient Count

In test list:

test name and amount is showing test type is not showing instead patient count is showing

In date list: same as test list

Code for adapter :

public class DateViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private List<DateResponse> listAll;
private final int limit = 10;

private Context mContext;

private static final int CENTER_LIST = 0;
private static final int TEST_LIST = 1;
private static final int DATE_LIST = 2;
private static final int TYPE_DEFAULT = 3;

public DateViewAdapter(List<DateResponse> listAll, Context mContext) {
    this.listAll = listAll;
    this.mContext = mContext;

}

public void addAllItems(List<DateResponse> items) {
    listAll.addAll(items);
    notifyDataSetChanged();
}

//override getItemViewType method to assign the layout to each item depending on the viewtype passed


@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view;
    //   view = LayoutInflater.from(parent.getContext()).inflate(R.layout.center_list, parent, false);
    //   return new DetailsViewHolder(view);
    switch (viewType) {
        case CENTER_LIST:
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.center_list, parent, false);
            return new CenterViewHolder(view);
        case TEST_LIST:
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.test_list, parent, false);
            return new TestViewHolder(view);
        case DATE_LIST:
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.date_list, parent, false);
            return new DateListViewHolder(view);
        default:
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.default_view, parent, false);
            return new DefaultViewHolder(view);

    }


}


@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

    if (listAll.get(position).toString().contains("CentreName")) {
        CenterViewHolder centerViewHolder = (CenterViewHolder) holder;
        centerViewHolder.setDetails(listAll.get(position));
      } else if (listAll.get(position).toString().contains("TestName")) {
          TestViewHolder testViewHolder = (TestViewHolder) holder;
          testViewHolder.setTestDetails(listAll.get(position));
      } else if (listAll.get(position).toString().contains("Date")) {
          DateListViewHolder dateListViewHolder = (DateListViewHolder) holder;
          dateListViewHolder.setDateListDetails(listAll.get(position));
      } else if (listAll.get(position).toString().contains("PatientID")) {
          DefaultViewHolder defaultViewHolder = (DefaultViewHolder) holder;
          defaultViewHolder.setDefaultDetails(listAll.get(position));

          // throw new RuntimeException("problem in logic");
      } else {
          throw new RuntimeException("problem in logic");
      }
}


public int getItemViewType(int position) {
    DateResponse dateResponse = new DateResponse();
    dateResponse = listAll.get(position);

    if(listAll.get(position).toString().contains("CentreName"))
    {
        return CENTER_LIST;
    } else if (listAll.get(position).toString().contains("TestName")) {
        return TEST_LIST;
    } else if (listAll.get(position).toString().contains("Date")) {
        return DATE_LIST;
    } else if (listAll.get(position).toString().contains("PatientID")) return TYPE_DEFAULT;
    return position;
}

@Override
public int getItemCount() {
    if (listAll.size() > limit) {
        return limit;
    } else {
        return listAll.size();
    }
}

class CenterViewHolder extends RecyclerView.ViewHolder {

    private TextView tvCenterName, tvPatientCount, tvAmount;

    public CenterViewHolder(@NonNull View itemView) {
        super(itemView);
        tvCenterName = itemView.findViewById(R.id.cn_name);
        tvPatientCount = itemView.findViewById(R.id.pt_count);
        tvAmount = itemView.findViewById(R.id.amount);
    }

    void setDetails(DateResponse centerDetails) {


        tvCenterName.setText("Centre Name : " + centerDetails.getCenter_Name());
        tvPatientCount.setText("Patient Count : " + centerDetails.getPatient_ID());
        tvAmount.setText("Amount : " + centerDetails.getDiscounted_Amount());

    }
}


class TestViewHolder extends RecyclerView.ViewHolder {

    private TextView tvTestName, tvTestType, tvTestAmount;

    public TestViewHolder(@NonNull View itemView) {
        super(itemView);
        tvTestAmount = itemView.findViewById(R.id.test_amount);
        tvTestName = itemView.findViewById(R.id.test_name);
        tvTestType = itemView.findViewById(R.id.test_type);

    }

    void setTestDetails(DateResponse testDetails) {
        tvTestType.setText("Test Name : " + testDetails.getTest_Type());
        tvTestName.setText(testDetails.getTest_Name());
        tvTestAmount.setText(String.valueOf(testDetails.getDiscounted_Amount()));
    }
}


class DateListViewHolder extends RecyclerView.ViewHolder {

    private TextView tvClientCount, tvPatCount, tvDateAmount;

    public DateListViewHolder(@NonNull View itemView) {
        super(itemView);

        tvClientCount = itemView.findViewById(R.id.client_name);
        tvPatCount = itemView.findViewById(R.id.patient_count);
        tvDateAmount = itemView.findViewById(R.id.date_test_amount);

    }

    void setDateListDetails(DateResponse dateListDetails) {
        tvDateAmount.setText(String.valueOf(dateListDetails.getDiscounted_Amount()));
        tvPatCount.setText(String.valueOf(dateListDetails.getPatient_ID()));
        tvClientCount.setText(dateListDetails.getTest_Name());
    }
}

class DefaultViewHolder extends RecyclerView.ViewHolder {
    private TextView tvdefaultCenter, tvdefaultCount, tvdefaultAmount;

    public DefaultViewHolder(@NonNull View itemView) {
        super(itemView);

        tvdefaultCenter = itemView.findViewById(R.id.default_center);
        tvdefaultCount = itemView.findViewById(R.id.default_pt_count);
        tvdefaultAmount = itemView.findViewById(R.id.default_amount);

    }

    void setDefaultDetails(DateResponse defaultDetails) {
        tvdefaultCenter.setText(defaultDetails.getCenter_Name());
        tvdefaultCount.setText(String.valueOf(defaultDetails.getPatient_ID()));
        tvdefaultAmount.setText(String.valueOf(defaultDetails.getDiscounted_Amount()));
    }
}

}

layout for centre_list view:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/parent_linear"
xmlns:app="http://schemas.android.com/apk/res-auto">

<androidx.cardview.widget.CardView
    android:id="@+id/parent_matrix"
    app:cardCornerRadius="3dp"
    app:cardElevation="2dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <LinearLayout
            android:id="@+id/cp_info"
            android:layout_width="wrap_content"
            android:orientation="vertical"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/cn_name"
                android:padding="1dp"
                android:layout_marginStart="5dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:text="Centre Name : "
                android:textColor="@color/purple_200">
            </TextView>
            <TextView
                android:padding="1dp"
                android:text="Patient Count : "
                android:id="@+id/pt_count"
                android:textColor="@color/black"
                android:layout_marginStart="5dp"
                android:paddingBottom="1dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
            </TextView>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_alignParentEnd="true">

            <TextView
                android:id="@+id/amount"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="end"
                android:layout_marginTop="@dimen/text_margin"
                android:layout_marginEnd="25dp"
                android:padding="2dp"
                android:text="Amount"
                android:textColor="@color/black"
                android:textStyle="bold" />

        </LinearLayout>


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

android

android-recyclerview

textview

retrofit

multiview

0 Answers

Your Answer

Accepted video resources