您好我正在使用ListFragment,其中我從我的SQLite數據庫添加聯繫人。然後我想刷新ListFragment,但它不起作用。Android:ListFragment不刷新
這是我的片段點擊一個項目ActionBar中後,以顯示AlertDialog添加一個接觸到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);
}
}
的onCreate()方法內定義的適配器:
@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);
}
什麼問題?
非常感謝您的詳細解答。我將嘗試實現一個ContentProvider。這看起來像是我眼中最好的解決方案。由於我從未使用ContentProvider,因此可能需要一些時間才能實現。 – 2013-03-24 08:30:41