I have a shopping cart In this cart, I want to click on the plus button My RecyclerView will be upgraded
In this cart, I want to click on the mines button My RecyclerView will be upgraded
my code :
public void update(List<ShopCartGet> array) {
mDataset = array;
notifyDataSetChanged();
}
public class ShopCartHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView title, price;
TextView number_order, priceall;
ImageView img, delete, minus, plus;
Spinner sp;
TextView id;
int currentNos;
int ids = 0;
public void update(List<ShopCartGet> list) {
mDataset = list;
notifyDataSetChanged();
}
public void delete(int position) {
list.remove(position);
notifyItemRemoved(position);
}
public void increment() {
currentNos = Integer.parseInt(number_order.getText().toString());
number_order.setText(String.valueOf(++currentNos));
ids = Integer.parseInt(id.getText().toString());
email = ShopCart.string_from_sp;
password = ShopCart.string_from_sp1;
}
public void Decrease() {
currentNos = Integer.parseInt(number_order.getText().toString());
number_order.setText(String.valueOf(--currentNos));
ids = Integer.parseInt(id.getText().toString());
email = ShopCart.string_from_sp;
password = ShopCart.string_from_sp1;
}
public void update() {
String BASE_URL = "http://laravel.cahoo.ir/json/android/update_basket.php";
StringRequest stringRequest = new StringRequest(Request.Method.POST, BASE_URL, new Response.Listener<String>() {
@Override
public void onResponse(String s) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("email", email);
params.put("order_id", String.valueOf(ids));
params.put("number_order", String.valueOf(currentNos));
params.put("password", password);
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
return super.getHeaders();
}
};
stringRequest.setRetryPolicy(new DefaultRetryPolicy(20000, 1, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
RequestQueue queue = Volley.newRequestQueue(context);
queue.add(stringRequest);
}
public void delete() {
String BASE_URL = "xxx";
StringRequest stringRequest = new StringRequest(Request.Method.POST, BASE_URL, new Response.Listener<String>() {
@Override
public void onResponse(String s) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("email", email);
params.put("order_id", String.valueOf(ids));
params.put("number_order", String.valueOf(currentNos));
params.put("password", password);
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
return super.getHeaders();
}
};
stringRequest.setRetryPolicy(new DefaultRetryPolicy(20000, 1, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
RequestQueue queue = Volley.newRequestQueue(context);
queue.add(stringRequest);
notifyDataSetChanged();
}
public ShopCartHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.text_info);
number_order = (TextView) itemView.findViewById(R.id.number_order);
price = (TextView) itemView.findViewById(R.id.price);
priceall = (TextView) itemView.findViewById(R.id.priceall);
img = (ImageView) itemView.findViewById(R.id.image_mahsol);
delete = (ImageView) itemView.findViewById(R.id.delet);
minus = (ImageView) itemView.findViewById(R.id.minus);
plus = (ImageView) itemView.findViewById(R.id.plus);
id = (TextView) itemView.findViewById(R.id.ids);
delete.setOnClickListener(this); //button onclick listener
plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
increment();
update();
}
});
minus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Decrease();
update();
}
});
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i("salam", "imag");
delete();
}
});
}
@Override
public void onClick(View v) {
delete(getAdapterPosition()); //calls the method above to delete
}
}
public ShopCartAdapter(Context context, List<ShopCartGet> myDataset) {
this.mDataset = myDataset;
this.context = context;
}
@Override
public ShopCartAdapter.ShopCartHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.sample_shop_cart, parent, false);
ShopCartAdapter.ShopCartHolder dataObjectHolder = new ShopCartAdapter.ShopCartHolder(view);
return dataObjectHolder;
}
@Override
public void onBindViewHolder(final ShopCartAdapter.ShopCartHolder holder, final int position) {
holder.title.setText(mDataset.get(position).title);
holder.price.setText(mDataset.get(position).price);
holder.number_order.setText(mDataset.get(position).number_order);
holder.id.setText(String.valueOf(mDataset.get(position).id));
holder.priceall.setText(mDataset.get(position).pricall);
Picasso.with(context)
.load(mDataset.get(position).img)
.placeholder(R.drawable.logolo)
.into(holder.img);
}
@Override
public int getItemCount() {
return mDataset.size();
}
public void delete(int position) { //removes the row
mDataset.remove(position);
notifyItemRemoved(position);
}