gpt4 book ai didi

java - 如何从 RecyclerView Adapter 中的 Sharedpreferences 获取数据并传递到下一个 fragment

转载 作者:行者123 更新时间:2023-12-02 10:26:31 25 4
gpt4 key购买 nike

尝试将共享首选项中的数据获取到 recylerview 适配器中,但我收到 context = null。我还在适配器中初始化了上下文。我搜索了很多,但没有任何帮助。任何教程或任何帮助将不胜感激

这是我的适配器:-

    public class HomeFeaturedAdapter extends RecyclerView.Adapter<HomeFeaturedAdapter.HomeViewHolder> {

Context context;

public HomeFeaturedAdapter(List<Products> productList) {

this.context = context;
}



@NonNull
@Override
public HomeViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.brand_adapter_row, parent, false);
return new HomeViewHolder(itemView);
}

@Override
public void onBindViewHolder(@NonNull HomeViewHolder homeViewHolder, int i) {

SharedPreferences preferences = context.getSharedPreferences("MyPref", MODE_PRIVATE);
String imageload = preferences.getString("permalink", null);

/*SharedPreferences sharedPrefs = ((HomeViewHolder) homeViewHolder).imageload.getContext().getSharedPreferences("MyData", Context.MODE_PRIVATE);*/

/*SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainActivity.context);
String imageload = sharedPreferences.getString("permalink", null);*/


if (imageload != null) {

Picasso.with(context).load(imageload).fit().into(homeViewHolder.imagesViews);

}

/*
homeViewHolder.view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

AppCompatActivity activity = (AppCompatActivity) view.getContext();
Fragment myFragment = new ProductDetailsFragment();
activity.getSupportFragmentManager().beginTransaction().replace(R.id.mainfraime, myFragment).addToBackStack(null).commit();
}
});
*/

}

@Override
public int getItemCount() {
return 10;
}

public class HomeViewHolder extends RecyclerView.ViewHolder {

public View view;
TextView productTitle;
ImageView imagesViews;
CardView brandCardview;

public HomeViewHolder(@NonNull View itemView) {
super(itemView);
this.view = itemView;

productTitle = itemView.findViewById(R.id.productTitle);
imagesViews = itemView.findViewById(R.id.imagesViews);
brandCardview = itemView.findViewById(R.id.brandCardview);
}
}
}

如果我像这样在构造函数中添加上下文

` 

public HomeFeaturedAdapter(Context context, List<Products> productList) {

this.context = context;
}

`

我的 Home Fragment 错误:类 HomeFeaturedAdapter 中的构造函数 HomeFeaturedAdapter 无法应用于给定类型;
HomeFeaturedAdapter 适配器 = new HomeFeaturedAdapter(productList);

请帮助我找到一个解决方案,我在谷歌上尝试了很多,但不明白该怎么做。

最佳答案

您已将 Context 添加到适配器构造函数中,因此当您从 fragment 或 Activity 中调用此构造函数时,您需要添加上下文。如果您从 Activity 调用中调用它

 HomeFeaturedAdapter adapter = new HomeFeaturedAdapter(this, productList)

您可以从 Fragment 中调用

HomeFeaturedAdapter adapter = new HomeFeaturedAdapter(getActivity(), productList)

关于java - 如何从 RecyclerView Adapter 中的 Sharedpreferences 获取数据并传递到下一个 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53920940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com