在onclick按钮之后更新RecyclerView

时间:2018-07-04 07:22:49

标签: android android-recyclerview recycler-adapter

我有一个购物车 在此购物车中,我想单击加号按钮 我的RecyclerView将被升级

在此购物车中,我想单击地雷按钮 我的RecyclerView将被升级

然后还单击删除按钮以从RecyclerView中删除该项目 非常感谢您的帮助

代码

public class ShopCartAdapter extends RecyclerView.Adapter<ShopCartAdapter.ShopCartHolder> {
List<ShopCartGet> mDataset;
Context context;
 String email,password;
List<ShopCartGet> list;
ShopCartAdapter adapter;

public class ShopCartHolder extends RecyclerView.ViewHolder {
    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 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 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);
    }

    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);
        plus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view ) {
                increment();        
                update();
            }
        });       
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                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);   
}
@Override
public int getItemCount() { 
    return mDataset.size();
}}

结果:

enter image description here

2 个答案:

答案 0 :(得分:0)

尝试此代码。 使界面处理加号和减号单击事件,如下所示,您将创建两个接口来处理加号和减号。 将接口定义为适配器类。

   OnItemClick onItemClick;

public void setOnItemClick(OnItemClick onItemClick) {
    this.onItemClick = onItemClick;
}

public interface OnItemClick {
    void getPosition(String data); //pass any things
}

单击事件后进入onBind方法。

@Override
public void onBindViewHolder(ItemViewHolder holder, final int position) {
    // below code handle click event on recycler view item.
    final String data = mStringList.get(position);
    holder.textView.setText(data);
    holder.pulshButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            onItemClick.getPosition(data);
        }
    });
}

该适配器绑定到recyclerview之后,这意味着适配器不为null,然后调用以下代码。

        adpater.setOnItemClick(new RecyclerViewAdpater.OnItemClick() {
        @Override
        public void getPosition(String data) {
            Log.d("Data of Click::",data);
            adpater.notifyDataSetChanged();
        }
    });

答案 1 :(得分:0)

ShopCartGet customModal; 列表列表;

  public void updateView(int index, String data){
        customModal = list.get(index);
        customModal.setStatus(data);
        notifyItemRangeChanged(index, getItemCount() - index);
        notifyDataSetChanged();
    }