如何更新具有从Sqlite填充的Gridview的片段

问题描述:

我有一个带有两个选项卡的ViewPager,其中包含片段.在第一个片段中,我有一个Gridview,其中正在填充Sqlite Db.

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.

我的活动"中有一个自定义的Alertdialog,它是Fragments的父级.

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

alertdialog关闭时,它要么添加/删除/更新Sqlite Db:

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.
    }
});
...

如何更新以下内容:

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

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!