将数据从一个活动传递到另一活动的交互者 [英] Passing data from one activity to interactor of another activity

查看:41
本文介绍了将数据从一个活动传递到另一活动的交互者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发聊天应用程序.有聊天室,这些房间内有消息.当用户单击聊天室时,我想转到另一个显示消息的活动.

I'm developing a chat application. There are chatrooms and inside these rooms there are messages. When a user clicks on the chatroom I want to go to another activity where messages are displayed.

在我的适配器类中,我用 onBindViewHolder 编写了此 onclick()方法,通常在该方法中与所需数据一起使用.像这样:

In my adapter class, I have this onclick() method written in onBindViewHolder where I would normally make an intent along with the data I need. Something like this:

@Override
public void onBindViewHolder(@NonNull ChatRoomAdapter.ChatRoomViewHolder holder, final int position) {
    holder.mRoomTitle.setText(mChatRooms.get(position).getTitle());
    holder.mRoomDescription.setText(mChatRooms.get(position).getDescription());

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(this, NextActivity.java);
            intent.putExtra("test", mChatRooms.get(position).getTitle());
        }
    });
}

但是我正在尝试MVP架构设计,我想将 roomTitle 传递给下一个活动的Interactor/presenter类.我该如何实现?

But I'm trying the MVP architecture design and I want to pass roomTitle to the Interactor/presenter class of my next activity. How can I achieve this?

推荐答案

在RecyclerView适配器中,您需要在适配器中传递onItemClickListener.

In RecyclerView adapter you need to pass a onItemClickListener in the adapter.

请参阅Google的MVP示例-> https://github.com/googlesamples/android-architecture/tree/todo-mvp/

Refer to the Google's MVP sample - > https://github.com/googlesamples/android-architecture/tree/todo-mvp/

尤其是在TaskFragment中引用TaskItemListener.他们正在做您想要达到的目标.在此,他们从任务列表(recyclervView)中打开任务详细信息(新活动).

Especially refer the TaskItemListener in TaskFragment. They are doing the same thing what you are trying to achieve. In this they open Task details (new activity) from List of tasks(recyclervView).

/**
 * Listener for clicks on tasks in the ListView.
 */
TaskItemListener mItemListener = new TaskItemListener() {
    @Override
    public void onTaskClick(Task clickedTask) {
        mPresenter.openTaskDetails(clickedTask);
    }

    @Override
    public void onCompleteTaskClick(Task completedTask) {
        mPresenter.completeTask(completedTask);
    }

    @Override
    public void onActivateTaskClick(Task activatedTask) {
        mPresenter.activateTask(activatedTask);
    }
};

然后将其传递到Recycler视图的适配器

And then pass it to adapter of Recycler view

mListAdapter = new TasksAdapter(new ArrayList<Task>(0), mItemListener);

然后在项目上单击

rowView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mItemListener.onTaskClick(task);
            }
        });

这篇关于将数据从一个活动传递到另一活动的交互者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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