[英]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.