I want to display User data from Firebase to RecyclerView - java

I want to retrieve data from Firebase to recyclerView through FirebaseRecyclerOptionsbut it keeps showing blanks pages,I want to display the registered Users in my App and there details in a layout,I have no idea where I have gone wrong,I have multi check on my codes and I can't see any error Please can Anyone help me here.
Here Are Code for ListAdapter,Model and ListActivity
public class ListActivity extends AppCompatActivity {
private FirebaseAuth firebaseAuth;
private DatabaseReference reference;
RecyclerView recyclerView;
ListAdapter listAdapter;
private String CurrentUserID;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
recyclerView = findViewById(R.id.children_home_list);
firebaseAuth = FirebaseAuth.getInstance();
CurrentUserID = firebaseAuth.getCurrentUser().getUid();
reference = FirebaseDatabase.getInstance().getReference().child("Children Home Details");
recyclerView.setLayoutManager(new LinearLayoutManager(this));
FirebaseRecyclerOptions<Model> options =
new FirebaseRecyclerOptions.Builder<Model>()
.setQuery(reference, Model.class)
.build();
listAdapter = new ListAdapter(options);
}
#Override
protected void onStart() {
super.onStart();
listAdapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
listAdapter.stopListening();
}
public class ListAdapter extends FirebaseRecyclerAdapter<Model,ListAdapter.ListViewHolder> {
public ListAdapter(#NonNull FirebaseRecyclerOptions<Model> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull ListViewHolder holder, int position, #NonNull Model model) {
holder.Name.setText(model.getName());
holder.Phone.setText(model.getPhone());
holder.Location.setText(model.getLocation());
holder.Email.setText(model.getMailAddress());
//Glide.with(holder.circleImageView.getContext()).load(model.IMAGELINK()).into(holder.circleImageView);
}
#NonNull
#Override
public ListViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.singlerow,parent,false);
return new ListViewHolder(view);
}
class ListViewHolder extends RecyclerView.ViewHolder {
CircleImageView circleImageView;
TextView Name,Phone,Email,Location;
public ListViewHolder(#NonNull View itemView) {
super(itemView);
circleImageView = itemView.findViewById(R.id.img1);
Name = itemView.findViewById(R.id.HomeName);
Phone = itemView.findViewById(R.id.phoneName);
Email = itemView.findViewById(R.id.paypalAddress);
Location = itemView.findViewById(R.id.location);
}
}
}
public class Model {
String Name,Phone,Location,MailAddress;
public Model(String name, String phone, String location, String mailAddress) {
Name = name;
Phone = phone;
Location = location;
MailAddress = mailAddress;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getPhone() {
return Phone;
}
public void setPhone(String phone) {
Phone = phone;
}
public String getLocation() {
return Location;
}
public void setLocation(String location) {
Location = location;
}
public String getMailAddress() {
return MailAddress;
}
public void setMailAddress(String mailAddress) {
MailAddress = mailAddress;
}
}

Related

Having problem to fetching file from firestore recycle view [duplicate]

This question already has an answer here:
Firebase Android ListView not being displayed
(1 answer)
Closed 1 year ago.
I am an android developer beginner, I am having a problem fetch files from firestore recycle view in the main activity but I can fetch files from fragments.
This is my main activity
public class FoodInfoActivity extends AppCompatActivity {
RecyclerView recyclerView1,recyclerView2;
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private CollectionReference notebookRef;
private NoteAdapter adapter;
FirebaseAuth fAuth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food_info);
fAuth = FirebaseAuth.getInstance();
setUpRecyclerView();
}
private void setUpRecyclerView() {
notebookRef = db.collection("FoodInside");
Query query = notebookRef;
FirestoreRecyclerOptions<FoodAdapter> options = new FirestoreRecyclerOptions.Builder<FoodAdapter>()
.setQuery(query, FoodAdapter.class)
.build();
adapter = new NoteAdapter(options);
RecyclerView recyclerView = findViewById(R.id.fi);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
}
This is my Note adapter where i use model class to fetch file
public class NoteAdapter extends FirestoreRecyclerAdapter<FoodAdapter, NoteAdapter.NoteHolder> {
public NoteAdapter(#NonNull FirestoreRecyclerOptions<FoodAdapter> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull NoteHolder holder, int position, #NonNull FoodAdapter foodAdapter) {
holder.textViewTitle.setText(foodAdapter.getRestaurant());
holder.textViewDescription.setText(foodAdapter.getItem());
holder.textViewPriority.setText(String.valueOf(foodAdapter.getPrice()));
}
#NonNull
#Override
public NoteHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.food_recycle_layout,
parent, false);
return new NoteHolder(v);
}
class NoteHolder extends RecyclerView.ViewHolder {
TextView textViewTitle;
TextView textViewDescription;
TextView textViewPriority;
public NoteHolder(View itemView) {
super(itemView);
textViewTitle = itemView.findViewById(R.id.textView95a);
textViewDescription = itemView.findViewById(R.id.textView123a);
textViewPriority = itemView.findViewById(R.id.textView124a);
}
}
}
This is my adapter class ,
public class FoodAdapter {
String Restaurant;
String Item;
String Price;
String item_id;
public FoodAdapter() {
}
public FoodAdapter(String restaurant, String item, String price,String item_id) {
Restaurant = restaurant;
Item = item;
Price = price;
this.item_id = item_id;
}
public String getRestaurant() {
return Restaurant;
}
public void setRestaurant(String restaurant) {
Restaurant = restaurant;
}
public String getItem() {
return Item;
}
public void setItem(String item) {
Item = item;
}
public String getPrice() {
return Price;
}
public void setPrice(String price) {
Price = price;
}
public String getItem_id() {
return item_id;
}
public void setItem_id(String item_id) {
this.item_id = item_id;
}
}
Why it is not working in main activity but work fragment. I am just afraid. and tired...working on the project. so need solution of this problem
#Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
Add this line to main activity.

