In general, I am now doing my shopping cart, and when removing a product, I ran into the problem of updating the window when the product was removed. In the main window there is a code that accepts info:

gvCart=(GridView)findViewById(R.id.gridView_Cart); final List<ItemCart> array_dynamic = new ArrayList<ItemCart>(); adapterCartBuy=new AdapterCartBuy(Cart.this,array_dynamic); gvCart.setAdapter(adapterCartBuy); String register = "http://wwwwwwwwww/cart"; StringRequest request = new StringRequest(Request.Method.GET, register, new Response.Listener<String>() { @Override public void onResponse(String response) { Log.d(TAG,"response"+ response); try { ObjectMapper mapper_main = new ObjectMapper(); PjCart pjCart = mapper_main.readValue(response, PjCart.class); for (PjCartItemsString pjCartItemsString : pjCart.getData().getItems()){ String image = pjCartItemsString.getImage(); itemCart.setImage(image); array_dynamic.add(itemCart); } Log.d(TAG,"pojCart"+ pjCart); }catch (IOException e){ e.printStackTrace(); }adapterCartBuy.notifyDataSetChanged(); 

But I twist the controls and the connection for deleting information in the adapter window, below:

 public class AdapterCartBuy extends BaseAdapter { final String TAG = "mylogs"; private LayoutInflater inflater; private Activity activity; private List<ItemCart> items; ImageLoader imageLoader=AppController.getmInstance().getmImageLoader(); public AdapterCartBuy(Activity activity, List<ItemCart> items){ this.activity=activity; this.items=items; } @Override public int getCount() { return items.size(); } @Override public Object getItem(int position) { return items.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { if(inflater==null){ inflater=(LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } if(convertView ==null){ convertView=inflater.inflate(R.layout.item_cart,null); } if(imageLoader==null) imageLoader=AppController.getmInstance().getmImageLoader(); NetworkImageView imageView= (NetworkImageView) convertView.findViewById(R.id.networkImageView_cart); ImageView del=(ImageView)convertView.findViewById(R.id.imageviewCartDel); final TextView textViewToken=(TextView)convertView.findViewById(R.id.textView_itemcart_token); textViewToken.setVisibility(View.GONE); final TextView itemId=(TextView)convertView.findViewById(R.id.textView_itemcart_id); itemId.setVisibility(View.GONE); del.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String del = "http://wwwwwwww/cart/"+itemId.getText().toString()+"/del"; StringRequest request = new StringRequest(Request.Method.GET, del, new Response.Listener<String>() { @Override public void onResponse(String response) { Log.d(TAG,"response "+ response); if (response.contains("true")){ ВОТ ТУТ НУЖНО ОБНОВИТЬ АДАПТЕР В ГЛАВНОМ ОКНЕ } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }) { @Override public Map<String, String> getHeaders() throws AuthFailureError { Map<String, String> params = new HashMap<String, String>(); params.put("Accept", "application/json"); params.put("Authorization", "Bearer "+textViewToken.getText().toString()); return params; } };AppController.getmInstance().addToRequesQueue(request); } }); //getting data for row final ItemCart item=items.get(position); imageView.setImageUrl(item.getImage(), imageLoader); textViewToken.setText(item.getToken()); itemId.setText(item.getItem_id()); return convertView; } 

I hope the problem is clear, and the question is how can I contact to update the adapter in the main class through a button listener in another, adapter class. Or how can it be different, correctly implement and not invent crutches.

  • ((Cart) activity).adapterCartBuy.notifyDataSetChanged(); - Serodv
  • unfortunately it doesn’t work, meanwhile you have to use rude activity.recreate () - Romik romikromik

1 answer 1

Your problem is that by updating the adapter you do not change the data it displays. Change the data first, then update the adapter.

 if (response.contains("true")){ //ВОТ ТУТ НУЖНО ОБНОВИТЬ АДАПТЕР В ГЛАВНОМ ОКНЕ final ItemCart item=items.get(position); //удаляем элемент из списка данных items.remove(item); //обновляем адаптер notifyDataSetChanged(); } 

Most likely, you will also need to override the equals method in the ItemCart class ItemCart

  • one
    Items are deleted, everything is fine, but the information is not updated, that is, if the product was 100 and it became 50, then the item with the price still remains 100. - Romik romikromik
  • one
    Thank you, I figured it out, just created a new connection and re-recorded all json. - Romik romikromik