当用户长按一个listViews元素时,会出现一个新对话框,询问他是否要从数据库中删除该项目。如果是这样,那么在那个对话框片段onCreateDialog方法中,我从数据库中删除项目。这是问题:如何刷新主片段中的listView?
这是我的mainFragment函数来处理ListView:
public void handleNotes(final ListView notesListView){
if (database.getNoteCount() != 0) {
notesListView.setAdapter(new NoteAdapter(getActivity(), database.getAllNotes()));
notesListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
TextView textViewId = (TextView) view.findViewById(R.id.textViewId);
Toast.makeText(getActivity(), textViewId.getText().toString() + "", Toast.LENGTH_LONG).show();
DeleteNoteFragment newFragment = new DeleteNoteFragment(database, that, notesListView, Integer.parseInt(textViewId.getText().toString()));
newFragment.show(getActivity().getSupportFragmentManager(), "deleteConfirmation");
return false;
}
});
}
}
这是onCreateDialog:
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.dialog_delete_note)
.setPositiveButton(R.string.dialog_delete_confirm, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
database.removeNote(i);
mainFragment.handleNotes(notesListView);
Toast.makeText(getActivity(), "Note deleted successfully!", Toast.LENGTH_LONG).show();
}
})
.setNegativeButton(R.string.dialog_delete_denny, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
return builder.create();
}
答案 0 :(得分:0)
使用以下代码删除ListAdapter
中的项目。
adapter.remove(item);
然后使用以下代码与ListView
adapter.notifyDataSetChanged();