简体   繁体   English

如何在另一个活动中从一个活动接收(放置)数据到recyclerview适配器

[英]How to receive (put) data from one activity to recyclerview adapter in another activity

I have an activity with tabs, in such tab - recyclerview. 我有一个带标签的活动,在这样的标签 - recyclerview中。 When i click on item in recyclerview, open another (detail) activity. 当我点击recyclerview中的项目时,打开另一个(详细)活动。 In detail activity i have "favorite" button, and i want to send boolean value in adapter. 详细活动我有“收藏”按钮,我想在适配器中发送布尔值。 My question is: how to pass data from detail activity to adapter of recyclerview. 我的问题是:如何将数据从详细活动传递到recyclerview的适配器。 Problem is in detail activity no instance of adapter, and i not set custom listener to adapter. 问题是详细的活动没有适配器的实例,我没有设置自定义监听器到适配器。

I tried to implement interface 我试着实现界面

public interface OnFavoriteListener {
 void changeIcon(int position, boolean favorite);
}

and Override method onPause in detail activity 和覆盖方法onPause详细活动

@Override
public void onPause() {
    super.onPause();
    if (mFavoriteListener != null) {
        mFavoriteListener.changeIcon(mPosition, mFavorite);            
    }
}

then implement listener in my adapter 然后在我的适配器中实现监听器

@Override
public void changeIcon(int position, boolean favorite) {        
    mResource.get(position).setFavorite(favorite);
}

and when i return from detail activity to recyclerview nothing happens 当我从详细活动返回到recyclerview时,没有任何反应

create an application class with YourAppName 使用YourAppName创建一个应用程序类

public class YourAppName extends Application {


private static YourAppName mInstance;
private static ArrayList<YourModel> source; //This is your mSourceList

 @Override
    public void onCreate() {
        super.onCreate();

        setmInstance(this); //this is a setter for mInstance value
}

 public static synchronized YourAppName getInstance() {
        return mInstance;
    }
     public static void setmInstance(YourAppName mInstance) {
        YourAppName.mInstance = mInstance;
    }

after you create this class in your app and define source value; 在应用程序中创建此类并定义源值后;

in your activity onCreate call this 在你的活动onCreate中调用它

YourAppName.getInstance().source = mSource;

and set your recyclerView adapter with YourAppName.getInstance().source not with mSource list; 并使用YourAppName.getInstance().source设置您的recyclerView适配器YourAppName.getInstance().source不使用mSource列表;

then go to your detailActivity onPause() 然后转到你的detailActivity onPause()

delete old interfaceMethod and write this 删除旧的interfaceMethod并写入此内容

YourAppName.getInstance.source.get(position).setFavorite(favorite);

when you return your Activity call adapter.notifySetDataChanged() in onResume(); 当你在onResume();返回你的Activity调用adapter.notifySetDataChanged() onResume();

it will work for you. 它会对你有用。

First, assume, when your "detail activity" popout, you click "favorite" to set a boolean value to "true". 首先,假设当您的“详细活动”弹出时,单击“收藏夹”将布尔值设置为“true”。 so after that, you will close your "detail activity", so when you click "favorite", you should get your recyclerView in your "main activity" and call notifyItemChanged() to set your value to "true" in your data source. 所以在那之后,你将关闭你的“详细活动”,所以当你点击“收藏”时,你应该在你的“主要活动”中获得recyclerView并调用notifyItemChanged()在你的数据源中将你的值设置为“true”。 You can write a "get()" method to get your recyclerView in your "main activity". 您可以编写“get()”方法,在“主要活动”中获取recyclerView。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将数据从一个活动返回到另一个活动,并在RecyclerView适配器中使用该数据 - Returning data from an activity to another activity and using that data in RecyclerView adapter 如何从recyclerView接收JSON数据到另一个活动 - How to receive JSON data from recyclerView to another activity 从一个活动(点击按钮)传递数据,在不启动活动的情况下添加到另一个活动的 recyclerView 适配器 - Passing data from an activity (On Button clicked), to be added to a recyclerView adapter at another activity, without starting the activity 如何从一个活动到另一个活动接收数据-Android - How to receive data from one activity to another - android 如何将值从一个活动传递到另一个活动/适配器 - How to Pass a Value from One Activity to Another Activity/Adapter 如何从 recyclerView 适配器访问另一个活动的 XML? - How to access another activity's XML from recyclerView adapter? 如何将图像从recyclerview适配器传递到另一个活动 - how can pass image from recyclerview adapter to another activity 如何更新来自其他活动的Recyclerview数据 - How to update Recyclerview data from another activity 如何使用Firebase适配器通过CardView将数据从Recyclerview传递到另一个活动 - How to pass data to another activity from recyclerview with cardview using firebase adapter 将数据从活动传递回recyclerview适配器 - Passing data from an activity back to recyclerview adapter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM