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

时间:2016-10-17 21:19:56

标签: android sqlite android-fragments

我有一个带有两个标签的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.

3 个答案:

答案 0 :(得分:1)

您可以将Intents and Intent FiltersBroadcast Receiver一起使用  对此。

  1. 在要更新数据的片段中创建BroadcastReceiver实例。
  2. 创建一个IntentFilter并为其设置一个操作字符串(可能是'db.update')并将其注册到您的应用程序上下文中(您可以通过调用 getActivity()。getApplicationContext()来实现此操作.registerReceiver (接收者,过滤器)。
  3. 在AlertDialog中,更新数据库后,使用您在上面设置的相同操作字符串(在我们的示例中为“db.update”)创建一个Intent,并使用上下文将其发送出去( getActivity()。 getApplicationContext()。sendBroadcast(意图))。您的BroadcastReceiver的 onReceive()方法将在您的片段中调用,您可以调用该方法刷新或重新加载您的数据。请参阅以下示例代码:
  4. 说这是你的片段

    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...

每次关闭对话框并完成诀窍......简单易行!