When I start the activity, it is showing the error:
Class com.rcpl.agni.Artist does not define a no-argument
constructor.If you are using ProGuard, make sure these constructors
are not stripped.
protected void onStart() {
super.onStart();
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//artistList.clear();
for(DataSnapshot artistSnapshot : dataSnapshot.getChildren()){
Artist artist = artistSnapshot.getValue(Artist.class);
Toast.makeText(MainActivity.this, artist.getArtistName(), Toast.LENGTH_SHORT).show();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Artist.java
package com.rcpl.agni;
/**
* Created by Dell-pc on 23-01-2018.
*/
public class Artist {
String artistId;
String artistName;
String artistGenre;
public Artist(String artistId, String artistName, String artistGenre) {
this.artistId = artistId;
this.artistName = artistName;
this.artistGenre = artistGenre;
}
public String getArtistId() {
return artistId;
}
public String getArtistName() {
return artistName;
}
public String getArtistGenre() {
return artistGenre;
}
}
To solve this, you just need to add the public no-argument constructor to your Artist class.
public Artist() {}
Remember, when the Firebase Realtime Database SDK deserializes objects coming from the database, it requires that any objects to a have a public no-argument constructor. This is needed to instantiate that particular object. All the fields within the object are set by using public setter methods or direct access to public members.
If you don't use a public no-arg constructor, the SDK doesn't know how to create an instance of the class. Most serialization libraries will have the same requirement.
I believe the error is pretty self-explanatory. You just need to add an empty constructor to your Artist class:
package com.rcpl.agni;
/**
* Created by Dell-pc on 23-01-2018.
*/
public class Artist {
String artistId;
String artistName;
String artistGenre;
public Artist(){}
public Artist(String artistId, String artistName, String artistGenre) {
this.artistId = artistId;
this.artistName = artistName;
this.artistGenre = artistGenre;
}
public String getArtistId() {
return artistId;
}
public String getArtistName() {
return artistName;
}
public String getArtistGenre() {
return artistGenre;
}
}
Firebase requires this constructor in order to use it when you call artistSnapshot.getValue(Artist.class);
add the non-argument in the Data class
Run the app with a clear logcat:
Related
I will try to be brief:
I have three classes that have to interact in this:
The server, which receives the message (in a thread).
The contact, which stores messages in its class (in each object).
The chat activity belonging to each user, which has to show the
messages of the corresponding object, the idea is that it is a RecyclerView.(an activity)
The server receives a message, this message would be added to the chat of the specific contact and then the RecyclerView must be updated.
This is the basic Contact class code:
public class Contact implements Serializable {
private String name;
private String ip;
//Here would be a variable that contains the chat strings
public Contact (String name, String ip){
this.name = name;
this.ip = ip;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
}
And then there is the activity of the chat that is launched when selecting a specific user and it contains a RecyclerView where I want the messages to be.
My problem is that, how to make the messages that are stored in the
variable of a specific object appear in the list.
From already thank you very much.
you can use data class instead of serialization
public class Data {
private String text;
private String uid;
private String tnviews;
private String numberlikes;
public Data(String text,String tnviews,String numberlikes,String uid) {
this.text = text;
this.tnviews = tnviews;
this.numberlikes = numberlikes;
this.uid = uid;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
/////////// VIEWS
public String getViews() {
return tnviews;
}
public void setViews(String tnviews) {
this.tnviews = tnviews;
}
//////// likes
public String getLikes() {
return numberlikes;
}
public void setLikes(String numberlikes) {
this.numberlikes = numberlikes;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
you can Update RecyclerView Adapter Data
mAdapter.notifyDataSetChanged();
Reference link
I have 2 applications(different package names) which use one Firebase database. One app has to write access to the database and another have read access to the database.in my second application, i use recyclerview to retrieve data which is stored by 1st App.
for this I use below code:
FirebaseOptions options = new FirebaseOptions.Builder()
.setApplicationId("1:567....259c8f58311") // Required for Analytics.
.setApiKey("AIzaSyA9BRxl......hE03y5qD-c") // Required for Auth.
.setDatabaseUrl("https://mycity-3a561.firebaseio.com/") // Required for RTDB.
.build();
FirebaseApp.initializeApp(this /* Context */, options, "MyCity");
// Retrieve my other app.
FirebaseApp app = FirebaseApp.getInstance("MyCity");
// Get the database for the other app.
FirebaseDatabase secondaryDatabase = FirebaseDatabase.getInstance(app);
DatabaseReference data = secondaryDatabase.getInstance().getReference();
data.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot ds : snapshot.getChildren()) {
for (DataSnapshot dSnapshot : ds.getChildren()) {
WaterClass waterClass = dSnapshot.getValue(WaterClass.class);
Log.d("Show", waterClass.getName() == null ? "" : waterClass.getName());
list.add(waterClass);
}
adapter = new WaterAdapter(ShowWaterDetails.this, list);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
progressDialog.dismiss();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
progressDialog.dismiss();
}
});
}
Adapter class
private class WaterAdapter extends RecyclerView.Adapter<WaterAdapter.ViewHolder> {
ShowWaterDetails showDetail;
List<WaterClass> listData;
public WaterAdapter(ShowWaterDetails showWaterDetails, List<WaterClass> list) {
this.showDetail = showWaterDetails;
this.listData = list;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.show_items, parent, false);
WaterAdapter.ViewHolder viewHolder = new WaterAdapter.ViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(WaterAdapter.ViewHolder holder, int position) {
WaterClass AllDetails = listData.get(position);
holder.NameTextView.setText(AllDetails.getName());
holder.DetailTextView.setText(AllDetails.getDetail());
holder.DateTextView.setText(AllDetails.getDate());
holder.LocationTextView.setText(AllDetails.getLocation());
holder.TypeTextView.setText(AllDetails.getType());
Picasso.with(showDetail).load(AllDetails.getImgurl()).resize(120, 60).into(holder.ImageTextView);
}
#Override
public int getItemCount() {
return listData.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
public TextView NameTextView;
public TextView DetailTextView;
public TextView DateTextView;
public TextView LocationTextView;
public TextView TypeTextView;
public ImageView ImageTextView;
public ViewHolder(View itemView) {
super(itemView);
NameTextView = itemView.findViewById(R.id.ShowNameTextView);
DetailTextView = itemView.findViewById(R.id.ShowDetailTextView);
DateTextView = itemView.findViewById(R.id.ShowDateTextView);
LocationTextView = itemView.findViewById(R.id.ShowLocationTextView);
TypeTextView = itemView.findViewById(R.id.ShowTypeTextView);
ImageTextView = itemView.findViewById(R.id.ShowImageView);
}
}
}
}
POJO Class
class WaterClass {
private String id;
private String email;
private String name;
private String type;
private String detail;
private String location;
private String date;
private String imgurl;
public WaterClass(){
}
public WaterClass(String id, String currentUserString, String imageUrl, String nameString, String typeString, String detailString, String locationString, String dateString) {
this.id = id;
this.email = currentUserString;
this.name =nameString;
this.type = typeString;
this.detail = detailString;
this.location = locationString;
this.date = dateString;
this.imgurl = imageUrl;
}
public String getImgurl() {
return imgurl;
}
public void setImgurl(String imgurl) {
this.imgurl = imgurl;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
:
there is no error but my recycler not showing anything
go to onStart() and start listening
#Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
and in your onStop
#Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
The FirebaseRecyclerAdapter uses a snapshot listener to monitor changes to the Firestore 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.
Be sure that the names of constant in the POJO match exatly the names
of your database structure in your firebase console !!
ps: do not post your api-keys or app-ids in your questions, keep them secret, and consider using firebaserecycleradapter if you are using firebase-database , it will be more easy to setup and to show values.
Your POJO is ok !
Found Solution!!
just change this part of a code
FirebaseApp.initializeApp(this /* Context */, options, "MyCity");
// Retrieve my other app.
FirebaseApp app = FirebaseApp.getInstance("MyCity");
TO
FirebaseApp.initializeApp(this);
// Retrieve my other app.
FirebaseApp app = FirebaseApp.getInstance("[DEFAULT]");
Can anyone help me to retrieve data from every child except doctor_profile.
Do i need to create separate class for every child or there is another way to solve this problem.
TIA
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("addiction psychiatrist");
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
Psychiatrist psychiatrist = snapshot.getValue(Psychiatrist.class);
//now you can add it to your list (collection) or do what you want
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
and of course you must have class which represents your objects, so for example:
#IgnoreExtraProperties
public class Psychiatrist {
private String name;
private String city;
private String hospital;
public Psychiatrist() {
// required empty constructor
}
public Psychiatrist(String name, String city, String hospital) {
this.name = name;
this.city = city;
this.hospital = hospital;
}
public String getName() {
return name;
}
public String getCity() {
return city;
}
public String getHospital() {
return hospital;
}
}
I have different packages in my app like this.
CansTypeObject class looks like this
public class CansTypeObject {
String productId;
String productName;
String productPrice;
String productReturnPrice;
String productImage;
}
Now, I want to access these strings from MainActivity
CansTypeObject object = new CansTypeObject();
But I cant. If I move the CansTypeObject class to the same package as that of MainActivity, I can access them. What is the solution for this?
The default scope is package-private. Use the public modifier on the String declaration
public String productId;
The only reason of this behavior is that you declared yours class Strings as package-protected strings. In Java this means, that this fields will be accessible only for classes in the same package.
There are several solutions for your problem.
The first (and the worst) - declare your fields as public strings, like this:
public String myField;
The second - create getters for your fields and declare your strings as private fields, like this:
private String myField;
public String getMyField() {
return myField;
}
Then, use this getters on your class object.
Hope, it helps.
You can define getter-setter method for the CansTypeObject class as follows for accessing them easily:
public class CansTypeObject {
String productId;
String productName;
String productPrice;
String productReturnPrice;
String productImage;
public String getProductId() {
return productId;
}
public void setProductId(String productId) {
this.productId = productId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getProductPrice() {
return productPrice;
}
public void setProductPrice(String productPrice) {
this.productPrice = productPrice;
}
public String getProductReturnPrice() {
return productReturnPrice;
}
public void setProductReturnPrice(String productReturnPrice) {
this.productReturnPrice = productReturnPrice;
}
public String getProductImage() {
return productImage;
}
public void setProductImage(String productImage) {
this.productImage = productImage;
}
}
for creating getter-setter method you can use shortcut key for
MAC - command + N
Windows - Alt + Insert
Another way is to open the class for which you have to make getter setter and then right click and from the context menu select Generate option
How can I get an object's attributes?
I created an object that has two fields, first one called Title containing the string value "title1" and second one called Description containing the string value "description1". I would like to get the strings inside.
The method item.toString() gets me the two strings one after the other. Is there a way to get the strings separatively?
Just create accessor methods for each field.
Assuming you have declared your fields like this:
private String title;
private String desciption;
create the accessor methods in your class definition like this:
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
then you just call these methods to get the appropriate value.
Class Declaration
public class Class_Name {
private String Category;
private String Category_Type;
public Class_Name () {
super();
}
public Class_Name(String Category, String Category_Type) {
super();
this.Category = Category;
this.Category_Type = Category_Type;
}
/*public Class_Name (String Category_Type) {
this.Category_Type = Category_Type;
}*/
public String getCategory() {
return Category;
}
public void setCategory(String Category) {
this.Category = Category;
}
public String getCategory_Type() {
return Category_Type;
}
public void setCategory_Type(String Category_Type) {
this.Category_Type = Category_Type;
}
}
In the class where you should use this Object to store and retrieve values.
//Create an object for this class
private Class_Name data;
// To save values
data.setCategory(sCategory);
data.setCategory_Type(sType);
// To retrieve values
String sCategory = data.getCategory();
String sType = data.getCategory_Type();