Data is not getting retried using firebaseUI [duplicate] - android

This question already has answers here:
FirebaseRecyclerAdapter unable to populate result [duplicate]
(1 answer)
FirebaseListAdapter not pushing individual items for chat app - Firebase-Ui 3.1
(2 answers)
Closed 5 years ago.
I am following a youtube video in which I was trying to retrieve data using FirebaseUI but I am not able to show any data in the recyclerview.
Here is the code
public class MemeFragment extends Fragment {
RecyclerView recyclerView;
FirebaseDatabase firebaseDatabase;
DatabaseReference myref;
FirebaseRecyclerAdapter<Datafileinfo, ShowDataViewHolder> firebaseRecyclerAdapter;
FirebaseRecyclerOptions<Datafileinfo> options;
public MemeFragment() {
// 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_meme, container, false);
firebaseDatabase = FirebaseDatabase.getInstance();
myref = FirebaseDatabase.getInstance().getReference("memes");
recyclerView = (RecyclerView) view.findViewById(R.id.recyc);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
options = new FirebaseRecyclerOptions.Builder<Datafileinfo>()
.setQuery(myref, Datafileinfo.class)
.build();
return view;
}
#Override
public void onStart() {
super.onStart();
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Datafileinfo, ShowDataViewHolder>
(options) {
#Override
protected void onBindViewHolder(#NonNull ShowDataViewHolder holder, int position, #NonNull Datafileinfo model) {
holder.ImageTitle(model.getTitle());
holder.ImageUrl(model.getImage());
}
#Override
public ShowDataViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.custo, parent, false);
return new ShowDataViewHolder(view);
}
};
recyclerView.setAdapter(firebaseRecyclerAdapter);
}
public static class ShowDataViewHolder extends RecyclerView.ViewHolder {
private ImageView imageView;
private TextView textView;
public ShowDataViewHolder(View itemView) {
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.imageView);
textView = (TextView) itemView.findViewById(R.id.textView);
}
private void ImageTitle(String title) {
textView.setText(title);
}
private void ImageUrl(String url) {
Glide.with(itemView.getContext()).
load(url).
into(imageView);
}
}
}
and datafileinfo is the getter and setter class I have also taken the internet permission.Note I am able to get the single value from firebase but for multiple values, I am facing a problem this is the database.
where I am doing wrong any idea??
Note I have followed and gone through all the tutorials and similar post.

Create a separate Class for your ViewHolder
public class ShowDataViewHolder extends RecyclerView.ViewHolder{
private ImageView imageView;
private TextView textView;
public ShowDataViewHolder (View itemView) {
super(itemView);
imageView = (ImageView)itemView.findViewById(R.id.imageView);
textView = (TextView) itemView.findViewById(R.id.textView);
}
}
In your MenuFragment add
DatabaseReference mDatabaseMemes= //your reference to memes node
FirebaseRecyclerAdapter<yourModel, ShowDataViewHolder> adapter= new FirebaseRecyclerAdapter<yourModel, ShowDataViewHolder>(yourModel.class, R.layout.yourLayout,
ShowDataViewHolder.class,mDatabaseMemes) {
#Override
protected void populateViewHolder(ShowDataViewHolder viewHolder, yourModel model, int position) {
viewHolder.textView.setText(model.getTitle());
Glide.with(context)
.load(model.getImage())
.fitCenter()
.into(viewHolder.imageView);
}
};
recyclerView.setAdapter(adapter);
}
MAKE SURE your model class has String title and image same as your child names under memes node.

Related

Retrieving data from firebase to recyclerview does not work

As mentioned in the title, my recyclerview does not work. No idea why, because I do not get any errors or anything in the logcat or while building the app. Is there a way I could debug this or could you point out the mistake that I did?
I've implemented another recyclerview with the adapter, but I used a simple adapter. I tried using the firebaseUI in this case, and it seems that the adapter somehow cannot retrieve the data from the firebase firestore.
https://imgur.com/a/JSLXuou The screenshot of my database structure.
My adapter:
public class ManageAdapter extends FirestoreRecyclerAdapter<Manage, ManageAdapter.ManageHolder> {
public ManageAdapter(#NonNull FirestoreRecyclerOptions<Manage> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull ManageHolder holder, int position, #NonNull Manage model) {
holder.textviewName.setText(model.getName());
holder.textviewGrade.setText(model.getGrade());
//holder.textviewAge.setText(String.valueOf(model.getAge()));
}
#NonNull
#Override
public ManageHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.manage_item,
parent,false);
return new ManageHolder(v);
}
class ManageHolder extends RecyclerView.ViewHolder{
TextView textviewName;
TextView textviewAge;
TextView textviewGrade;
public ManageHolder(#NonNull View itemView) {
super(itemView);
textviewAge = itemView.findViewById(R.id.manage_item_title_age);
textviewGrade = itemView.findViewById(R.id.manage_item_grade);
textviewName = itemView.findViewById(R.id.manage_item_name);
}
}
}
Main class:
private FirebaseFirestore firebaseFirestore;
private Toolbar mToolbar;
private Button delete_event;
private RecyclerView participants;
private TextView manage_events_title;
private TextView participans_text;
private ManageAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manage_events);
final String ID = getIntent().getExtras().getString("ID");
firebaseFirestore = FirebaseFirestore.getInstance();
delete_event = (Button) findViewById(R.id.delete_event);
manage_events_title = (TextView) findViewById(R.id.manage_events_title);
participans_text = (TextView) findViewById(R.id.participants_text);
participants = (RecyclerView) findViewById(R.id.recyclerview_manage);
setUpRecyclerView();
}
private void setUpRecyclerView(){
final String ID = getIntent().getExtras().getString("ID");
Query query = firebaseFirestore.collection("Events").document(ID).collection("Users");
FirestoreRecyclerOptions<Manage> options = new FirestoreRecyclerOptions.Builder<Manage>()
.setQuery(query, Manage.class).build();
adapter = new ManageAdapter(options);
participants.setHasFixedSize(true);
participants.setLayoutManager(new LinearLayoutManager(this));
participants.setAdapter(adapter);
}
#Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
}

Static context error when using firebaseRecyclerAdapter [duplicate]

This question already has answers here:
What is the reason behind "non-static method cannot be referenced from a static context"? [duplicate]
(13 answers)
Closed 3 years ago.
Non-static method cannot be referenced from a non-static context. The problem is coming from my PersonHolder.setPerson(person) with setPerson being underlined. Is this because the setPerson is inside of my onCreate?
I have 3 classes and 4 activity_layouts.
my activity_main class is just for the menu activity_main - just a
menu
Person class
ReadFirebase class attached to my activity_read_firebase which is
meant to pull the data from firebase and put it into my recycler.
WriteToFirebase class used to send data to the firebase.
The last activity is my firebase_item_file that contains the items I require for my recycler in my activity_write_to_firebase.
public class ReadFirebase extends AppCompatActivity {
private FirebaseRecyclerAdapter<Player, PersonHolder> firebaseRecyclerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_read_firebase);
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
DatabaseReference roofRef = FirebaseDatabase.getInstance().getReference();
Query query = roofRef.child("adult").child("male");
FirebaseRecyclerOptions<Person> firebaseRecyclerOptions = new FirebaseRecyclerOptions.Builder<Person>()
.setQuery(query, Person.class)
.build();
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Person, PersonHolder>(firebaseRecyclerOptions) {
#Override
protected void onBindViewHolder(#NonNull PersonHolder personHolder, int i, #NonNull Person person) {
PersonHolder.setPerson(person);
}
#NonNull
#Override
public PersonHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.firebase_item_file, parent, false);
return new PersonHolder(view);
}
};
recyclerView.setAdapter(firebaseRecyclerAdapter);
}
private class PersonHolder extends RecyclerView.ViewHolder {
private TextView name1;
private TextView name2;
private TextView name3;
private TextView age1;
private TextView age2;
private TextView age3;
PersonHolder(View itemView){
super(itemView);
pNameOne = itemView.findViewById(R.id.name1);
pNameTwo = itemView.findViewById(R.id.name2);
pNameThree = itemView.findViewById(R.id.name3);
pAgeOne = itemView.findViewById(R.id.age1);
pAgeTwo = itemView.findViewById(R.id.age2);
pAgeThree = itemView.findViewById(R.id.age3);
}
void setPerson(Person person){
String pName1 = person.getName();
pNameOne.setText(playerName1);
String pName2 = person.getName();
pNameTwo.setText(playerName2);
String pName3= person.getName();
pNameThree.setText(playerName3);
int pAge1= person.getAge();
pAgeOne.setText(Integer.toString(personAge1));
int pAge2 = person.getAge();
pAgeTwo.setText(Integer.toString(personAge2));
int pAge3 = person.getAge();
pAgeThree.setText(Integer.toString(personAge3));
}
}
Non-static method cannot be referenced from a non-static context.
You are trying to access method setPerson using class PersonHolder in onBindViewHolder
You should use instance of PersonHolder which you get in onBindViewHolder as parameter
#Override
protected void onBindViewHolder(#NonNull PersonHolder personHolder, int i, #NonNull Person person) {
personHolder.setPerson(person);
}

