Search code examples
androidhorizontal-recyclerview

Not detecting invisible item values if it is in horizontalrecyclerview


i have my scrollview items horizontally from sunday to saturday with checkboxes, in mobile view it is showing sunday to saturday if i scroll,if not scroll it shows sunday to wednesday,

coming to point when i update my subscription it takes values from sunday to wednesay,remaining values not coming they coming as null what should i do?

here is my adapterclass code:

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

private List<DaysOfDelivery> daysOfDeliveryList;
 public static String[] daylist;


public class MyViewHolder extends RecyclerView.ViewHolder {
    CheckBox checkBox;
    TextView textViewPrice;

    public MyViewHolder(View view) {
        super(view);
        checkBox = (CheckBox) view.findViewById(R.id.deliveryname_checkbox);
        textViewPrice = (TextView) view.findViewById(R.id.period_price);
    }
}


public SubscriptionRecyclerViewAdapter(List<DaysOfDelivery> daysDeliveryList) {
    this.daysOfDeliveryList = daysDeliveryList;
    daylist = new String[daysOfDeliveryList.size()];

}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.horizontal_listview_availability, parent, false);

    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    DaysOfDelivery days = daysOfDeliveryList.get(position);
    holder.checkBox.setText(days.getAvailabilityDays());
    holder.checkBox.setChecked(days.getCheckbox());
    holder.textViewPrice.setText(days.getPrice());
    if (days.getCheckbox())
        daylist[position]="1";
    else
        daylist[position]="0";

}

@Override
public int getItemCount() {
    return daysOfDeliveryList.size();
}

}

and my activity class update functionality:

update1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String[] days= SubscriptionRecyclerViewAdapter.daylist;

            if(isConnected)
                new EditSubscription().execute();
            else
                Toast.makeText(Subscription.this, "No Internet Connection", Toast.LENGTH_LONG).show();
        }
    });

in days i'm getting only sunday to wednesday values,remaining are not coming


Solution

  • maintain the daylist array in your activity/fragment, and when you want to update that list from adapter use callback for update.

    In recyclerView views that are not visible in screen, are not been initialize until they comes in screen.

    OR initialize that list too in constructor:

    public SubscriptionRecyclerViewAdapter(List<DaysOfDelivery> daysDeliveryList) {
        this.daysOfDeliveryList = daysDeliveryList;
        daylist = new String[daysOfDeliveryList.size()];
    
        for (int i = 0; i < daysDeliveryList.size(); i++) {
            daylist[i] = daysDeliveryList.get(i).getCheckbox() ? "1" : "0";
        }
    
    }