• 4
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

I am using Github library StepperTouch in my recyclerview layout. Inside my adapter, I am trying to remove an element from the array list when the stepper is zero or change the value of the element inside the array list when the value is increased or decreased. Problem is as to how to notify the adapter of the change inside my bindviewholder() method below is my adapter

public class MyCartAdapter  extends RecyclerView.Adapter<MyCartAdapter.MyCartViewHolder>{
private List<AllItems> listItems1;
private Context context;

public MyCartAdapter(List<AllItems> listItems1, Context context) {
    this.listItems1 = listItems1;
    this.context = context;
}

@NonNull
@Override
public MyCartAdapter.MyCartViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.cart_items_layout, parent, false);
    return new MyCartViewHolder(v);
}

@Override
public void onBindViewHolder(@NonNull final MyCartAdapter.MyCartViewHolder holder, final int position) {


    final AllItems orderItem = listItems1.get(position);
    holder.setProductImage(orderItem.getImageUrl(),context);
    holder.name.setText(orderItem.getName());
    holder.setStepper(context);
    holder.stepperTouch.setCount(Integer.parseInt(orderItem.getQuantity()));
    String price = String.valueOf(orderItem.getPrice());
    holder.price.setText(price);
    holder.setComname(orderItem.getComname());

    final HashMap<String, AllItems> items = ((UserMainActivity)context).getItemMap();

    holder.stepperTouch.addStepCallback(new OnStepCallback() {
        @Override
        public void onStep(int value, boolean positive) {
            int count = holder.stepperTouch.getCount();
            if (count==0){
                AllItems item = items.get(orderItem.getName());
                if (item!=null){
                    String orderit = orderItem.getName();
                    ((UserMainActivity)context).removeItem(orderit);
                    listItems1.remove(position);
                }
            }
            else {
                    String quantity = String.valueOf(holder.stepperTouch.getCount());
                    String orderitemname = orderItem.getName();
                    String url = orderItem.getImageUrl();
                    String weight = orderItem.getWeight();
                    AllItems newitem = new AllItems(orderitemname ,orderItem.getComname(),url, quantity,weight,orderItem.getPrice());
                    ((UserMainActivity)context).addItem(orderitemname,newitem);
                    listItems1.set(position, newitem);
            }
        }
    });
}


@Override
public int getItemCount() {

    return listItems1.size();

}


public class MyCartViewHolder extends RecyclerView.ViewHolder {
    public TextView name,price,count,comname;
    public TextView weight;
    public ImageView productImage;

    public MyCartViewHolder(View itemView) {
        super(itemView);
        name = (TextView) itemView.findViewById(R.id.ProName);
        price = (TextView) itemView.findViewById(R.id.proPrice);
        weight = (TextView) itemView.findViewById(R.id.ProWeight);
    }

    public void setProductImage(final String thumb_image, final Context ctx){

        productImage = (ImageView) itemView.findViewById(R.id.ProImage);
        Picasso.with(ctx).setIndicatorsEnabled(false);
        Picasso.with(ctx)
                .load(thumb_image)
                .networkPolicy(NetworkPolicy.OFFLINE)
                .placeholder(R.drawable.basket_b).into(productImage, new Callback() {
            @Override
            public void onSuccess() {
            }
            @Override
            public void onError() {
                Picasso.with(ctx).load(thumb_image).placeholder(R.drawable.basket).into(productImage);
            }
        });
}


    public void setComname(String name){
        comname = (TextView)itemView.findViewById(R.id.proComName);
        comname.setText(name);
    }
    public void setStepper(final Context context){
        stepperTouch = (StepperTouch) itemView.findViewById(R.id.stepper);
        stepperTouch.setMinValue(0);
        stepperTouch.setMaxValue(9);
        stepperTouch.setSideTapEnabled(true);
    }
    StepperTouch stepperTouch;
}

}

i have tried using MyCartAdapter.this.notifyDataSetChanged(); but its showing error. Cannot call this method while RecyclerView is computing a layout or scrolling androidx.recyclerview.widget.RecyclerView

the above code works for removing but I can not do the notifyDatasetChanged() inside the bindview method is there any way to use notifydatasetChanged() in this case.

try running it on UI thread

 runOnUiThread(new Runnable(){
  public void run() {
  // UI code goes here
 }
});
  • 1
Reply Report
    • did you tried notify data if not please call notifyDataSetChanged() after this line listItems1.remove(position)
    • yes i have tried after remove() method but its showing an error cannot call this method while RecyclerView is computing a layout or scrolling androidx.recyclerview.widget.RecyclerView

You are not in the scope of your Adapter, but rather of your OnStepCallback. To access your adapter's scope you can try : YourAdapter.this.notifyDataSetChanged();

  • 0
Reply Report