rycyclerview is not showing data using firebaserecyclerAdpater

i have real time database and getting data through firebaserecyclerAdapter from realtime database and want to show in rcylerview . by using database reference code is accessing firebase database but fields are not showing in recyclerview. However the data is not showing only in my application.
public class find_friends extends AppCompatActivity {
RecyclerView find_friends_recyclerList;
DatabaseReference databaseReference;
FirebaseRecyclerAdapter<Contacts,findFriendsViewHolder> adapter;
FirebaseRecyclerOptions<Contacts> options;
Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find_friends);
find_friends_recyclerList=findViewById(R.id.find_friends_recyclerList);
find_friends_recyclerList.setHasFixedSize(true);
databaseReference= FirebaseDatabase.getInstance().getReference().child("Users");
options= new FirebaseRecyclerOptions.Builder<Contacts>()
.setQuery(databaseReference, Contacts.class)
.build();
adapter=new FirebaseRecyclerAdapter<Contacts, findFriendsViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull findFriendsViewHolder holder, int position, #NonNull Contacts model) {
holder.name.setText(model.getName());
holder.status.setText(model.getStatus());
Picasso.get().load(model.getImages()).into(holder.profileImage);
}
#NonNull
#Override
public findFriendsViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view=inflater.inflate(R.layout.friendlist_item,parent,false);
return new findFriendsViewHolder(view);
}
};
find_friends_recyclerList.setAdapter(adapter);
adapter.startListening();
}
public static class findFriendsViewHolder extends RecyclerView.ViewHolder{
TextView name,status;
CircleImageView profileImage;
public findFriendsViewHolder(View itemView) {
super(itemView);
name=itemView.findViewById(R.id.name);
status=itemView.findViewById(R.id.phone);
profileImage=itemView.findViewById(R.id.friend_image);
}
}
}
You must provide a layout manager for the recycler view. For instance, a LinearLayoutManager.
Add this line of code:
find_friends_recyclerList.setLayoutManager(new LinearLayoutManager(this))

Fragment isn't displaying RecyclerView [duplicate]

