【问题标题】:Pass Data From Activity To Fragment Using Interface使用接口将数据从 Activity 传递到 Fragment
【发布时间】:2019-01-15 15:49:23
【问题描述】:

使用我的应用程序时,我有一个 Picker 控件(在 Fragment 中),当用户单击它时,将显示一个新的 Menu Activity,其中包含一些 Items 列表,当用户单击任何项​​目时,Activity 将完成,并且界面应传输所选的 Item 字符串再次到 Fragment 中的 Picker,

这是我的菜单活动:

public class Menu extends AppCompatActivity {

    ListView listView;


    public interface ItemListener {
        void getItem(String s);
    }


    ItemListener itemListener;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);

        // Error Goes Here

        itemListener = (ItemListener) this;

        // Define and fill the list view

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                itemListener.getItem("Some Item");
                finish();

            }
        });
    }
}

还有我的片段:

public class Addorder extends Fragment implements Menu.ItemListener {


    Picker picker;



    public Addorder() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_addorder, container, false);

        picker = (Picker) view.findViewById(R.id.picker);


        return view;
    }


    @Override
    public void getItem(String s) {
        picker.setChosenItem(s);
    }
}

但是我遇到了铸造错误:

Caused by: java.lang.ClassCastException: Menu cannot be cast to Menu$ItemListener

我错了,任何帮助将不胜感激

【问题讨论】:

  • 您的Activity 没有实现您的接口。您还需要在Activity 中实现它。

标签: android interface


【解决方案1】:

在我看来,听众是没用的。在Addorder 中,您应该使用startActivityForResult() 打开菜单活动。在Menu 中设置结果后,您将在片段的onActivityResult 回调中收到它。这是如何做到这一点的示例:How to manage startActivityForResult on Android?

【讨论】:

  • 谢谢你,完美的解决方案
【解决方案2】:

从您的 cmets 现在更清楚您要做什么。 Fragment 很大程度上依赖于Activity,它实际上不能独立,任何接口实现都会在onAttach() 期间被捕获,Fragment 的上下文是Activity 所以如果Activity 没有实现接口崩溃将发生在 Fragment 中的onAttach()

但在您的场景中,您有两个不同的Activities,因此在这种情况下,您可以使用BundleBroadcastReceiver 传递数据。要使用BroadcastReciver,请在Fragment 中创建一个方法:

private void registerReciver() {
BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent != null) {
                String getData = intent.getStringExtra("data");
            }
    }
};
IntentFilter intentFilter = new IntentFilter("action");
this.registerReceiver(receiver, intentFilter);

}

在您的片段onCreate() 方法中调用registerReciver();。现在从第二个Activity 关闭它,触发BroadcastReceiver 发送一些数据:

                        Intent intent = new Intent();
                        intent.putExtra("data", "some data");
                        intent.setAction("action");
                        this.sendBroadcast(intent);

在您的Fragment 覆盖onResume 方法中应该也可以使用的第二种方法将在您的Activity 第二个Activity 完成后调用。在Activity 中将数据放入Bundle 中,例如:

Addorder fragment = new Addorder();
Bundle bundle = new Bundle();
bundle.putString("data", "Some data");
fragment.setArguments(bundle);

比在Fragment里​​面onResume()方法获取数据:

Bundle bundle = this.getArguments();
if (bundle != null) { //to prevent crash must check against null
    String getData = bundle.getString("data", defaultValue);
 }

我认为这两种方法都应该有效

【讨论】:

  • 为什么我的活动应该实现监听器?选择器控件在 Fragment 中而不是在活动中
  • 两个解决方案都抛出错误并且不起作用!第一个解决方案:涉及菜单的循环继承,第二个解决方案:无法将 Main 强制转换为 Menu
  • Fragment 很大程度上依赖于Activity,它实际上不能独立,任何接口实现都会在onAttach() 期间被捕获,Fragment 的上下文是Activity 所以如果那样的话Activity 未实现 interface 崩溃将发生在 onAttach() 中的 Fragment 中。我已经编辑了代码。错误在 implements 删除 Menu
  • 效果不佳,请更清楚我的片段父级是主活动(BottomNavigationView 中的片段),选择器在片段中,菜单活动是独立活动,我想从添加订单片段的菜单活动
  • 附加在哪个Activity 片段上?这是主要问题,如果您的Fragment 附加在MainActivity 上,那么您需要在MainActivity 中实现接口。如果您尝试将数据从一个Activity 传递到附加到另一个ActivityFragment,那么这个带有接口的部分将不起作用,您不能这样传递数据。这就是你想要做的吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-07
  • 1970-01-01
  • 2015-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-23
相关资源
最近更新 更多