我有一个带有两个标签的ViewPager,它们包含片段。在第一个片段中,我有一个Gridview,其中填充了Sqlite Db。
我的Activity中有一个自定义alertdialog,它是Fragments的父级。
当alertdialog关闭时,它会添加/删除/更新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.
答案 0 :(得分:1)
您可以将Intents and Intent Filters与Broadcast Receiver一起使用 对此。
说这是你的片段
public class YourFragment extends Fragment {
private GridView mGridView;
private BaseAdapter mAdapter;
private BroadcastReceiver mReceiver;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//inflate view as usual
View view = inflater.inflate(R.layout.yourlayour, container, false);
...
//create instance of broadcast receiver
mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) { //when intent is receiver, this method is called
if(intent.getAction().contentEquals("db.update")){
//update intent received, call method to refresh your content loader
refreshFragment();
}
}
};
//create a new intent filter
IntentFilter mDataUpdateFilter = new IntentFilter("db.update");
//register our broadcast receiver and intent filter
getActivity().getApplicationContext().registerReceiver(mReceiver, mDataUpdateFilter);
return view;
}
@Override
public void onDestroy() {
super.onDestroy();
//never forget to unregister the receiver when you're done, it could cause your app to crash
//if it receives an intent and calls null pointing methods in your code
getActivity().getApplicationContext().unregisterReceiver(mReceiver);
} }
然后如上所述在AlertDialog中,通过以下方式将意图发送给此接收者:
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
//Create an intent with our action
Intent updateIntent = new Intent("db.update");
//send the intent by
getContext().getApplicationContext().sendBroadcast(updateIntent);
dialog.dismiss();
}
});
...
当碎片被破坏时,不要忘记取消注册广播接收器。在 onDestroy()方法中调用 getActivity()。getApplicationContext()。unregisterReceiver(receiver); 。
我还应该指出,即使你从后台线程发送你的意图,也总会在主线程上调用广播接收器的 onReceive()方法。
答案 1 :(得分:0)
这是我用来访问ViewPager中的Fragments的技巧 在我的自定义viewPagerAdapter中,我添加了两个方法
public class CustomViewPagerAdapter extends FragmentPagerAdapter {
.....
private String getFragmentTag(int viewId, int index) {
return "android:switcher:" + viewId + ":" + index;
}
//mFragManager is a reference to FragmentManager
public Fragment getFragmentByTag(int containerId, int position) {
return mFragManager.findFragmentByTag(getFragmentTag(containerId, position));
}
}
然后在您的活动中或您可以访问customViewPager的任何地方
YourFragment frag = (YourFragment) customViewPager
.getFragmentByTag(YouViewPager.getId(), position);
之后,在YourFragment refreshData中添加一个方法(如果你喜欢这个名字!)并在其中刷新网格 希望这个帮助
答案 2 :(得分:0)
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(frag).attach(frag).commit(); //frag is my Fragment instance...
每次关闭对话框并完成诀窍......简单易行!