Ilja Tarasovs
Ilja Tarasovs

Reputation: 211

how to call notifyDataSetChanged after deleting listItem

My fragment class:

        ServicesListInNewOrderAdapter adapter = new ServicesListInNewOrderAdapter(getActivity(),
                R.layout.adapter_view_services_list_in_new_order , servicesArrayList);
        servicesListView.setAdapter(adapter);

In ServicesListInNewOrderAdapter class:

    class ServicesListInNewOrderAdapter extends ArrayAdapter<ServicesList>
    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {

    deleteServiceBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int id = servicesList.get(position).getId();
                servicesList.remove(position);
                //MARK
                Toast.makeText(mContext, "pressed: " + id, Toast.LENGTH_SHORT).show();

            }
        });
    }

How to call adapter.notifyDataSetChanged() in MARK? First idea is to create interface in ServicesListInNewOrderAdapter class.

    public interface ServicesListAdapterListener {
        void updateServicesListAfterDeleting(ServicesList servicesList);
    }  

But it won't work because I don't have method onAttach in my adapter. How to solve?
I ran into another problem:enter image description here Red button is deleteServiceBtn on btn press item disappears but total sum no recounted. Field Total sum(Netto): and other fields below is from first class. And this is code how I calculate total sum:

        adapter = new ServicesListInNewOrderAdapter(getActivity(), R.layout.adapter_view_services_list_in_new_order , servicesArrayList);
        servicesListView.setAdapter(adapter);

        int sum = 0;
        for (int i = 0; i < servicesArrayList.size(); i++) {
          sum += servicesArrayList.get(i).getServiceTotalPrice();
        }
        totalPriceForAllServices.setText(String.valueOf(sum));

How to update totalPriceForAllServices.setText from my ServicesListInNewOrderAdapter class?

Upvotes: 0

Views: 48

Answers (3)

archana
archana

Reputation: 1

For particular item position want to change to call notifyItemRemoved(item) You want to change all list item notifyDataSetChanged();

Upvotes: 0

apksherlock
apksherlock

Reputation: 8371

There is another operator called notifyItemRemoved() in case of deletion from RecyclerView but please make sure you have deleted the item from data source also.

 int id = servicesList.get(position).getId();
 servicesList.remove(position);
 notifyDataSetChange()
 Toast.makeText(mContext, "pressed: " + id, Toast.LENGTH_SHORT).show();

Edit: Since you have extended ArrayAdapter there is no notifyItemRemoved() but you can use remove(T object) method described here.

Upvotes: 1

Mahdi-Malv
Mahdi-Malv

Reputation: 19240

To notify the list adapter that the dataset has been modified, you can do:

((ServicesListInNewOrderAdapter) listView.getAdapter()).notifyDataSetChanged()

In case of remove you can use notifyItemRemoved(item) instead of notifyDataSetChanged()

Upvotes: 0

Related Questions