theroot
theroot

Reputation: 15

How to refresh ListView after deleting element?

I have created a listview but when i delete a element, it doesn't refresh. I have tried

 adapter.notifyDataSetChanged();

and

getListView().invalidateViews();

in mainactivity:

case R.id.ordini:

                    Ordini O = new Ordini();
                    android.app.FragmentManager fragmentManager0 = getFragmentManager();
                    android.app.FragmentTransaction fragmentTransaction0 = fragmentManager0
                            .beginTransaction();

                    fragmentTransaction0.replace(R.id.frame, O);
                    fragmentTransaction0.commit();

Can someone help me with listview refresh? I want refresh my listview in CustomAdapterOrdini or in onResume in Ordini.java.

public void onClick(View v) {

                // HERE I WANT REFRESH MY LISTVIEW

There is a simple way? Thank you so much!

Upvotes: 0

Views: 70

Answers (2)

Niranj Patel
Niranj Patel

Reputation: 33248

You have declare ArrayProdotto in your MainActivity and you are display your data from rowItem in CustomAdapterOrdini. You have to remove your data from both list and call notifyDataSetChanged();

Like as below code.

Del.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

         // HERE I WANT REFRESH MY LISTVIEW
         MainActivity.ArrayProdotto.remove(position); //Delete from main source
         rowItem.remove(position) // Delete from adapter
         CustomAdapter.ShowDialog("Elemento eliminato con successo!", context);
         notifyDataSetChanged(); // Refresh Adapter

    }
});

Upvotes: 1

Danila Grigorenko
Danila Grigorenko

Reputation: 423

1). Remove your item from adapter:

adapter.remove(adapter.getItem(position));

2). Call adapter.notifyDataSetChanged();

You should call notifyDataSetChanged() every time when you deleting item from adapter. Not only in onResume.

Note, that you should remove item from adapter, to change ListView items, not from storage(ArrayList, SQLite, or else).

Upvotes: 0

Related Questions