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");
Related
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.
I'm retrieving data from firebase and trying to display that in recycler view, every thing is fine but it only shows one item in the list (there are total 14 items)
comment if you do not understand the question
This is my main activity where i get datasnapshots from firebase
public class cakes_activity extends AppCompatActivity {
RecyclerView recyclerView;
ArrayList<Data> listdata;
private DatabaseReference mref;
private NorthAdaptor north;
private final Context context = this;
public int counter = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cakes);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
EditText editText = (EditText) findViewById(R.id.editText) ;
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//FIREBASE
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mref= FirebaseDatabase.getInstance().getReference("Cakes");
mref.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Data data = dataSnapshot.getValue(Data.class);
listdata = new ArrayList<>();
listdata.add(data);
north = new NorthAdaptor(context,listdata);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
recyclerView.hasFixedSize();
recyclerView.setAdapter(north);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
and the following code is my adaptor class:
public class NorthAdaptor extends RecyclerView.Adapter<NorthAdaptor.NorthHolder> {
private LayoutInflater inflater;
ArrayList<Data> northy;
public NorthAdaptor(Context context,ArrayList<Data> northy)
{
inflater=LayoutInflater.from(context);
this.northy=northy;
}
public NorthHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.each_item,parent,false);
NorthHolder holder = new NorthHolder(view);
return holder;
}
#Override
public void onBindViewHolder(NorthHolder holder, int position) {
holder.text2.setText(northy.get(position).getName());
holder.text3.setText("₹."+northy.get(position).getRS());
}
#Override
public int getItemCount() {
return northy.size();
}
class NorthHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
TextView text,text2,text3;
ImageView img;
private final Context context;
public NorthHolder(View itemview)
{
super(itemview);
context = itemView.getContext();
CardView card = (CardView) itemView.findViewById(R.id.card);
text3=(TextView)itemView.findViewById(R.id.Rs);
text2 = (TextView) itemView.findViewById(R.id.itemtext2);
img = (ImageView) itemView.findViewById(R.id.itemimage);
card.setOnClickListener(this);
}
#Override
public void onClick(View view){
Toast.makeText(
cakes_activity.this,
"Your message here",
Toast.LENGTH_SHORT
).show();
}
}
}
this is my java class to store the data received from firebase
public class Data {
private String ID,Name,image,RS;
public Data(){
}
public Data(String ID, String Name, String image, String RS) {
this.ID = ID;
this.Name = Name;
this.image= image;
this.RS = RS;
}
public String getId() {
return ID;
}
public String getRS() {
return RS;
}
public void setId(String id) {
this.ID = id;
}
public String getName() {
return Name;
}
public void setName(String Name) {
this.Name = Name;
}
public String getimage() {
return image;
}
public void setImg(String image) {
this.image = image;
}
}
listdata = new ArrayList<>();
north = new NorthAdaptor(context,listdata);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
recyclerView.hasFixedSize();
recyclerView.setAdapter(north);
you need to write this code in onCreate instead of onchildadded
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.
i am a newbie and i am trying to add some data in to the recyclerview from firbase db.but it showing the not serializable error. i searched everywhere and found couple of answers to make classes static and variables public but still not working, here are my codes
My Adapter
public class CardViewAdapter extends FirebaseRecyclerAdapter<MainpageCardModel,CardViewAdapter.Recyclerviewholder>{
public Context mcontext;
public List<MainpageCardModel> cakeslist;
public CardViewAdapter(DatabaseReference ref){
super(MainpageCardModel.class,R.layout.card,CardViewAdapter.Recyclerviewholder.cl ass,ref);
}
#Override
protected void populateViewHolder(Recyclerviewholder viewHolder, MainpageCardModel model, int position) {
viewHolder.ItemName.setText(MainpageCardModel.getItemName());
viewHolder.ItemPrice.setText(MainpageCardModel.getItemPrice());
if(MainpageCardModel.getItemImage() == null){
viewHolder.CardviewImageView.setImageDrawable(ContextCompat.getDrawable(mcontext,
R.drawable.ic_cake_dark));
}
else {
Glide.with(mcontext)
.load(MainpageCardModel.getItemImage())
.into(viewHolder.CardviewImageView);
}
}
public static class Recyclerviewholder extends RecyclerView.ViewHolder {
public ImageView CardviewImageView;
public TextView ItemName, ItemPrice;
public Recyclerviewholder(View itemView) {
super(itemView);
CardviewImageView = (ImageView) itemView.findViewById(R.id.cakepic_cardview);
ItemName = (TextView) itemView.findViewById(R.id.caketitle_cardview);
ItemPrice = (TextView) itemView.findViewById(R.id.cakeprice_cardview);
}
}
My model class
public Class MainpageCardModel{
public static String itemName;
public static String itemImage;
public static String itemPrice;
public MainpageCardModel(){
}
public MainpageCardModel(String itemName, String itemImage, String itemPrice){
this.itemImage=itemImage;
this.itemName=itemName;
this.itemPrice=itemPrice;
}
public static String getItemName() {
return itemName;
}
public void setItemName(String itemName){
this.itemName = itemName;
}
public static String getItemImage(){
return itemImage;
}
public void setItemImage(String itemImage){
this.itemImage= itemImage;
}
public static String getItemPrice(){
return itemPrice;
}
public void setItemPrice(String itemPrice) {
this.itemPrice = itemPrice;
}
}
My Fragment
private static final String TAG = "FreshCreamFragment";
public static final String MENU_CHILD = "FreshCream";
private String Cakename, cakeprice, cakeurl;
private RecyclerView mRecyclerView;
private DatabaseReference mFirebaseDatabaseReference;
private CardViewAdapter adapterl;
private GridLayoutManager gridLayoutManager;
public List<MainpageCardModel> cakeslist;
ProgressBar mprogressbar;
MainpageCardModel mainpageCardModel;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
cakeslist= new ArrayList<>();
adapterl = new CardViewAdapter(mFirebaseDatabaseReference.child(MENU_CHILD));
mFirebaseDatabaseReference = FirebaseDatabase.getInstance().getReference().child("menu");
mFirebaseDatabaseReference.child(MENU_CHILD).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Map<String,String> cakedetails = (Map)dataSnapshot.getValue();
Cakename= cakedetails.get("Item_name");
cakeprice = cakedetails.get("Item_price");
cakeurl = cakedetails.get("url");
mainpageCardModel = new MainpageCardModel(Cakename,cakeurl,cakeprice);
cakeslist.add(mainpageCardModel);
adapterl.notifyDataSetChanged();
mprogressbar.setVisibility(ProgressBar.INVISIBLE);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
};
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.fresh_cream_fragment, container, false);
mprogressbar = (ProgressBar) rootview.findViewById(R.id.progressBar3);
mRecyclerView = (RecyclerView) rootview.findViewById(R.id.freshcream_recyclerview);
gridLayoutManager = new GridLayoutManager(getContext(),2);
mRecyclerView.setLayoutManager(gridLayoutManager);
mRecyclerView.setAdapter(adapterl);
return rootview;
}
}
MYdb
mydbscreenshot
looks like you have following lines in wrong order?
adapterl = new CardViewAdapter(mFirebaseDatabaseReference.child(MENU_CHILD));
mFirebaseDatabaseReference = FirebaseDatabase.getInstance().getReference().child("menu");
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