populateViewHolder() isn't getting executed in a FirebaseRecyclerAdapter - android

My RecyclerView was working until I decided to start adding Tab Slide layout, so I moved it along with it's Viewholder to a Fragment class.
However, proved by debugging, the method populateViewHolder() isn't getting called. Here's my Fragment's onCreate():
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//region RecyclerView
View v = View.inflate(getApplicationContext(),R.layout.activity_recent_occasions,null);
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Campaigns");
RecyclerView list = (RecyclerView) v.findViewById(R.id.list);
list.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
FirebaseRecyclerAdapter mAdapter = new FirebaseRecyclerAdapter<Campaign,CampaignHolder>(Campaign.class,R.layout.recyclerview_template,CampaignHolder.class,ref) {
#Override
protected void populateViewHolder(CampaignHolder viewHolder, Campaign model, int position) {
viewHolder.setTitle(model.title);
viewHolder.setDescription(model.description);
viewHolder.setAuthor(model.author);
viewHolder.setImage(model.image,getApplicationContext(),RecentOccasions.this);
}
};
list.addItemDecoration(new HorizontalDividerItemDecoration.Builder(getApplicationContext()).build());
list.setAdapter(mAdapter);
//endregion
}
My model:
public class Campaign {
public String title;
public String description;
public String image;
public String author;
public Campaign() {
}
public Campaign(String title, String description, String image, String author) {
this.title = title;
this.description = description;
this.image = image;
this.author = author;
}
}
What I've tried: Everything here and here

Related

Using FirestoreRecyclerAdapter but cannot query any data from Firebase

Base on guideline at https://github.com/firebase/FirebaseUI-Android/tree/master/firestore, I make a list items with FirestoreRecyclerAdapter but I don't know why the couldn't retrieve any data. In fact, the number view items are shown correctly, but the content always null. Anyone can help me. Below is source code:
Activity:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private FirebaseFirestore mDatabase;
private ViewPager mViewPager;
private CollectionReference mOrderRef;
private OrderAdapter mOrderAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_order_list);
mDatabase = FirebaseFirestore.getInstance();
mOrderRef = mDatabase.collection("order");
Query query = mOrderRef.limit(1000);
FirestoreRecyclerOptions<OrderInfo> options = new FirestoreRecyclerOptions.Builder<OrderInfo>().setQuery(query, OrderInfo.class).build();
mOrderAdapter = new OrderAdapter(options);
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recycler_view_order_list);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(mOrderAdapter);
}
#Override
protected void onStart() {
super.onStart();
mOrderAdapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
mOrderAdapter.stopListening();
}
Adapter:
public class OrderAdapter extends FirestoreRecyclerAdapter<OrderInfo, OrderAdapter.OrderHolder> {
/**
* Create a new RecyclerView adapter that listens to a Firestore Query. See {#link
* FirestoreRecyclerOptions} for configuration options.
*
* #param options
*/
public OrderAdapter(#NonNull FirestoreRecyclerOptions<OrderInfo> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull OrderHolder holder, int position, #NonNull OrderInfo model) {
holder.txtTitle.setText(String.valueOf(model.getTitle()));
holder.txtCusName.setText(String.valueOf(model.getCusName()));
holder.txtDate.setText(String.valueOf(model.getDate()));
}
#NonNull
#Override
public OrderHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.order_item, parent, false);
return new OrderHolder(view);
}
class OrderHolder extends RecyclerView.ViewHolder{
TextView txtCusName;
TextView txtTitle;
TextView txtDate;
public OrderHolder(#NonNull View itemView) {
super(itemView);
txtCusName = itemView.findViewById(R.id.txt_customer_name);
txtTitle = itemView.findViewById(R.id.txt_title);
txtDate = itemView.findViewById(R.id.txt_date);
}
}
}
Data model:
public class OrderInfo {
private String mTitle;
private String mCusName;
private String mDate;
public OrderInfo(){
//default constructor
}
public OrderInfo(String title, String name, String date) {
this.mTitle = title;
this.mCusName = name;
this.mDate = date;
}
public String getTitle() {
return mTitle;
}
public String getCusName() {
return mCusName;
}
public void setCusName(String cusName) {
this.mCusName = cusName;
}
public String getDate() {
return mDate;
}
}
my database structure:
You data model is not aligned with database structure. Try to update it like below:
public class OrderInfo {
private String title;
private String name;
private String date;
public OrderInfo(){
//default constructor
}
public OrderInfo(String title, String name, String date) {
this.title = title;
this.name = name;
this.date = date;
}
public String getTitle() {
return title;
}
public String getName() {
return name;
}
public String getDate() {
return date;
}
}
Besides this don't forget to sync your project with firebase firestore.