Fetching data from firebase, Screen shows blank after executing in android

MainActivity:
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private FirebaseAdapter mAdapter;
private static final String TAG = "MainActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = (RecyclerView)findViewById(R.id.recyclerview);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
FirebaseRecyclerOptions<DataClass> options =
new FirebaseRecyclerOptions.Builder<DataClass>()
.setQuery(FirebaseDatabase.getInstance().getReference().child("Users"), DataClass.class)
.build();
mAdapter = new FirebaseAdapter(options);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
}
#Override
protected void onStart() {
super.onStart();
mAdapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
mAdapter.stopListening();
}
}
FirebaseAdapter
public class FirebaseAdapter extends FirebaseRecyclerAdapter<DataClass,FirebaseAdapter.MyviewHolder> {
private static final String TAG = "FirebaseAdapter";
public FirebaseAdapter(#NonNull FirebaseRecyclerOptions<DataClass> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull MyviewHolder holder, int position, #NonNull DataClass model) {
holder.name.setText(model.getName());
holder.age.setText(model.getAge());
Log.d(TAG, "onBindViewHolder: Now Bind method executing");
}
#NonNull
#Override
public MyviewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View v= layoutInflater.inflate(R.layout.row_represent,parent,false);
MyviewHolder v1 = new MyviewHolder(v);
Log.d(TAG, "onCreateViewHolder: Now on create method executing");
return v1;
}
public static class MyviewHolder extends RecyclerView.ViewHolder{
TextView name;
TextView age;
public MyviewHolder(#NonNull View itemView) {
super(itemView);
name = (TextView)itemView.findViewById(R.id.names);
age = (TextView) itemView.findViewById(R.id.age);
}
}
}
DataClass
public class DataClass {
private String name;
private String age;
public DataClass(){
}
public DataClass(String name, String age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public String getAge() {
return age;
}
}
doubt in this parts
public DataClass(String name, String age) {
this.name = name;
this.age = age;
}
protected void onBindViewHolder(#NonNull MyviewHolder holder, int position, #NonNull DataClass model) {
holder.name.setText(model.getName());
holder.age.setText(model.getAge());
Log.d(TAG, "onBindViewHolder: Now Bind method executing");
}
because showing me that constructor never used
I'm not getting any errors but still my screen shows empty, Actually it is displaying null data
how to find it and guys tell me how to solve this issue
I know I made small mistake somewhere in this code help me to rectify it
Thank you.............................................
Replace this code
From
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
To
RecyclerView.LayoutManager layoutManager;
layoutManager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
mRecyclerView.setLayoutManager(layoutManager);
Hope this work.
do u have any messages in firebase database or its empty?
you can add some messages through firebase so you can check it

How to fix incompatible types: fragment cannot be converted to Context

i am trying to view items from my database and i have written everything to retrieve that and display it in an activity page. but this one line of code gives an error that doesn't allow the app to run:
here is my detailed code:
public class HomeFragment extends Fragment {
private String name;
private String location;
private String country;
private String address;
public HomeFragment() {
}
public HomeFragment(String name, String location, String country, String address) {
this.name = name;
this.location = location;
this.country = country;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
private FirebaseFirestore firebaseFirestore;
private RecyclerView FirestoreList;
private FirestoreRecyclerAdapter adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
firebaseFirestore = FirebaseFirestore.getInstance();
FirestoreList = FirestoreList.findViewById(R.id.firestore_list);
//query
Query q = firebaseFirestore.collection("Shops");
//recycle options
FirestoreRecyclerOptions<ShopModel> options = new FirestoreRecyclerOptions.Builder<ShopModel>().setQuery(q, ShopModel.class).build();
adapter = new FirestoreRecyclerAdapter<ShopModel, ShopViewHolder>(options) {
#NonNull
#Override
public ShopViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_home,parent,false);
return new ShopViewHolder(view);
}
#Override
protected void onBindViewHolder(#NonNull ShopViewHolder holder, int position, #NonNull ShopModel model) {
holder.list_name.setText(model.getName());
holder.list_country.setText(model.getName());
holder.list_location.setText(model.getName());
holder.list_address.setText(model.getName());
}
};
FirestoreList.setHasFixedSize(true);
FirestoreList.setLayoutManager(new LinearLayoutManager(this));// this is the line that is giving me the error
FirestoreList.setAdapter(adapter);
return inflater.inflate(R.layout.fragment_home, container, false);
}
private class ShopViewHolder extends RecyclerView.ViewHolder {
private TextView list_name;
private TextView list_country;
private TextView list_location;
private TextView list_address;
public ShopViewHolder(#NonNull View itemView) {
super(itemView);
list_name = itemView.findViewById(R.id.list_name);
list_country = itemView.findViewById(R.id.list_country);
list_location = itemView.findViewById(R.id.list_location);
list_address = itemView.findViewById(R.id.list_address);
}
}
#Override
public void onStart() {
super.onStart();
adapter.startListening();
}
#Override
public void onStop() {
super.onStop();
adapter.stopListening();
}
}
this is the line where there is a problem at
FirestoreList.setLayoutManager(new LinearLayoutManager(this));// this is the line that is giving me the error
what can i do to resolve this?
You are passing your fragment to LinearLayoutManager, not the Context
So what you need to do is simply replace this line
FirestoreList.setLayoutManager(new LinearLayoutManager(this));
To
FirestoreList.setLayoutManager(new LinearLayoutManager(requireContext()));
Can you please make an object for FirestoreList and try to set the LayoutManager to the object ?
FirestoreList fsList = FirestoreList.findViewById(R.id.firestore_list);
...
fsList.setLayoutManager(new LinearLayoutManager(this));
Change this statement :
return inflater.inflate(R.layout.fragment_home, container, false);
to:
View view = inflater.inflate(R.layout.fragment_home, container, false);

Get firebase auto generate Id via pojo, and my model class's getter() method return null

I working with an app but when I am trying to access firebase data the getter method of the POJO returns null. I can not access data and setup in RecyclerView
Here is what I did:
My Firebase Structure:
My app debugging mode:
My Book Class
public class Book {
private String bookId;
private String bookImage;
private String bookName;
private String writerName;
private String publisherName;
private String shortDesc;
public Book() {
}
public Book(String bookImage, String bookName,
String writerName, String publisherName,String shortDesc) {
this.bookImage = bookImage;
this.bookName = bookName;
this.writerName = writerName;
this.publisherName = publisherName;
this.shortDesc = shortDesc;
}
public Book(String bookId, String bookImage, String bookName,
String writerName, String publisherName, String shortDesc) {
this.bookId = bookId;
this.bookImage = bookImage;
this.bookName = bookName;
this.writerName = writerName;
this.publisherName = publisherName;
this.shortDesc = shortDesc;
}
public String getBookId() {
return bookId;
}
public String getBookImage() {
return bookImage;
}
public String getBookName() {
return bookName;
}
public String getWriterName() {
return writerName;
}
public String getPublisherName() {
return publisherName;
}
public String getShortDesc() {
return shortDesc;
}
public void setBookId(String bookId) {
this.bookId = bookId;
}
My BookListFragment where I'm showing my book list
public class BookListFragment extends Fragment {
private Button button1,button2,button3;
private RecyclerView bookListRv;
private ArrayList<Book> bookList;
private BookAdapter bookAdapter;
private String adminId;
private FirebaseAuth firebaseAuth;
private DatabaseReference databaseReference;
public BookListFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_book_list, container, false);
init(view);
adminId = firebaseAuth.getUid();
Book book = new Book();
String bookid =book.getBookId();
configRV();
getBooks();
return view;
}
private void init(View view) {
bookListRv = view.findViewById(R.id.bookRVId);
firebaseAuth = firebaseAuth.getInstance();
databaseReference = FirebaseDatabase.getInstance().getReference();
bookList = new ArrayList<>();
bookAdapter = new BookAdapter(bookList, getContext());
}
private void configRV() {
bookListRv.setLayoutManager(new LinearLayoutManager(getContext()));
bookListRv.setAdapter(bookAdapter);
}
private void getBooks() {
DatabaseReference showBookRef = databaseReference.child("Books").child(adminId);
showBookRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot data:dataSnapshot.getChildren()){
Book book = data.getValue(Book.class);
bookList.add(book);
bookAdapter.notifyDataSetChanged();
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
}
My AddBookActivity
public class AddBookActivity extends AppCompatActivity {
private Button addBookBtn;
private ImageView bookIv;
private EditText bookNameEt,writerNameEt,publisherNameEt,shortDescEt;
private Uri imgUrl = null;
private FirebaseAuth firebaseAuth;
private DatabaseReference databaseReference;
private StorageReference imgReference;
private String adminId,imgUri="";
private FirebaseUser mFirebaseUser;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_book);
init();
if(mFirebaseUser!=null){
adminId = mFirebaseUser.getUid();
}
bookIv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent,1);
}
});
addBookBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
addBookData();
}
});
}
private void init() {
firebaseAuth = FirebaseAuth.getInstance();
mFirebaseUser = firebaseAuth.getCurrentUser();
imgReference = FirebaseStorage.getInstance().getReference();
databaseReference = FirebaseDatabase.getInstance().getReference();
addBookBtn = findViewById(R.id.addBookBtnId);
bookIv = findViewById(R.id.addBookIvId);
bookNameEt = findViewById(R.id.addBookNameEtId);
writerNameEt = findViewById(R.id.addBookWriterNameEtId);
publisherNameEt = findViewById(R.id.addBookPublisherNameEtId);
shortDescEt = findViewById(R.id.addBookDescripEtId);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==1 && resultCode==RESULT_OK){
if(data!=null){
imgUrl = data.getData();
bookIv.setImageURI(imgUrl);
}
}
}
private void addBookData() {
if (imgUrl!=null) {
final StorageReference filePath = imgReference.child("Book_images").child(String.valueOf(System.currentTimeMillis()));
filePath.putFile(imgUrl).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
#Override
public void onComplete(#NonNull Task<UploadTask.TaskSnapshot> task) {
if (task.isSuccessful()) {
filePath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
imgUri=uri.toString();
}
});
}
}
});
}
String bookName = bookNameEt.getText().toString();
String writerName = writerNameEt.getText().toString();
String publisherName = publisherNameEt.getText().toString();
String bookDesc = shortDescEt.getText().toString();
String bookImg = imgUri;
if(TextUtils.isEmpty(bookName) && TextUtils.isEmpty(writerName)
&& TextUtils.isEmpty(publisherName) && TextUtils.isEmpty(bookDesc) && TextUtils.isEmpty(bookImg)){
Toast.makeText(this,"Please Fill The Blank Field",Toast.LENGTH_LONG).show();
}
else if(!TextUtils.isEmpty(bookName) && !TextUtils.isEmpty(writerName)
&& !TextUtils.isEmpty(publisherName) && !TextUtils.isEmpty(bookDesc) && !TextUtils.isEmpty(bookImg)){
saveBook(bookImg,bookName,writerName,publisherName,bookDesc);
}
}
private void saveBook(String url, String bookName, String writerName, String publisherName, String bookDesc) {
DatabaseReference bookRef = databaseReference.child("Books").child(adminId);
String bookId = bookRef.push().getKey();
if(bookId!=null){
Book book = new Book(bookId,url,bookName,writerName,publisherName,bookDesc);
bookRef.child(bookId).child("BookInfo").setValue(book).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
}
});
}
}
}
My BookAdapter
public class BookAdapter extends RecyclerView.Adapter {
private List<Book> books;
private Context context;
public BookAdapter(List<Book> books, Context context) {
this.books = books;
this.context = context;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_book_layout,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
final Book currentBook = books.get(position);
// holder.bookIv.setIm(currentBook.getBookImage());
holder.bookNameTv.setText(currentBook.getBookName());
holder.writerNameTv.setText(currentBook.getWriterName());
holder.publisherNameTv.setText(currentBook.getPublisherName());
holder.shortDescTv.setText(currentBook.getShortDesc());
}
#Override
public int getItemCount() {
return books.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView bookNameTv,writerNameTv,publisherNameTv,shortDescTv;
private ImageView bookIv;
public ViewHolder(#NonNull View itemView) {
super(itemView);
bookIv = itemView.findViewById(R.id.bookListIvId);
bookNameTv = itemView.findViewById(R.id.bookListNameTvId);
writerNameTv = itemView.findViewById(R.id.bookListWriterNameTvId);
publisherNameTv = itemView.findViewById(R.id.bookListPubNameTvId);
shortDescTv = itemView.findViewById(R.id.bookListShortDescTvId);
}
}
}
Now when I am debugging my app the debugger showing me the Book class reference book is all field null.
Any help me for appreciated
It's because you are creating book object in app and book is not fetched from firebase. You need to query for books using adminId
You have to go one more level deep to get BookInfo. Try using below code:
showBookRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot data: dataSnapshot.getChildren()) {
Book book = data.child("BookInfo").getValue(Book.class);
bookList.add(book);
}
bookAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
You have to have your BookInfo directly outside i.e. YourRootId->SubId->YourInfo but this is what you have used as YourRootId->SubId->BookInfo->YourInfo (this is right if you want to retrieve only that particular data)
So First setup a FirebaseRecyclerAdapter
Query query = FirebaseDatabase.getInstance()
.getReference()
.child("yourRootId/");
FirebaseRecyclerOptions<Book> options = FirebaseRecyclerOptions.Builder<Book>()
.setQuery(query, Book.class)
.build();
FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Book, BookHolder>(options) {
#Override
public BookHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// Create a new instance of the ViewHolder, in this case we are using a custom
// layout called R.layout.message for each item
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.yourlayout, parent, false);
return new BookHolder(view);
}
#Override
protected void onBindViewHolder(BookHolder holder, int position, Book model) {
// assign all data in here
}
};
then create a viewholder
public class BookHolder extends RecyclerView.ViewHolder {
public LinearLayout root;
public TextView abc;
public TextView xyz;
public ViewHolder(View itemView) {
super(itemView);
abc= itemView.findViewById(R.id.list_title);
xyz= itemView.findViewById(R.id.list_desc);
}
}

