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
Related
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);
}
}
}
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);
}
}
}
I have two adapters that adapt to a recycler view depending on different conditions. I want to merge them.
The issue is that I created first in the same file as the activity where it is being used and the other is in a new file.
Take it as follows:
DefaultAdapter is in the file where it is being used.
AutoCompleteAdapter is also used in the same file but is declared in other file.
I want to get rid of Default Adapter and get its funtions into AutoCompleteAdapter.
There are some issues to it: I can no more use the ParentFiles' variables.
Below is the adapter that I want to get rid of.
public class DefaultAdapter extends RecyclerView.Adapter<DefaultAdapter.ViewHolder> {
private Context context;
private AdapterView.OnItemClickListener onItemClickListener;
List<String> searchList;
#NonNull #Override
public DefaultAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = (View) LayoutInflater.from(parent.getContext())
.inflate(android.R.layout.simple_list_item_1, null);
ViewHolder holder = new ViewHolder(view);
return holder;
}
#Override public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.txt.setText(
Html.fromHtml(getItem(position)).toString());///need to get rid of this error
}
#Override public int getItemCount() {
return searchList.size();
}
#NonNull public String getItem(int position) {
return searchList.get(position);
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView txt;
public ViewHolder(#NonNull View itemView) {
super(itemView);
txt = itemView.findViewById(android.R.id.text1);
itemView.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
String title = txt.getText().toString();
searchPresenter.saveSearch(title);
}
});
itemView.setOnLongClickListener(new View.OnLongClickListener() {
#Override public boolean onLongClick(View v) {
String searched = txt.getText().toString();
deleteSpecificSearchDialog(searched);
return true;
}
});
}
}
}
Please take note of the SearchPresenter. This is a variable in the parent file, that is causing major issues. Autocomplete adapter doesn't have to use it, but default have to.
This is the adapter I want it to merge into
public class AutoCompleteAdapter extends RecyclerView.Adapter<AutoCompleteAdapter.ViewHolder>
implements Filterable {
#Inject JNIKiwix currentJNIReader;
#Inject SharedPreferenceUtil sharedPreferenceUtil;
#Inject ZimReaderContainer zimReaderContainer;
private List<String> data;
private KiwixFilter kiwifilter;
private Context context;
public AutoCompleteAdapter(Context context) {
this.context = context;
data = new ArrayList<>();
kiwifilter = new KiwixFilter();
setupDagger();
}
private void setupDagger() {
CoreApp.getCoreComponent().inject(this);
}
#NonNull #Override public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = (View) LayoutInflater.from(parent.getContext())
.inflate(android.R.layout.simple_list_item_1, null);
ViewHolder holder = new ViewHolder(view);
return holder;
}
#Override public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.title.setText(Html.fromHtml(getItem(position)).toString());
}
#Override
public String getItem(int index) {
String a = data.get(index);
if (a.endsWith(".html")) {
String trim = a.substring(2);
trim = trim.substring(0, trim.length() - 5);
return trim.replace("_", " ");
} else {
return a;
}
}
#Override public int getItemCount() {
return data.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView title;
public ViewHolder(#NonNull View itemView) {
super(itemView);
title = itemView.findViewById(android.R.id.text1);
}
}
}
...
Note the similarity between the two, they are almost similar, autocompleteadapter is like superset of defaultadapter.
I just want to get rid of the default adapter.
Note: I have chopped off some other things from autocompeleteadapter, I think they are not required for the problem.
Noob here...
Thanks
I'm trying to develop a project in accordance with MVP standarts. In the guide i followed, author created separate ViewHolder class. I tried to do the same, but Adapter refuses to work with separate ViewHolder.
There is 2 errors
Cannot resolve symbol 'LessonCardView'
'onCreateViewHolder(ViewGroup, int)' in RVAdapter clashes with 'onCreateViewHolder(ViewGroup, int)' in 'android.support.v7.widget.RecyclerView.Adapter'; attempting to use incompatible return type
RVAdapter.java
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.LessonCardViewHolder> {
private String[] mDataset;
public RVAdapter(String[] dataset) {
mDataset = dataset;
}
#Override
public LessonCardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new LessonCardViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.lessons_item_card, parent, false));
}
#Override
public void onBindViewHolder(LessonCardViewHolder holder, int position) {
}
#Override
public int getItemCount() {
return mDataset.length;
}
}
LessonCardViewHolcer.java
public class LessonCardViewHolder extends RecyclerView.ViewHolder implements LessonCardView {
private final TextView lessonCardText;
public LessonCardViewHolder(View itemView) {
super(itemView);
lessonCardText = (TextView) itemView.findViewById(R.id.lesson_card_view);
}
#Override
public void setLessonCardText(String text) {
lessonCardText.setText(text);
}
}
I created subclass ViewHolder that inherited from LessonCardView in RVAdapter. Errors disappeared. But i'm not sure if this the right way. If it works for someone else, then i'm doing something wrong.
Change your adapter declaration from this
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.LessonCardViewHolder> {
to this
// import here your view holder
public class RVAdapter extends RecyclerView.Adapter<LessonCardViewHolder> {
It looks like from how you're providing the snippets that these classes are in different files, or not correctly nested within the same file.
Have you tried using RecyclerView.Adapter<LessonCardViewHolder> rather than RecyclerView.Adapter<RVAdapter.LessonCardViewHolder>?
LessonCardViewHolder should be a nested class in RVAdapter
Here is the complete solution - Recyclerview Adapter class example
public class IAdapter extends RecyclerView.Adapter<IAdapter.ViewHolder> {
Context context;
ArrayList<Model> modelList;
public ImagesAdapter(Context context,ArrayList<Model> modelList) {
this.context=context;
this.modelList=modelList;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_items, parent, false);
return new ViewHolderImages(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Model model=photo.get(position);
userViewHolder.textView.setText(model.getTitle());
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView textView;
public ViewHolderImages(View itemView) {
super(itemView);
textView=(TextView)itemView.findViewById(R.id.textView);
}
}
#Override
public int getItemCount() {
return modelList.size();
}
}
I have this horizontal recyclerview inside a tab layout.
Things I am sure of:
1. The list is not empty and is passed correctly.
2. The adapter constructor method is called.
3. The fragment is called.
The adapter code:
public class ProjectAdapter extends RecyclerView.Adapter<ProjectAdapter.ProjectViewHolder> {
ArrayList<ProjectModel> project;
public ProjectAdapter(ArrayList<ProjectModel> project) {
this.project = project;
}
public static class ProjectViewHolder extends RecyclerView.ViewHolder {
TextView title, desc;
public ProjectViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.proj_element_title);
desc = (TextView) itemView.findViewById(R.id.proj_element_desc);
}
}
#Override
public ProjectViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.projects_element, parent, false);
return new ProjectViewHolder(view);
}
#Override
public void onBindViewHolder(ProjectViewHolder holder, int position) {
//holder.imageView.setImageResource(projectModel.getImg_res());
holder.title.setText(project.get(position).getTitle());
holder.desc.setText(project.get(position).getDesc());
}
#Override
public int getItemCount() {
return project.size();
}
}
I added debugging points at
holder.title.setText(project.get(position).getTitle());
holder.desc.setText(project.get(position).getDesc());
return project.size();
but they were never called.
What could be the problem?