FirebaseRecyclerAdapter database exception

I created a model class and layout for single item and then created view holder and firebaseRecyclerAdapter as shown in code below and when I run it gives me this error
com.google.firebase.database.DatabaseException: Can't
convert object of type java.lang.String to type
com.best.karem.letsplay.Model.PsModel
I tried adding onValueEventListener but it is not working too
Model Class
public class PsModel {
private String title;
private String location;
private String thumb_image;
public PsModel(String title, String location, String thumb_image) {
this.title = title;
this.location = location;
this.thumb_image = thumb_image;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getThumb_image() {
return thumb_image;
}
public void setThumb_image(String thumb_image) {
this.thumb_image = thumb_image;
}
public PsModel(){
}
}
Main Class
public class Ps extends AppCompatActivity {
private Toolbar toolbar;
private FloatingActionButton psBtn;
private RecyclerView recyclerView;
private FirebaseAuth auth;
private DatabaseReference psRef;
private String currentUser;
private FirebaseRecyclerAdapter<PsModel , PsViewHolder> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ps);
auth = FirebaseAuth.getInstance();
currentUser = auth.getCurrentUser().getUid();
psRef = FirebaseDatabase.getInstance().getReference().child("Posts").child(currentUser).child("Ps");
recyclerView = findViewById(R.id.ps_recyclerview);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new FirebaseRecyclerAdapter<PsModel, PsViewHolder>(
PsModel.class,
R.layout.list_item,
PsViewHolder.class,
psRef
) {
#Override
protected void populateViewHolder(PsViewHolder viewHolder, PsModel model, int position) {
viewHolder.itemTitle.setText(model.getTitle());
viewHolder.itemLocation.setText(model.getLocation());
Picasso.get().load(model.getThumb_image()).into(viewHolder.itemImage);
}
};
recyclerView.setAdapter(adapter);
}
public static class PsViewHolder extends RecyclerView.ViewHolder{
public ImageView itemImage;
public TextView itemTitle , itemLocation;
public PsViewHolder(#NonNull View itemView) {
super(itemView);
itemImage = itemView.findViewById(R.id.item_image);
itemTitle = itemView.findViewById(R.id.item_title);
itemLocation = itemView.findViewById(R.id.item_location);
}
}
FIREBASE DATA STRUCTURE IMAGE
and Please don't refer me to another answer on this site because I searched for this for 4 hours and tried everything and didn't work
Can you check the content of psRef?
I understand it's coming as String and expected as an object.
psRef = FirebaseDatabase.getInstance().getReference().child("Posts").child(currentUser).child("Ps");

RecyclerView onCreateViewHolder does not run

