繁体   English   中英

如何从“共享首选项”中获取数据到RecylerView Adapter中并传递到下一个片段

[英]How to get data from Shared Preferences into RecylerView Adapter and pass to next fragment

试图从共享首选项获取数据到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 error: constructor HomeFeaturedAdapter in class HomeFeaturedAdapter cannot be applied to given types; HomeFeaturedAdapter adapter = new HomeFeaturedAdapter(productList); error: constructor HomeFeaturedAdapter in class HomeFeaturedAdapter cannot be applied to given types; HomeFeaturedAdapter adapter = new HomeFeaturedAdapter(productList);

请帮助我找到我在Google上尝试了很多但不了解该怎么做的解决方案。

您已经将Context添加到了适配器构造函数中,因此,当您从片段或活动中调用此构造函数时,您需要添加上下文。 如果您通过“活动”电话进行调用

 HomeFeaturedAdapter adapter = new HomeFeaturedAdapter(this, productList)

您可以从Fragment呼叫

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

您使用了错误的构造函数来创建适配器的对象。您必须使用此适配器

HomeFeaturedAdapter adapter = new HomeFeaturedAdapter(getContext(),productList);

您在适配器中使用多个构造函数,因此您在使用一个没有context构造函数,因此您的context在适配器context为null。

希望这会帮助你。 这是我已经尝试过的正确代码!

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

Context context;

public HomeFeaturedAdapter (Context context, List<Products> productList) {
    this.context = context;
    SharedPreferences preferences = context.getSharedPreferences("MyPref", MODE_PRIVATE);
}

@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) {

    String imageload = preferences.getString("permalink", null);
    if (imageload != null) {
        Picasso.with(context).load(imageload).fit().into(homeViewHolder.imagesViews);

    }
}

@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);
    }
}

}

在你的片段中做到这一点

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM