How to get shared prefs in Adapter class, Firebase - android

I Get data from firebase into a recycler view, In my Adapter class I want to get a Data which is stored in my Shared Prefs, How can I get the Data which is in my Shared Prefs into my Firebase Adapter Activity !
MY ADAPTER CLASS
public class Chat_Adapter extends
FirebaseRecyclerAdapter<Chat_Model,Chat_Adapter.myViewHolder> {
public Chat_Adapter(#NonNull FirebaseRecyclerOptions<Chat_Model> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull myViewHolder holder, int position, #NonNull Chat_Model model) {
holder.date.setText(model.getDate());
holder.uname.setText(model.getUname());
holder.msg.setText(model.getMsg());
}
#NonNull
#Override
public myViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_item, parent, false);
return new myViewHolder(view);
}
class myViewHolder extends RecyclerView.ViewHolder {
TextView uname, date,msg;
public myViewHolder(#NonNull View itemView) {
super(itemView);
uname = itemView.findViewById(R.id.named);
date = itemView.findViewById(R.id.date);
msg = itemView.findViewById(R.id.message);
}
}
}
I have a data coming from my Database which (model.getUname), What i want to do is to my original name from my Shared Prefs and check if (model.getUname == myname).

Get Context inside onCreateViewHolder() method and save it in your field variable and then use it inside onBindViewHolder() and get shared prefs there
public class Chat_Adapter extends FirebaseRecyclerAdapter<Chat_Model, Chat_Adapter.myViewHolder> {
private Context context;
public Chat_Adapter(#NonNull FirebaseRecyclerOptions<Chat_Model> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull myViewHolder holder, int position, #NonNull Chat_Model model) {
//here is your shared pref
SharedPreferences your_shared_pref_name = context.getSharedPreferences("YOUR_SHARED_PREF_NAME", Context.MODE_PRIVATE);
holder.date.setText(model.getDate());
holder.uname.setText(model.getUname());
holder.msg.setText(model.getMsg());
}
#NonNull
#Override
public myViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_item, parent, false);
context = parent.getContext();
return new myViewHolder(view);
}
class myViewHolder extends RecyclerView.ViewHolder {
TextView uname, date, msg;
public myViewHolder(#NonNull View itemView) {
super(itemView);
uname = itemView.findViewById(R.id.named);
date = itemView.findViewById(R.id.date);
msg = itemView.findViewById(R.id.message);
}
}
}

Related

how to add item OnClickListener in FirebaseRecyclerAdapter?

I am trying to set an onClickListener on my recyclerview. I retrieve data from Firebase and am able to load the data in the recyclerview. When I set an onClickListener nothing happens. How can I solve this?
myadapter.class
public class my adapter extends FirebaseRecyclerAdapter<model,myadapter.myviewholder>{
public myadapter(#NonNull FirebaseRecyclerOptions<model> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull myadapter.myviewholder holder, int position, #NonNull model model)
{
holder.name.setText(model.getName());
Glide.with(holder.img.getContext()).load(model.getImgurl()).into(holder.img);
}
#NonNull
#Override
public myviewholder onCreateViewHolder(#NonNull ViewGroup parent, int viewType)
{
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.singlerow,parent,false);
return new myviewholder(view);
}
class myviewholder extends RecyclerView.ViewHolder
{
ImageView img;
TextView name;
Uri tvurl;
public myviewholder(#NonNull View itemView)
{
super(itemView);
img = (ImageView)itemView.findViewById(R.id.img1);
name =(TextView) itemView.findViewById(R.id.nametext);
}
}
}

FirestoreRecyclerAdapter onBindViewHolder Not Working properly

I'm not sure what am doing wrong but I have a recyclerview populated with firebase firestore using FirestoreRecyclerAdapter. My challenge is that I can add documents and retrieve them to the recyclerview but for some reason, the adapter doesn't set Text.
This is my Adapter
public class CategoriesAdapter extends FirestoreRecyclerAdapter<BookCategory, CategoriesAdapter.CategoryHolder> {
public CategoriesAdapter(#NonNull FirestoreRecyclerOptions<BookCategory> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull CategoryHolder categoryHolder, int i, #NonNull BookCategory bookCategory) {
categoryHolder.txtCategory.setText(bookCategory.getCategory());
categoryHolder.txtDescription.setText(bookCategory.getDescription());
}
#NonNull
#Override
public CategoryHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.category_item,
parent, false);
return new CategoryHolder(v);
}
class CategoryHolder extends RecyclerView.ViewHolder{
TextView txtCategory, txtDescription;
public CategoryHolder(#NonNull View itemView) {
super(itemView);
txtCategory = itemView.findViewById(R.id.Category);
txtDescription = itemView.findViewById(R.id.Description);
}
}
I managed to find the issue. The problem was in the class, I had changed the private variables

runtime exception when i click the button to load data

I want to retrieve the data from a SQLite database and show it in the RecyclerView but it shows an error
((Adapter class))
public class myAdapert extends RecyclerView.Adapter<myAdapert.myViewHolder> {
ArrayList<Model_class> modelClassArrayList;
public myAdapert(ArrayList<Model_class> modelClassArrayList) {
this.modelClassArrayList = modelClassArrayList;
}
#NonNull
#Override
public myViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View customView = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.custom_row,viewGroup,false);
myViewHolder myviewHolder = new myViewHolder(customView);
return myviewHolder;
}
#Override
public void onBindViewHolder(#NonNull myViewHolder myViewHolder, int i) {
Model_class model_class = modelClassArrayList.get(i);
myViewHolder.author.setText(model_class.getAuthor());
myViewHolder.title.setText(model_class.getTitle());
myViewHolder.cost.setText(model_class.getCost());
myViewHolder.quantity.setText(model_class.getQuantity());
}
#Override
public int getItemCount() {
return modelClassArrayList.size();
}
public class myViewHolder extends RecyclerView.ViewHolder {
TextView author,title;
TextView cost,quantity;
public myViewHolder(#NonNull View itemView) {
super(itemView);
author = itemView.findViewById(R.id.authortxt);
title = itemView.findViewById(R.id.titleTxt);
cost = itemView.findViewById(R.id.costTxt);
quantity = itemView.findViewById(R.id.quantityTxt);
}
}
As cost and quantity are numbers, I suppose, you should convert it to String explicitly, to prevent resourse manager from loading drawables:
myViewHolder.cost.setText(String.valueOf(model_class.getCost()));
myViewHolder.quantity.setText(String.valueOf(model_class.getQuantity()));

