porthfind
porthfind

Reputation: 1619

Android: can't refresh my listview

When I try to delete an item in a listview it does not update then the listview. I have followed and read some similar questions here in stackoverflow but I had no results. Can you please help me?

I have an Activity Ver where I have an onResume() method:

public class Ver_DespesasActivity extends ActionBarActivity {

private ListView listView ;
private ArrayAdapter dataAdapter;
private DatabaseHelper databaseHelper ;
private List<Despesa> despesas;
ArrayList<Despesa>items;
MyAdapter adapter;

(...)
  @Override
  public void onResume() {
            super.onResume();
            databaseHelper= new DatabaseHelper(this.getApplicationContext());
            Gasto gasto = databaseHelper.getGasto();
            String mes = gasto.getMes();
            int ano = gasto.getAno();

            items.clear();
            items.addAll(databaseHelper.getAllDespesas(mes, ano));
            adapter.refresh(items);
    }

Then I have a class MyAdapter where I have the refresh() method:

public class MyAdapter extends BaseAdapter implements ListAdapter {

    private ArrayList<Despesa> list = new ArrayList<Despesa>();
    private Context context;
(...)
public void refresh(ArrayList<Despesa> list)
    {
        this.list = list;
        this.notifyDataSetChanged();
    }

MyAdapterConstructor:

public MyAdapter(ArrayList list, Context context) {
        this.list = list;
        this.context = context;
    }

Update: I don't know if this is important, I'm not using list.remove to remove the items of my listview, I have a method mine to do it.

Upvotes: 4

Views: 217

Answers (1)

tachyonflux
tachyonflux

Reputation: 20211

Since the adapter is already using your items list, you don't need your refresh method. You can just modify the items as you are doing and directly notifyDataSetChanged.

items.clear();
items.addAll(databaseHelper.getAllDespesas(mes, ano));
adapter.notifyDataSetChanged();

Upvotes: 3

Related Questions