如何在片段活动和非父活动之间传输数据? [英] How to transfer data between fragment and non parent activity?

查看:67
本文介绍了如何在片段活动和非父活动之间传输数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很努力地解决这个问题

I am very struggling to solve this problem

我正在监听对话列表片段中的数据

I am listening data from conversation list fragment

现在如何在进行聊天时在聊天活动中发送接收到的数据?请帮忙.

Now how to send received data in chatting activity when chatting is going on? Please help.

推荐答案

如果我正确地理解了这个问题,那么这似乎是EventBus的一项工作.在这里看看如何实现它: http://square.github.io/otto/

If I undestood the problem correctly, this seems like a job for an EventBus. Have a look on how to implement it here : http://square.github.io/otto/

首先您需要定义一个事件

First of all you need to define a event

public class ChatMessage{
   private String status,message,sender;
   //with constructors and toString
}

在您的聊天"活动中,您将事件发布在事件总线上,诸如此类

In your Chatting activity you post the event on the event bus, something like this

EventBus.getDefault().post(new ChatMessage(status,message,sender)

在对话"列表中(假设您使用适配器管理的List或Recyclerview进行显示),使适配器知道您的事件总线

In your Conversation list (assuming you're displaying with a List or Recyclerview managed by an adapter) , make the adapter aware of your event bus

@Subscribe(threadMode = ThreadMode.MAIN)
public void updateAdapter(ChatMessage message) {
    //here you should get the chat item and update it
    //do not forget to call notifyDatasetChanged() at the end to update your adapter
}

在您希望更新会话列表片段的任何时间(例如,当消息传入时,当您向联系人发送消息时)都将消息发布到EventBus

Post a message to the EventBus at anytime you want the Conversation list fragment to be updated(eg. when a message is incoming , when you are sending a message to a contact)

如果您可以通过片段/活动和适配器来提供其他代码,我将更新我的答案.

If you can provide additional code form your fragments/activities and form the adapter I'll update my answer.

这篇关于如何在片段活动和非父活动之间传输数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