Si8 Si8 - 1 month ago 10
Android Question

How to update a Fragment that has a Gridview populated from Sqlite

I have a ViewPager with two tabs which holds fragment. Inside the first fragment, I have a Gridview which is being populated with Sqlite Db.

I have an custom alertdialog in my Activity, which is the parent of the Fragments.

When the alertdialog closes, it either adds/removes/updates the Sqlite Db:

DataBaseHelper dbh = DataBaseHelper(this);
...
positiveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dbh.addVal(new TheData(editName.getText().toString(), editAge.getText().toString())); //adds a row to the Sqlite Db
dialog.dismiss();
//on dismiss, refresh the Fragment, which in turn will display the updated GridView.
}
});
...


How can I update complete the following:

//on dismiss, refresh the Fragment, which in turn will display the updated GridView.

Si8 Si8
Answer
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(frag).attach(frag).commit(); //frag is my Fragment instance...

Each time the dialog closed and it did the trick... simple and easy!

Comments