I have search for hours and i'm sure its something simple but i can't find the answer. onCreateViewHolder and onBindViewHolder does not run. can someone help me out. thanks
MainActivity.java
public class MainActivity extends AppCompatActivity {
private RecyclerView mBlogList;
private DatabaseReference mDatabase;
private Query query = FirebaseDatabase.getInstance().getReference().child("Blog").limitToLast(50);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDatabase = FirebaseDatabase.getInstance().getReference().child("Blog");
// query = FirebaseDatabase.getInstance().getReference().child("Blog").limitToLast(50);
mBlogList = findViewById(R.id.blog_list);
mBlogList.setHasFixedSize(true);
mBlogList.setLayoutManager(new LinearLayoutManager(this)); //sets the orientation to vertical
}
#Override
protected void onStart() {
super.onStart();
Toast.makeText(this, "1", Toast.LENGTH_SHORT).show();
FirebaseRecyclerOptions<Blog> options = new FirebaseRecyclerOptions.Builder<Blog>()
.setQuery(query, Blog.class)
.build();
Toast.makeText(this, "2", Toast.LENGTH_SHORT).show();
FirebaseRecyclerAdapter firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Blog, BlogViewHolder>(options) {
#Override
public BlogViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.blog_row, parent, false);
Toast.makeText(MainActivity.this, "oh oh", Toast.LENGTH_SHORT).show();
return new BlogViewHolder(view);
}
#Override
protected void onBindViewHolder(BlogViewHolder holder, int position, Blog model) {
holder.setTitle(model.getTitle());
holder.setDesc(model.getDesc());
Toast.makeText(MainActivity.this, "oh si", Toast.LENGTH_SHORT).show();
}
};
mBlogList.setAdapter(firebaseRecyclerAdapter);
}
public static class BlogViewHolder extends RecyclerView.ViewHolder {
View mView;
public BlogViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setTitle(String title) {
TextView post_title = mView.findViewById(R.id.post_title);
post_title.setText(title);
}
public void setDesc(String desc) {
TextView post_desc = mView.findViewById(R.id.post_desc);
post_desc.setText(desc);
}
}
#Override
public boolean onCreateOptionsMenu (Menu menu){
getMenuInflater().inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected (MenuItem item){
if (item.getItemId() == R.id.action_add) {
startActivity(new Intent(getApplicationContext(), Postctivity.class));
}
return super.onOptionsItemSelected(item);
}
}
Blog.class
public class Blog {
private String title;
private String desc;
private String image;
public Blog(){
}
public Blog(String title, String desc, String image) {
this.title = title;
this.desc = desc;
this.image = image;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}
blog_row.xml
public class Blog {
private String title;
private String desc;
private String image;
public Blog(){
}
public Blog(String title, String desc, String image) {
this.title = title;
this.desc = desc;
this.image = image;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}
According to the documentation for the library you're using, you have to call adapter.startListening() to actually read information from your database.
The FirebaseRecyclerAdapter uses an event listener to monitor changes to the Firebase query. To begin listening for data, call the startListening() method. You may want to call this in your onStart() method. Make sure you have finished any authentication necessary to read the data before calling startListening() or your query will fail.
#Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
It looks like you're doing all the setup of your adapter in onStart() already, so I think you can just add one line at the end:
firebaseRecyclerAdapter.startListening();
Noob trying to learn here. The recyclerView was not downloading the content from the firebase database. What I did to fix it was changing the from setHasFixedSize(true) to setHasFixedSize(false) Im not sure why it fixed it but it did. I know my question is confusing my english is not very good. I hope this help.

WebView implementation with Firebase Database in Android

I want to implement webView with Firebase so that if I add a URL in firebase Database it gets implemented on my webView in application in realtime, I have successfully implemented the text and images through database but could not get webView work correctly, I have searched many options but neither work for me, please help. If you want any more detail I can provide you.
I am using Android Studio
Below is my Java class code for Firebase implementation for Images and text
#Override
public void onStart() {
super.onStart();
FirebaseUser currentUser = mAuth.getCurrentUser();
if (currentUser == null) {
sentToStart();
}
FirebaseRecyclerAdapter <post, postViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<post, postViewHolder>(
post.class,
R.layout.post_row_recycle_home,
postViewHolder.class,
mDatabaseReference
) {
#Override
protected void populateViewHolder(postViewHolder viewHolder, post model, int position) {
viewHolder.setTitle(model.getTitle());
viewHolder.setdescription(model.getDescription());
viewHolder.setimage(getApplicationContext(), model.getImage());
viewHolder.setsource(model.getSource());
}
};
mrecyclerView.setAdapter(firebaseRecyclerAdapter);
}
public static class postViewHolder extends RecyclerViewPager.ViewHolder{
View mView;
public postViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setTitle(String title){
TextView post_title = (TextView)mView.findViewById(R.id.title_cardView);
post_title.setText(title);
}
public void setsource(String source){
TextView post_source = (TextView)mView.findViewById(R.id.source_cardView);
post_source.setText(source);
}
public void setdescription(String description){
TextView post_description = (TextView)mView.findViewById(R.id.description_cardView);
post_description.setText(description);
}
public void setimage(final Context ctx, final String image){
final ImageView post_image = (ImageView)mView.findViewById(R.id.post_image);
Picasso.with(ctx).load(image).networkPolicy(NetworkPolicy.OFFLINE).into(post_image, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError() {
Picasso.with(ctx).load(image).into(post_image);
}
});
}
}
Getter/Setter Java Class
public class post {
private String title;
private String description;
private String image;
private String source;
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public post(){
}
public post(String title, String description, String image, String source) {
this.title = title;
this.description = description;
this.image = image;
this.source = source;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}
Updated: WebView java Activity
public class webViewNews extends AppCompatActivity {
private WebView webviewthis;
private DatabaseReference mDatabaseReference;
private DatabaseReference mdataRef;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview_page);
mdataRef = FirebaseDatabase.getInstance().getReference().child("webView");
webviewthis = (WebView)findViewById(R.id.webView_news);
webviewthis.setWebViewClient(new WebViewClient());
webviewthis.getSettings().setJavaScriptEnabled(true);
webviewthis.getSettings().setLoadsImagesAutomatically(true);
mdataRef.child("webView").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
dataSnapshot.child("webView").getValue();
webviewthis.loadUrl("");
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
#Override
protected void onStart() {
super.onStart();
}
}
First of all, as i see in your code, you are using .child("webView") twice. Once in the DatabaseReference and once when you add the ValueEventListener. Is that what you are having in your database? If there are no duplicate childrens in your database, you need to remove one of them.
You have added a ValueEventListener on mdataRef but you are doing nothing there. Your onDataChange() method is empty. Remember, this method is triggered every time a change is taking place in your database. So in order to display some content, you need to get the data out from the dataSnapshot object according to your database structure and display it right there.
Hope it helps.

FirebaseListAdapter not populating Listview from Firebase Database Android

I've been researching Firebase for a while and decided to give it a try.
I can't seem to figure out why my ListView is not populating.
I can't seem to understand how the adapter looks for our query, for example, I want the string "Artist" and "Title" from my firebase database.
My POJO:
public class Song {
private String mTitle;
private String mArtist;
public Song(String title, String artist) {
mTitle = title;
mArtist = artist;
}
public Song(){
//Needed for firebase
}
public String getTitle() {
return mTitle;
}
public String getArtist() {
return mArtist;
}
public void setTitle(String title) {
mTitle = title;
}
public void setArtist(String artist) {
mArtist = artist;
}
My Activity
public class player extends AppCompatActivity {
FirebaseListAdapter<Song> myAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player2);
ListView songListView = (ListView) findViewById(R.id.songListView);
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
myAdapter = new FirebaseListAdapter<Song>(player.this, Song.class, R.layout.song_item, ref) {
#Override
protected void populateView(View view, Song song, int position) {
TextView textView = (TextView) view.findViewById(android.R.id.text1);
((TextView)view.findViewById(R.id.titleItem)).setText(song.getTitle());
((TextView)view.findViewById(R.id.artistItem)).setText(song.getArtist());
}
};
songListView.setAdapter(myAdapter);
}
}
My database:
Any help would be appreciated!
When you create an adapter at a location, it will be populated with the direct child nodes under that location.
You attach the adapter to the root of the database, so your child node is issue_1. And in your data model issue_1 does not have artist and title properties.
Simplest way to get the code working is to attach the adapter one level lower in the tree:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
DatabaseReference issue1 = ref.child("issue_1");
myAdapter = new FirebaseListAdapter<Song>(player.this, Song.class,
R.layout.song_item, issue1) {

Categories

Resources