I have objects in my firebase database called userForm .
Each userForm have an instance variable called isPassedInspection that is either set to true or false in my firebase.
users can send Form object to my firebase, therefore I manually set isPassedInspection to true once I consider the form they send as "approved" by my standards.
I would like that my RecyclerView only create CardViews for userForm that it's isPassedInspection is true, otherwise, don't create a CardView (return null).
This is my code:
adapter = new FirebaseRecyclerAdapter<userForm, userFormViewHolder>(options) {
boolean isFormInspected;
#Override
public userFormViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
isPassedFormQuery.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(!dataSnapshot.exists()){
Log.e(TAG,"datasnapshot doesn't exist in db");
}
for(DataSnapshot singleSnapshot: dataSnapshot.getChildren()){
if(singleSnapshot.exists()){
//get the boolean variable from firebase for this item, and set it to "isFormInspected"
isFormInspected = singleSnapshot.getValue(userForm.class).isPassedInspection();
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
// if form is manually inspected create a CardView for that form, else return null
if(isFormInspected){
CardView cv =(CardView)LayoutInflater.from(parent.getContext())
.inflate(R.layout.form_card, parent, false);
Log.e(TAG, "Created a CardView");
return new userFormViewHolder(cv);
}
else{
Log.e(TAG,"Form is not inspected,so dont create a CardView");
return null;
}
}
even though I know for sure that my item isPassedInspection is true, I always get this log I made:
Form is not inspected,so dont create a CardView
and after that this error:
java.lang.NullPointerException: Attempt to write to field 'int
android.support.v7.widget.RecyclerView$ViewHolder.mItemViewType' on a
null object reference
any suggestions? thank you!
Use your own adapter class.
you can see the codes.
UserForm.class
public class UserForm {
String name,birthday,hobby;
boolean isPassedInspection;
public UserForm(String name, String birthday, String hobby, boolean isPassedInspection) {
this.name = name;
this.birthday = birthday;
this.hobby = hobby;
this.isPassedInspection = isPassedInspection;
}
public UserForm() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public boolean isPassedInspection() {
return isPassedInspection;
}
public void setPassedInspection(boolean passedInspection) {
isPassedInspection = passedInspection;
}}
// adapter class
public class FormsAdapter extends RecyclerView.Adapter<FormsViewHolder> {
private ArrayMap<String,UserForm> formsList=new ArrayMap<>();
/*use arrayMap to get from data and key */
public FormsAdapter() {
formsList=new ArrayMap<>();
}
#Override
public FormsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
// inflating card view item
View v = inflater.inflate(R.layout.form_card, parent, false);
return new FormsViewHolder(v);
}
#Override
public void onBindViewHolder(FormsViewHolder holder, int position) {
String itemKey=formsList.keyAt(position);
UserForm userForm=formsList.get(itemKey);
// set one forms data
holder.setFormsData(userForm);
holder.view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// handle click event
}
});
}
#Override
public int getItemCount() {
return formsList.size();
}
public void addAFormItem(String key,UserForm userForm)
{
if (!formsList.containsKey(key))
{
formsList.put(key,userForm);
notifyItemInserted(formsList.size());
}
} }
ViewHolder class
public class FormsViewHolder extends RecyclerView.ViewHolder {
public View view;
public FormsViewHolder(View itemView) {
super(itemView);
view=itemView;
}
public void setFormsData(UserForm userForm)
{
// initialise card views items and set value in them
TextView userName=(TextView)view.findViewById(R.id.userName);
userName.setText(userForm.getName());
}}
your fragment
public class FormsListFragment extends Fragment {
private DatabaseReference formsRef;
private FormsAdapter adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_forms_list, container, false);
/**/
RecyclerView recyclerView=(RecyclerView)view.findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
adapter=new FormsAdapter();
recyclerView.setAdapter(adapter);
formsRef= FirebaseDatabase.getInstance().getReference().child("forms");
formsRef.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
/*add data in adapter one by one if isPassedInspection true*/
String itemKey=dataSnapshot.getKey();
UserForm userForm=dataSnapshot.getValue(UserForm.class);
if (userForm.isPassedInspection())
adapter.addAFormItem(itemKey,userForm);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
/*when something change in data then add in adapter if isPassedInspection true*/
String itemKey=dataSnapshot.getKey();
UserForm userForm=dataSnapshot.getValue(UserForm.class);
if (userForm.isPassedInspection())
adapter.addAFormItem(itemKey,userForm);
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
return view;
}}
Related
This is the code for the nested recycler view.
If I add data using the push() method, i get the Nested RecycleView with Firebase this error:
Expected a List while deserializing, but got a class java.util.HashMap
AlertDialog dialog;
IFirebaseLoadListener iFirebaseLoadListener;
RecyclerView my_recycler_view;
DatabaseReference myData;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myData= FirebaseDatabase.getInstance().getReference("MyData");
dialog= new SpotsDialog.Builder().setContext(this).build();
iFirebaseLoadListener = this;
// view
my_recycler_view= (RecyclerView)findViewById(R.id.my_recycler_view);
my_recycler_view.setHasFixedSize(true);
my_recycler_view.setLayoutManager(new LinearLayoutManager(this));
load data
getFirebaseData();
}
private void getFirebaseData() {
dialog.show();
myData.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
List<itemGroup> itemGroups= new ArrayList<>();
for(DataSnapshot groupSnapshot: dataSnapshot.getChildren())
{
itemGroup itemGroup= new itemGroup();
///
Firebase instance code
///
itemGroup.setHeaderTitle(groupSnapshot.child("headerTitle").getValue(true).toString());
GenericTypeIndicator<ArrayList<itemData>> genericTypeIndicator= new GenericTypeIndicator<ArrayList<itemData>>(){} ;
itemGroup.setListItem(groupSnapshot.child("listItem").child(key).getValue(genericTypeIndicator));
itemGroups.add(itemGroup);
}
iFirebaseLoadListener.onFirebaseLoadSuccess(itemGroups);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
iFirebaseLoadListener.onFirebaseLoadFailed(databaseError.getMessage());
}
});
}
#Override
public void onFirebaseLoadSuccess(List<itemGroup> itemGroupList) {
MyItemGroupAdapter adapter = new MyItemGroupAdapter(this,itemGroupList);
my_recycler_view.setAdapter(adapter);
dialog.dismiss();
}
#Override
public void onFirebaseLoadFailed(String message) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
}
public class itemData {
private String name ,image;
public itemData() {
}
public itemData(String name, String image) {
this.name = name;
this.image = image;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}
public class itemGroup {
private String headerTitle;
private ArrayList<itemData> listItem;
public itemGroup() {
}
public itemGroup(String headerTitle, ArrayList<itemData> listItem) {
this.headerTitle = headerTitle;
this.listItem = listItem;
}
public String getHeaderTitle() {
return headerTitle;
}
public void setHeaderTitle(String headerTitle) {
this.headerTitle = headerTitle;
}
public ArrayList<itemData> getListItem() {
return listItem;
}
public void setListItem(ArrayList<itemData> listItem) {
this.listItem = listItem;
}
}
public class MyItemAdapter extends RecyclerView.Adapter<MyItemAdapter.MyViewHolder> {
private Context context;
private List<itemData> itemDataList;
public MyItemAdapter(Context context, List<itemData> itemDataList) {
this.context = context;
this.itemDataList = itemDataList;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View itemView =
LayoutInflater.from(context).inflate(R.layout.layout_item,viewGroup,false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder myViewHolder, int i) {
myViewHolder.txt_item_titles.setText(itemDataList.get(i).getName());
Picasso.get().load(itemDataList.get(i).getImage()).into(myViewHolder.img_item);
myViewHolder.setiItemClickListener(new IItemClickListener() {
#Override
public void onItemClickListener(View view, int i) {
Toast.makeText(context, ""+ itemDataList.get(i).getName(),
Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return (itemDataList != null ? itemDataList.size() : 0);
}
public class MyViewHolder extends RecyclerView.ViewHolder implements
View.OnClickListener {
TextView txt_item_titles;
ImageView img_item;
IItemClickListener iItemClickListener;
public void setiItemClickListener(IItemClickListener iItemClickListener) {
this.iItemClickListener = iItemClickListener;
}
public MyViewHolder (View itemView ){
super(itemView);
txt_item_titles= (TextView)itemView.findViewById(R.id.tvTitle);
img_item= (ImageView) itemView.findViewById(R.id.itemImage);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
iItemClickListener.onItemClickListener(v,getAdapterPosition());
}
}
}
public class MyItemGroupAdapter extends
RecyclerView.Adapter<MyItemGroupAdapter.MyViewHolder> {
private Context context;
private List<itemGroup> dataList;
public MyItemGroupAdapter(Context context, List<itemGroup> dataList) {
this.context = context;
this.dataList = dataList;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View itemView =
LayoutInflater.from(context).inflate(R.layout.layout_group,viewGroup,false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(#NonNull final MyViewHolder myViewHolder, int i) {
myViewHolder.item_title.setText(dataList.get(i).getHeaderTitle());
List<itemData> itemData = dataList.get(i).getListItem();
MyItemAdapter itemListAdapter=new MyItemAdapter(context,itemData);
myViewHolder.recycler_view_item_list.setHasFixedSize(true);
myViewHolder.recycler_view_item_list.setLayoutManager(new
LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false));
myViewHolder.recycler_view_item_list.setAdapter(itemListAdapter);
myViewHolder.recycler_view_item_list.setNestedScrollingEnabled(false);
// btn more
myViewHolder.btn_more.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "Button More "+ myViewHolder.item_title.getText(),
Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return (dataList != null ? dataList.size() : 0);
}
public class MyViewHolder extends RecyclerView.ViewHolder{
TextView item_title ;
RecyclerView recycler_view_item_list;
Button btn_more;
public MyViewHolder (View itemView ){
super (itemView);
item_title= (TextView) itemView.findViewById(R.id.itemTitle);
btn_more= (Button)itemView.findViewById(R.id.btMore);
recycler_view_item_list=
(RecyclerView)itemView.findViewById(R.id.recycler_view_list);
}
}
}
This happens when you try to have a structure like this in the database:
"list": {
"-Nasdueqriyew": "First value",
"-Nberqiy39esd": "Second value",
"-Nc3hadsiuhad": "Third value"
}
And then try to map it to a List or array in your Java class.
The JSON structure is not a list, because it was pairs of keys and values, which in Java translates to:
Map<String, Object>
So most likely, the List<itemData> in your code needs to be a Map<String, itemData>.
This is my RecyclerView Adaptor Class
public class TodoAdaptor extends RecyclerView.Adapter<TodoAdaptor.SingleTodoView> {
private CheckBox checkbox;
private TextView title, dueDate;
private Context context;
private ArrayList<SingleTodo> todoList;
private onItemCLickListener itemCLickListener;
public TodoAdaptor(Context context, ArrayList<SingleTodo> todoList, onItemCLickListener onItemClickListener) {
this.context = context;
this.todoList = todoList;
this.itemCLickListener = onItemClickListener;
}
#NonNull
#Override
public SingleTodoView onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_todo, parent, false);
return new SingleTodoView(v, itemCLickListener);
}
#Override
public void onBindViewHolder(#NonNull SingleTodoView holder, int position) {
SingleTodo singleTodo = todoList.get(position);
checkbox.setChecked(singleTodo.isComplete());
title.setText(singleTodo.getTitle());
if (singleTodo.isComplete()) {
title.setPaintFlags(title.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
Toast.makeText(context, "IsCompleted", Toast.LENGTH_SHORT).show();
} else {
title.setPaintFlags(title.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
Toast.makeText(context, "Not IsCompleted", Toast.LENGTH_SHORT).show();
}
if (singleTodo.getDueDate().isEmpty()) {
dueDate.setVisibility(View.GONE);
} else {
dueDate.setText(singleTodo.getDueDate());
}
}
#Override
public int getItemCount() {
return todoList.size();
}
class SingleTodoView extends RecyclerView.ViewHolder {
private SingleTodoView(#NonNull View itemView, final onItemCLickListener itemCLickListener) {
super(itemView);
checkbox = itemView.findViewById(R.id.todo_list_completed_checkbox);
title = itemView.findViewById(R.id.todo_list_title);
dueDate = itemView.findViewById(R.id.todo_list_due_date);
title.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
itemCLickListener.onTextClickListener(getAdapterPosition());
}
});
checkbox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
itemCLickListener.onCheckboxClickListener(getAdapterPosition());
}
});
}
}
public interface onItemCLickListener {
void onTextClickListener(int position);
void onCheckboxClickListener(int position);
}
}
This is My Adaptor
todoAdaptor = new TodoAdaptor(this, singleTodoArrayList, new TodoAdaptor.onItemCLickListener() {
#Override
public void onTextClickListener(int position) {
singleTodo = singleTodoArrayList.get(position);
singleTodo.setTitle("This is Test");
singleTodoArrayList.set(position, singleTodo);
todoAdaptor.notifyItemChanged(position);
}
#Override
public void onCheckboxClickListener(int position) {
singleTodo = singleTodoArrayList.get(position);
singleTodo.setComplete(!singleTodo.isComplete());
singleTodoArrayList.set(position, singleTodo);
todoAdaptor.notifyItemChanged(position);
}
});
and This is my SingleTodo Class
package com.example.simpletodo.classes;
public class SingleTodo {
private int id;
private String title;
private String dueDate;
private boolean isComplete;
public SingleTodo(int id, String title, String dueDate, boolean isComplete) {
this.id = id;
this.title = title;
this.dueDate = dueDate;
this.isComplete = isComplete;
}
public int getId() {
return id;
}
public String getTitle() {
return title;
}
public String getDueDate() {
return dueDate;
}
public boolean isComplete() {
return isComplete;
}
public void setId(int id) {
this.id = id;
}
public void setTitle(String title) {
this.title = title;
}
public void setDueDate(String dueDate) {
this.dueDate = dueDate;
}
public void setComplete(boolean complete) {
isComplete = complete;
}
}
Whenever I check the checkbox, Text of that item should paint Strikethrough and Its not working as Expected
I have 3 Items in the list, when i click checkbox Item get Checked and Text Strikethrough works but when i again click checkbox checkbox stay checked (although Object is being Updated as it should be) and strikethrought text revert to normal, and i have to click checkbox twice again for check box to get unchecked
other issue is that when i checked on item and then i try to check other item, Current Item text get replaced by one of the other checked item Text.
Images :
Default:
When i click the Checkbox Once:
When i Click the Checkbox Again:
When i Checked multiple Items one by one(I have not changed position of any item before checking they was in default order and after checking --in order-- this is what i get)
Any Help is Appreciated. I just want my code to work as Expected.
I found the Solution from this Question
I had to change my Adaptor Class. I Declared Views inside my view holder class and onBindViewHolder, i am using first parameter (holder) to get my Views
public class TodoAdaptor extends RecyclerView.Adapter<TodoAdaptor.SingleTodoView> {
private boolean darkTheme;
private Context context;
private ArrayList<SingleTodo> todoList;
private onItemCLickListener itemCLickListener;
public TodoAdaptor(Context context, ArrayList<SingleTodo> todoList, boolean darkTheme, onItemCLickListener onItemClickListener) {
this.darkTheme = darkTheme;
this.context = context;
this.todoList = todoList;
this.itemCLickListener = onItemClickListener;
}
#NonNull
#Override
public SingleTodoView onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.single_todo, parent, false);
return new SingleTodoView(v, itemCLickListener);
}
#Override
public void onBindViewHolder(#NonNull SingleTodoView holder, int position) {
SingleTodo singleTodo = todoList.get(position);
holder.checkbox.setChecked(singleTodo.isComplete());
holder.title.setText(singleTodo.getTitle());
if (singleTodo.isComplete()) {
holder.title.setPaintFlags(holder.title.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
} else {
holder.title.setPaintFlags(holder.title.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
}
if (singleTodo.getDueDate().isEmpty()) {
holder.dueDate.setVisibility(View.GONE);
} else {
holder.dueDate.setVisibility(View.VISIBLE);
holder.dueDate.setText(singleTodo.getDueDate());
}
}
#Override
public int getItemCount() {
return todoList.size();
}
class SingleTodoView extends RecyclerView.ViewHolder {
private CheckBox checkbox;
private TextView title, dueDate;
private SingleTodoView(#NonNull View itemView, final onItemCLickListener itemCLickListener) {
super(itemView);
checkbox = itemView.findViewById(R.id.todo_list_completed_checkbox);
title = itemView.findViewById(R.id.todo_list_title);
dueDate = itemView.findViewById(R.id.todo_list_due_date);
if (darkTheme) {
title.setTextColor(context.getResources().getColor(R.color.colorLightGray));
dueDate.setTextColor(context.getResources().getColor(R.color.colorLightGray));
} else {
title.setTextColor(context.getResources().getColor(R.color.colorBlack));
dueDate.setTextColor(context.getResources().getColor(R.color.colorBlack));
}
title.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
itemCLickListener.onTextClickListener(getAdapterPosition());
}
});
checkbox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
itemCLickListener.onCheckboxClickListener(getAdapterPosition());
}
});
}
}
public interface onItemCLickListener {
void onTextClickListener(int position);
void onCheckboxClickListener(int position);
}
}
I am trying to make a list of Recycler View by loading data from FireBase
For that I made MainList.java
public class MainList {
private String imageResourceId;
private String name;
public MainList() {}
public MainList(String imageResourceId, String name){
this.imageResourceId = imageResourceId;
this.name = name;
}
public String getImageResourceId() { return imageResourceId; }
public String getName(){ return name;}
}
and MainListAdapter
public class MainListAdapter extends RecyclerView.Adapter<MainListAdapter.MainListViewHolder>{
ImageView item_image;
List<MainList> mMainList;
LayoutInflater inflater;
class MainListViewHolder extends RecyclerView.ViewHolder {
TextView item_name;
ImageView item_image;
MainListAdapter mAdapter;
public MainListViewHolder(View itemView, MainListAdapter adapter) {
super(itemView);
item_name = itemView.findViewById(R.id.item_name);
item_image = itemView.findViewById(R.id.item_image);
this.mAdapter = adapter;
}
}
public MainListAdapter(Context context, List<MainList> main_list){
inflater = LayoutInflater.from(context);
this.mMainList = main_list;
}
#NonNull
#Override
public MainListViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View mItemView = inflater.inflate(R.layout.list_item, parent, false);
return new MainListViewHolder(mItemView, this);
}
#Override
public void onBindViewHolder(#NonNull MainListViewHolder holder, int position) {
MainList mainList = mMainList.get(position);
holder.item_name.setText(mainList.getName());
Picasso.get().load(mainList.getImageResourceId()).into(item_image);
//holder.item_image.setImageResource(mainList.getImageResourceId());
}
#Override
public int getItemCount() {
return mMainList.size();
}
}
And I try to use use Picasso inside of OnBindViewHolder for my image
and in MainActivity I try to get my data from FireBase to fill my List
main_comics_List = new ArrayList<>();
mRef = FirebaseDatabase.getInstance().getReference().child("ComicsData").child("AllComicses");
mRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()){
MainList item_of = dataSnapshot1.getValue(MainList.class);
main_comics_List.add(item_of);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
But when I start my app nothing shows at the place of my Recycler and I get the message
What am I doing wrong? And how do I solve it?
I would appreciate you answering me.
The instance variables inside the class MainList should be the same as the attributes inside the database. Therefore inside the MainList add the following:
private String imageLink;
private String title;
And the setters and getters:
public String getImageLink() { return imageLink; }
public String getTitle(){ return title;}
public void setImageLink(String imageLink) { this.imageLink = imageLink; }
public void setTitle(String title){ this.title = title;}
Also in your database it doesnt seem that you are using name and imageResourceId therefore you can remove them from the class MainList.
I am currently working on a project that uses firebase. I have problems concerning using firebase. How do I retrieve the images?
public class ViewHolder extends RecyclerView.Adapter<ViewHolder.ImageViewHolder> {
private Context mContext;
private List<Model> mModel;
public ViewHolder(Context context, List<Model> models)
{
mContext=context;
mModel=models;
}
#NonNull
#Override
public ImageViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(mContext).inflate(R.layout.row, viewGroup,false);
return new ImageViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull ImageViewHolder imageViewHolder, int i) {
Model mModelcur = mModel.get(i);
imageViewHolder.img_description.setText(mModelcur.getDescription());
Picasso.get().load(mModelcur.getImage()).into(imageViewHolder.image_view);
}
#Override
public int getItemCount() {
return mModel.size();
}
public class ImageViewHolder extends RecyclerView.ViewHolder
{
public TextView img_description;
public ImageView image_view;
public ImageViewHolder(#NonNull View itemView) {
super(itemView);
img_description = itemView.findViewById(R.id.rDescription);
image_view = itemView.findViewById(R.id.rImageview);
}
}
}
The code above shows the viewholder while this code is for executing the firebase with the viewholder and the getter and setter which is the model
public class Ordering extends AppCompatActivity {
private RecyclerView mRecyclerView;
private ViewHolder mAdapter;
private DatabaseReference mDatabaseReference;
private List<Model> mModel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ordering);
mRecyclerView= findViewById(R.id.recyclerview);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mModel = new ArrayList<>();
mDatabaseReference=FirebaseDatabase.getInstance().getReference("Data");
mDatabaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapshot:dataSnapshot.getChildren())
{
Model model=postSnapshot.getValue(Model.class);
mModel.add(model);
}
mAdapter=new ViewHolder(Ordering.this, mModel);
mRecyclerView.setAdapter(mAdapter);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(Ordering.this, databaseError.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
}
The code works but it only shows this recycler part. I already did the database which looks like this database. How do I show the images which have been stored in the database and show it in the recycler view? I'm still new in using firebase can someone help me, please?
This is my model.class #Alex
public class Model {
String title,image,description; //These must be match in the firebase database
//Constructor
public Model()
{
}
//getter and setter
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
You are getting the following error:
No setter/field for image found on class com.example.firebaseproject.Model
Because you are using in your Model class the fileds with no modifier. To solve this, change the modifier to private:
private String title, image, description;
override fun onStart() {
super.onStart()
mRecyclerView.startListening()
}
override fun onStop() {
super.onStop()
mRecyclerView.stopListening()
}
It's look like the problem is from your image url use picasso 2.71828 and then you can see call back of image processing and you can see what is wrong there
Picasso.get().load(mModelcur.getImage()).into(imageViewHolder.image_view , new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError(Error e) {
Log.i("log" , e.getMessage());
}
});
Firebase database screenshot link
I Cannot retrieve the data in a device this is wallpaper app
I have updated the google play services also but nothing happens.
I have also given the internet permission.
I have also tested on the emulator and a physical device.
Firebase database screenshot link on top.
CategoryFragment.java
public class CategoryFragment extends Fragment {
FirebaseDatabase database;
DatabaseReference categoryBackground;
//Firebase UI adpter
FirebaseRecyclerOptions<CategoryItem> options;
FirebaseRecyclerAdapter<CategoryItem, CategoryViewHolder> adapter;
//View
RecyclerView recyclerView;
private static CategoryFragment INSTANCE=null;
public CategoryFragment() {
database = FirebaseDatabase.getInstance();
categoryBackground =
database.getReference(Common.STR_CATEGORY_BACKGROUND);
options = new FirebaseRecyclerOptions.Builder<CategoryItem>()
.setQuery(categoryBackground, CategoryItem.class)
.build();
adapter = new FirebaseRecyclerAdapter<CategoryItem, CategoryViewHolder>
(options) {
#Override
protected void onBindViewHolder(#NonNull final CategoryViewHolder
holder, int position, #NonNull final CategoryItem model) {
Picasso.with(getActivity())
.load(model.getImageLink())
.networkPolicy(NetworkPolicy.OFFLINE)
.into(holder.background_image, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError() {
//Try again if cache failed to load
Picasso.with(getActivity())
.load(model.getImageLink())
.error(R.drawable.ic_terrain_black_24dp)
.into(holder.background_image, new
Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError() {
Log.e("ERROR_MSU", "Couldn't
fetch image");
}
});
}
});
holder.category_name.setText(model.getName());
holder.setItemClickListener(new ItemClickListener() {
#Override
public void onClick(View view, int position) {
//Code later for detail category
}
});
}
#Override
public CategoryViewHolder onCreateViewHolder(ViewGroup parent, int
viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.layout_category_item,parent,false);
return new CategoryViewHolder(itemView);
}
};
}
public static CategoryFragment getInstance()
{
if(INSTANCE == null)
INSTANCE = new CategoryFragment();
return INSTANCE;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_category, container,
false);
recyclerView = (RecyclerView)view.findViewById(R.id.recycler_category);
recyclerView.setHasFixedSize(true);
GridLayoutManager gridLayoutManager = new
GridLayoutManager(getActivity(),2);
recyclerView.setLayoutManager(gridLayoutManager);
setCategory();
return view;
}
private void setCategory() {
adapter.startListening();
recyclerView.setAdapter(adapter);
}
#Override
public void onStart() {
super.onStart();
if (adapter!=null)
adapter.startListening();
}
#Override
public void onStop() {
if (adapter!=null)
adapter.stopListening();
super.onStop();
}
#Override
public void onResume() {
super.onResume();
if (adapter!=null)
adapter.stopListening();
}
}
CategoryItem.java
public class CategoryItem {
public String name;
public String imageLink;
public CategoryItem() {
}
public CategoryItem(String name, String imageLink){
this.name = name;
this.imageLink = imageLink;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImageLink() {
return imageLink;
}
public void setImageLink(String imageLink) {
this.imageLink = imageLink;
}
}
CategoryViewHolder
public class CategoryViewHolder extends RecyclerView.ViewHolder implements
View.OnClickListener {
public TextView category_name;
public ImageView background_image;
ItemClickListener itemClickListener;
public void setItemClickListener(ItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}
public CategoryViewHolder(View itemView) {
super(itemView);
background_image = (ImageView)itemView.findViewById(R.id.image);
category_name = (TextView)itemView.findViewById(R.id.name);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v){
itemClickListener.onClick(v,getAdapterPosition());
}
}