Populating RecyclerView from firebase database

I am new to Android developing and trying to display data from Firebase Database to Recyclerview but when I run the application the Users Activity is blank or nothing displayed on my RecyclerView.
I'm stuck and still the output is the same: nothing is displayed.
UsersActivity:
private Toolbar mToolbar;
private RecyclerView mUsersList;
private DatabaseReference mUsersDatabase;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_users);
mToolbar = (Toolbar) findViewById(R.id.users_appBar);
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle("Users");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mToolbar.setTitleTextColor(Color.WHITE);
mToolbar.setSubtitleTextColor(Color.WHITE);
mUsersDatabase = FirebaseDatabase.getInstance().getReference().child("Users");
mUsersList = (RecyclerView) findViewById(R.id.users_list);
mUsersList.setHasFixedSize(true);
mUsersList.setLayoutManager(new LinearLayoutManager(this));
}
#Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Users, UsersViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Users, UsersViewHolder>(
Users.class,
R.layout.users_single_layout,
UsersViewHolder.class,
mUsersDatabase
) {
#Override
protected void populateViewHolder(UsersViewHolder usersViewHolder, Users users, int i) {
usersViewHolder.setName(users.getName());
}
};
mUsersList.setAdapter(firebaseRecyclerAdapter);
}
public static class UsersViewHolder extends RecyclerView.ViewHolder
{
View mView;
public UsersViewHolder(#NonNull View itemView) {
super(itemView);
mView = itemView;
}
public void setName(String name)
{
TextView userNameView = (TextView) mView.findViewById(R.id.user_name);
userNameView.setText(name);
}
}
and this is my code in my Model class which is Users
private String name;
private String image;
private String status;
public Users()
{
}
public Users(String name, String image, String status) {
this.name = name;
this.image = image;
this.status = status;
}
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 String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
You are using an old version of FirebaseUI, you need to remove mUsersList.setHasFixedSize(true); for the data to be displayed

Categories