How to acces SharedPrefences from recyclerView adapter?

I want to acces SharedPrefences from my adapter because I want to check a variable to set an ImageView visible or gone.
class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> implements View.OnClickListener, View.OnLongClickListener{
ArrayList<String> mData;
public ImageView mMinus;
public static final String SHARED_PREFS_DATA = "sharedPrefsData";
public MainAdapter(ArrayList<String> data) {
mData = data;
}
#NonNull
#Override
public MainAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MainAdapter.ViewHolder holder, int position) {
holder.mText.setText(mData.get(position));
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS_DATA, 0);
int a = sharedPreferences.getInt("visible", 0);
if(a == 0)
holder.mMinus.setVisibility(View.GONE);
else
holder.mMinus.setVisibility(View.VISIBLE);
}
error: cannot find symbol method getSharedPreferences(String,int)
1.Pass the context from the Activity to the adapter and using this context you can access the shared preferences
2.You can also pass the Activity reference instead of Context

Refreshing values of recyclerview adapter from another adapter

I have to two data sets with different adapters inside a single activity and I want to use notifyDataSetChanged() however its not working for the other adapter. So inside my adapterFirst class I have LongCLickListener which is using notifyDataSetChanged() and I want to call the adapterSecond to notify too if adapterFirst is updated. The AdapterSecond data is not being updated when pressing LongClick
public class AdapterFirst extends RecyclerView.Adapter <AdapterFirst .ViewHolder> {
private List<Object> objectList;
private Context mContext;
AdapterSecond adapterSecond;
public AdapteFirst(List<Object> list) { objectList= list; }
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_list, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
//query method
notifyDataSetChanged(); //working
//here it gets the nullpointerexception error
adapterSecond = new AdapteSecond();
adapterSecond.notifyDataSetChanged();
}
});
}
#Override
public int getItemCount() {
return (objectList != null? objectList.size():0);
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView data;
public ViewHolder(View itemView) {
super(itemView);
data= (TextView) itemView.findViewById(R.id.text);
}
}
}
AdapterSecond
public class AdapterSecond extends
RecyclerView.Adapter <AdapterSecond.ViewHolder> {
private List<Object> objectList;
private Context mContext;
public AdapterSecond () {}
public AdapterSecond (List<Object> list) { objectList= list; }
#Override
public AdapterSecond.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_list_2, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final AdapterSecond.ViewHolder holder, int position) {
final Object object = objectList.get(holder.getAdapterPosition());
holder.data2.setText(object.getData());
}
#Override
public int getItemCount() {
return (objectList != null? objectList.size():0);
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView data2;
public ViewHolder(View itemView) {
super(itemView);
data2= (TextView) itemView.findViewById(R.id.text2);
}
}
}
Seems like the order of initialization of the adapters have some issues.
Just make sure that you have initialize the secondAdapter before the firstAdapter
secondAdapter= new SecondAdapter();
firstAdapter = new FirstAdapter();
Just check the order in which they initialize!

Categories

Resources