简体   繁体   中英

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

Trying to get data from shared preference into recylerview adapter but I'm getting context = null . I also initialized context in the adapter. I searched a lot but nothing helped me. Any tutorial or any help will be appreciated

here is My adapter:-

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

If I add context in the constructor like this

` 

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

            this.context = context;
        }

`

I'm getting in my 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);

Please help me to find a solution I tried a lot on google but not understanding what to do.

You've added Context to the adapter constructor, so when you call this constructor from your fragment or activity you need to add context. If you call it from an Activity call

 HomeFeaturedAdapter adapter = new HomeFeaturedAdapter(this, productList)

From Fragment you can call

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

You are using the wrong constructor to make the object of an adapter.You have to use this one

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

You are using multiple constructors in the adapter so you are using the one without context so your context is null in the adapter.

Hope this will Help You. This is correct code i already tried it!

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

}

And in your fragment do like this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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