Reputation: 2028
Hi am using a ListFragment where I add a contact from my SQLite database. I then want to refresh the ListFragment but it does not work.
This is the code inside my Fragment to display an AlertDialog after clicking on a item in the ActionBar to add an contact to the ListFragment:
//Handle OnClick events on ActionBar items
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// handle item selection
switch (item.getItemId()) {
case R.id.menu_add:
Toast.makeText(getActivity(), "Click", Toast.LENGTH_SHORT).show();
LayoutInflater factory = LayoutInflater.from(getActivity());
//textEntryView is an Layout XML file containing text field to display in alert dialog
textEntryView = factory.inflate(R.layout.dialog_add_room, null);
//get the control from the layout
enter_room = (EditText) textEntryView.findViewById(R.id.enter_room);
//create Dialog
final AlertDialog.Builder alert1 = new AlertDialog.Builder(getActivity());
//configure dialog
alert1.setTitle("Raum hinzufügen:").setView(textEntryView)
.setPositiveButton("Hinzufügen",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
String roomname = enter_room.getText().toString();
Log.d("Insert: ", "Inserting ..");
dbHandler.addContact(new Contact(roomname, "0"));
adapter.notifyDataSetChanged();
}
}).setNegativeButton("Abbrechen",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
//cancel dialog
}
});
alert1.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
The adapter defined inside the OnCreate() method:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//display ActionBar items
setHasOptionsMenu(true);
//database
dbHandler = new DatabaseHandler(getActivity());
//get all contacts
contacts = dbHandler.getAllContacts();
adapter = new ArrayAdapter<Contact>(getActivity(),
android.R.layout.simple_list_item_activated_1,
android.R.id.text1, contacts);
adapter.setNotifyOnChange(true);
//show all contacts in the ListFragment
setListAdapter(adapter);
}
What is the problem?
Upvotes: 0
Views: 2122
Reputation: 15280
Your onCreate
code uses your DatabaseHandler
class to load all the contacts from a database into an array or List
of some kind. Later, your click handler adds another contact to the database: but it doesn't change your array. The ArrayAdapter
only knows about the array you gave it, not the database, so adapter.notifyDataSetChanged();
has no effect.
There are two approaches you could take.
You could replace your ArrayAdapter
and the set-up code with a Loader
. The easiest way to do this is to access your database via a ContentProvider
, which means you can use a CursorLoader
and it will deal with updating your adapter when the database changes, even if the change comes from a Service
or another Activity
. You can still use a Loader
even if you don't want the overhead of a ContentProvider
, but then less of the work is done for you. Either way, a Loader
gives you the advantage of performing the load in a background thread, so you don't get an "app not responding" when you have lots of contacts. See the loaders guide on Android Developers for more information.
A quicker, short-term fix for this problem is to edit the array you gave to the adapter at the same time as updating the database. You could do this by replacing the line
dbHandler.addContact(new Contact(roomname, "0"));
with
Contact newContact = new Contact(roomname, "0");
dbHandler.addContact(newContact);
contacts.add(newContact);
This way, you'll have to do something similar when you change or remove a contact, and it won't update if your database is changed from outside your ListFragment
. You'll probably find you can keep adding these extra calls for a while, but eventually you'll come up against some operation where you have to do it the other way to make it work.
Upvotes: 1