I'm working on an application where populo a ListActivity
using data of a SQLite
created by the program itself. When you add items to the database, the list is updated automatically, but when you remove it, the list is not updated.
I insert new items into a Activity
separate from the main, but at the time of deleting items, I do the same within a DialogFragment
called within ListActivity
. Here's my code:
package activities;
import adapters.ItemListAdapter;
import android.app.FragmentManager;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import br.bravosix.historico.R;
import classes.Database;
import classes.ListItem;
import fragments.QuickViewFragment;
public class ActivityList extends ListActivity {
ItemListAdapter adapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// carrega os itens salvos no banco de dados quando
// a tela é criada
loadItems();
}
// re-carrega os itens após o resumo da activity
public void onResume() {
super.onResume();
loadItems();
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_list_items, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.menu_list_add) {
// activity responsável por adicionar novos itens
// ao banco de dados
Intent addItem = new Intent(this, ActivityNewItem.class);
startActivity(addItem);
return true;
}
return super.onOptionsItemSelected(item);
}
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
ListItem item = (ListItem) l.getItemAtPosition(position);
FragmentManager fm = getFragmentManager();
QuickViewFragment quickView = QuickViewFragment.newInstance(item);
quickView.show(fm, "tag_quick_view");
// testei o notifyDataSetChanged aqui e não funcionou, o mesmo
// ocorreu quando usei a função loadItems();
adapter.notifyDataSetChanged();
}
public void loadItems() {
Database db = new Database(this);
adapter = new ItemListAdapter(this, db.readItems());
setListAdapter(adapter);
}
}
What do I need to do so that when QuickViewFragment
is closed, the list is updated?