This question already has answers here:
How to implement Firebase Recycler Adapter in newer version of Android 3.1 and higher?
(2 answers)
Closed 3 years ago.
So, I watched some tutorial on how to do the RecyclerView mainly this. I tweaked the code to fit my needs. But when i try to ran it, the RecyclerView isn't displaying on the fragment. My guess is the problem lies in adapter and viewholder part, because i'm not sure how to implement it yet. Can anyone help me?
The Fragment.java
public class JobListFragment extends Fragment {
RecyclerView mRecyclerView;
FirebaseDatabase firebaseDatabase;
DatabaseReference databaseReference;
public JobListFragment(){
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
((MainHomeActivity)getActivity()).setActionBarTitle("Job List");
View view = inflater.inflate(R.layout.fragment_job_list, container, false);
firebaseDatabase = FirebaseDatabase.getInstance();
databaseReference = firebaseDatabase.getReference().child("Data");
mRecyclerView = view.findViewById(R.id.recycleView);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
mRecyclerView.setHasFixedSize(true);
return view;
}
#Override
public void onStart() {
super.onStart();
FirebaseRecyclerOptions<Model> options =
new FirebaseRecyclerOptions.Builder<Model>()
.setQuery(databaseReference, Model.class).build();
FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Model, ViewHolder>(options) {
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.row, viewGroup, false);
return new ViewHolder(view);
}
#Override
protected void onBindViewHolder(#NonNull ViewHolder holder, int position, #NonNull Model model) {
holder.setDetails(model.getTitlept(), model.getDescpt(), model.getImagept());
}
};
mRecyclerView.setAdapter(adapter);
}
}
ViewHolder.java
public class ViewHolder extends RecyclerView.ViewHolder {
View mView;
public ViewHolder(View itemView){
super(itemView);
mView = itemView;
}
public void setDetails(String title, String description, String image){
TextView mTitleTv = mView.findViewById(R.id.rTitleTv);
TextView mDescTv = mView.findViewById(R.id.rDescIv);
ImageView mImageIv = mView.findViewById(R.id.rImageIv);
mTitleTv.setText(title);
mDescTv.setText(description);
Picasso.get().load(image).into(mImageIv);
}
}
Model.java
public class Model {
String titlept, imagept, descpt;
public Model(){}
public String getTitlept() {
return titlept;
}
public void setTitlept(String titlept) {
this.titlept = titlept;
}
public String getImagept() {
return imagept;
}
public void setImagept(String imagept) {
this.imagept = imagept;
}
public String getDescpt() {
return descpt;
}
public void setDescpt(String descpt) {
this.descpt = descpt;
}
}
Can you write the problem a little bit more clearly? Picture not displayed? #AdhityoPutro

RecyclerView with setOnClickListener

I'm using Firebase as a database and a preview to get information about the saved data, now I can bring both the image and the name of that image in my view, but now I would like to be able to click on the quick view and open a new fragment or activity where the same image or publication can be displayed that is clicked, that is, the photo, the name and the power to include other data within that new fragment or activity when clicking on the recyclerview post.
In a few words for example: a clothing store, you have a recycling view with your products and your name, click on that product opens but with all the other details and specifications you have.
I am currently using: Firebase navigation button with fragments
Fragment where the recyclerview is shown:
mRecyclerView = v.findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mUploads = new ArrayList<>();
mDatabaseRef = FirebaseDatabase.getInstance().getReference("Posts");
mDatabaseRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
Uploads uploads = postSnapshot.getValue(Uploads.class);
mUploads.add(uploads);
}
mAdapter = new ImageAdapter(getActivity(), mUploads);
mRecyclerView.setAdapter(mAdapter);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});return v;
//adapter code
public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ImageViewHolder> {
private Context mContext;
private List<Uploads> mUploads;
public ImageAdapter(Context context, List<Uploads> uploads) {
mContext = context;
mUploads = uploads;
}
#Override
public ImageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(mContext).inflate(R.layout.home_item, parent, false);
return new ImageViewHolder(v);
}
#Override
public void onBindViewHolder(ImageViewHolder holder, final int position) {
Uploads uploadCurrent = mUploads.get(position);
holder.textViewName.setText(uploadCurrent.getName());
Picasso.get()
.load(uploadCurrent.getImageUrl())
.placeholder(R.mipmap.ic_launcher)
.into(holder.imageView);
}
#Override
public int getItemCount() {
return mUploads.size();
}
public class ImageViewHolder extends RecyclerView.ViewHolder {
public TextView textViewName;
public ImageView imageView;
public ImageViewHolder(View itemView) {
super(itemView);
textViewName = itemView.findViewById(R.id.Descripcion);
imageView = itemView.findViewById(R.id.ImagePro);
}
}
}
You have two options.
You can create a callback interface and trigger the callback method from your Adapter's onClick. The activity or fragment that implemented the interface and passed the instance of it to the Adapter will get the callback and you can launch a quick view from there. I prefer this method.
Here you can find a detailed example to achieve this.
Since you already have Context in your class, you can directly reference a method in an Activity or Fragment and create a quick view Fragment from there.
public void onBindViewHolder(ImageViewHolder holder, final int position) {
((YourActivity) context).startQuickviewFragment();
}

Categories

Resources