Reputation: 11
I have a fragment class containg a button and Listview, when the button is clicked a DialogFragment will appear ontop of the fragment class. It will take the users input and add it into a database, everything works however the listview does not update, I know I need to call adapter.notifyDataChange() I will write the psudeo code.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_notes, container, false);
add = (Button) view.findViewById(R.id.button1);
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//New DialogFragment show.dialog
}
});
filllist(view);
return view;
}
Another class, the creation of my DialogFragment
public Dialog onCreateDialog(Bundle savedInstanceState){
final AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
final View view = getActivity().getLayoutInflater().inflate(R.layout.custom_layout, null);
build.setView(view);
build.setPositiveButton(R.string.add, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
final String text = ((EditText) view.findViewById(R.id.randomtext)).getText().toString();
//Call insert method of database helper. pass user input
//dismiss
adapter.notifyDataSetChanged();
}
});
build.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dismiss();
}
});
return dialog.create();
}
When the user finishes filling in editText fields and presses add inserts data in database and dismisses dialogFragment but it does not update/refresh the listview, where can I call notifyDataSetChanged();
Upvotes: 1
Views: 3645
Reputation:
It's a simple solution. Everytime before calling notifyDataSetChanged()
you always have to update the list attached to adapter. For example if you have function naming setListData()
which fills the data for you in adapter list, you have call it before notifyDataSetChanged()
. (note setDataList() is just a blind example as you haven't shared this code, your code may vary)
As simple as that ! Hope it works ✌
Upvotes: 0
Reputation: 3746
You need to actually change backing data of your listview before you call notifyDataSetChanged
. For example, if you created your adapter as following:
mAdapter = new MyAdapter(context, R.layout.item_listview_layout, mData);
then you need to update your mData
field and only then call notifyDataSetChanged
.
Upvotes: 1