Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 组件间通信接口的替代方案_Java_Android - Fatal编程技术网

Java 组件间通信接口的替代方案

Java 组件间通信接口的替代方案,java,android,Java,Android,我想调用notifyDataSetChanged(),但我的代码结构一团糟,无法解决这个问题。我有一个片段、一个自定义对话框类和MainActivity MainActivity.java Fragment is created here along with 3 other fragments. Dialog is also created here private BottomNavigationView.OnNavigationItemSelectedListener mOnNaviga

我想调用
notifyDataSetChanged()
,但我的代码结构一团糟,无法解决这个问题。我有一个片段、一个自定义对话框类和MainActivity

MainActivity.java

Fragment is created here along with 3 other fragments. Dialog is also created here

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
                = new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.navigation_home:
                        fragment = new FragmentHome();
                        loadFragment(fragment);
                        loadCustomActionBar("Home", false);
                        return true;
                    case R.id.navigation_foods:
                        fragment = new FragmentFood();
                        loadFragment(fragment);
                        loadCustomActionBar("Foods", true);
                        return true;
                    case R.id.navigation_drinks:
                        fragment = new FragmentDrink();
                        loadFragment(fragment);
                        loadCustomActionBar("Beverages", true);
                        return true;
                    case R.id.navigation_cart:
                        fragment = new FragmentCart();
                        loadFragment(fragment);
                        loadCustomActionBar("Cart", false);
                        return true;
                }
                return false;
            }
        };

private void loadCustomActionBar(String titleTxt, boolean bool) {
        CustomDialog dialog = new CustomDialog()
         .....
    }
ListAdapter is located here
自定义对话框

CustomDialog is responsible for updating database. 
I want to call notifyDataSetChanged() here after updating database to update view as well.

Button addItem = .....
addItem.setOnClickListener(new OnClickListener() {
    public void onClick() {
         dialog.show();
    }
});
Fragment.java

Fragment is created here along with 3 other fragments. Dialog is also created here

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
                = new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.navigation_home:
                        fragment = new FragmentHome();
                        loadFragment(fragment);
                        loadCustomActionBar("Home", false);
                        return true;
                    case R.id.navigation_foods:
                        fragment = new FragmentFood();
                        loadFragment(fragment);
                        loadCustomActionBar("Foods", true);
                        return true;
                    case R.id.navigation_drinks:
                        fragment = new FragmentDrink();
                        loadFragment(fragment);
                        loadCustomActionBar("Beverages", true);
                        return true;
                    case R.id.navigation_cart:
                        fragment = new FragmentCart();
                        loadFragment(fragment);
                        loadCustomActionBar("Cart", false);
                        return true;
                }
                return false;
            }
        };

private void loadCustomActionBar(String titleTxt, boolean bool) {
        CustomDialog dialog = new CustomDialog()
         .....
    }
ListAdapter is located here
我尝试过使用接口,但不是我做得不对,就是不起作用。请帮忙。接口的其他替代方案是什么

编辑:
看来你不明白我的问题。我只想从我的自定义对话框“本网站的人”中访问片段中包含的LISTADAPTER。

正如我在代码中看到的,您有一个可变片段。对于您的情况,您可以定义一些BaseFragment类,其中包含抽象方法datasetChanged()。所有单独的片段都是BaseFragment的衍生物,变量片段的类型是BaseFragment。派生片段将实现函数datasetChanged(),在该函数中,它们将为自己的适配器调用notifyDatasetChanged

然后您可以在CustomDialog中有一个回调,它将调用变量片段的datasetChanged

然后,在创建CustomDialog时,只需将回调设置为片段方法datasetChanged():

之后,当customDialog需要通知更新时,它将调用回调函数

notifyCallback.apply()

实现这一点有多种方法

  • 将片段实例发送到CustomDialog,如
    CustomDialog=new CustomDialog(片段)
    。现在CustomDialog已经引用了片段,您可以在片段中调用一个方法,该方法将从db中获取新数据并更新其视图

  • 使用EventBus库。您可以将片段注册为EventBus的侦听器,并实现
    onEventMainThread(YourEventName事件)
    。CustomDialog会在更新数据库后将YourEventName事件发布到EventBus。EventBus将此事件传递到片段,因为它已注册为侦听YourEventName

  • 尝试刷新片段的onResume中的视图。在对话框和片段不能同时访问的情况下,这将起作用


  • 简而言之,当CustomDialog更改数据库中的数据时,您希望片段更新其视图?是的,就是这样……您的no.1无法工作。我有4个片段,它们都是不同的。你的第三个问题是我的问题,视图只有在我更改选项卡时才会更新。我不熟悉你的2号,你想更新一个自定义对话框中的所有4个片段吗?是的,一个对话框。但是更新所有的4个片段。对吗?不,只有一个片段那么我看解决方案1没有任何问题。将片段分配给新的自定义片段后,您将调用loadCustomActionBar,它将创建一个新的CustomDialog。所以每次调用loadCustomActionBar时,都会出现一个新的CustomDialog,其中包含对当前片段的引用真的吗?告诉我你什么时候能找到你的银弹。