Android Firebase pass data recyclerview to recyclerview - java

I want to pass data RC_1 to RC_2. How can I do this(cat, info, orderid) RecyclerView to RecyclerView?Examp.pic_1
My firebase node is like as to picture.
My firebase node
//RC_1 Struct
public class Name_Struct {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
//RC_2 Struct
public class Detail_Struct {
private String cat;
private String info;
private String orderid;
public String getCat() {
return cat;
}
public void setCat(String cat) {
this.cat = cat;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public String getOrderid() {
return orderid;
}
public void setOrderid(String orderid) {
this.orderid = orderid;
}
RC_1 for;
//Myactivity_1
private void rc_1_get_name(){
Query query = FirebaseDatabase.getInstance().getReference()
.child(“Pro_”);
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
mName.clear();
for(DataSnapshot snp : snapshot.getChildren()){
Name_Struct names = snp.getValue(Name_Struct.class);
for (DataSnapshot recipeSnapshot: snp.child(“basket”).getChildren()) {
mName.add(names);
}
}
mName_adapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
RC_1 for;
//Myadapter_ 1
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, #SuppressLint("RecyclerView") int position) {
final Name_Struct names = mName.get(position);
holder.name_.setText(names.getName()));
holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(mContext, RC_2.class);
???
mContext.startActivity(intent);
}
}

As I see in your screenshot, the "Pro_" node contains a name that has the value of "Mike". This means that you can map that node into an object of type Name_Struct. And you are doing that successfully. Under the same node, there is however another node called "Basket". That node is represented by a Map object, where the keys are represented by those IDs (ID_1, ID_2, etc) and the value by Detail_Struct objects. To be able to map the data under the "Basket" node, you have to add a new property in your Name_Struct class called basket like this:
public class Name_Struct implements Serializable {
private String name;
private Map<String, Object> basket;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#PropertyName("Basket")
public Map<String, Object> getBasket() {
return basket;
}
public void setBasket(Map<String, Object> basket) {
this.basket = basket;
}
}
Now to pass the data to the second activity, you have to add iterate the Map and create a List<Detail_Struct> that can be added to the Intent so it can be read in the second activity.

Related

How to insert data in the firebase

So i have problem regarding adding the appointment details in my firebase android studio. I want the user to store their appointment details in appointment database and I also want to check if the user has booked the date and day. However , after the user has pressed the confirm button, it doesnt store the appointment details in the firebase and it also doesnt prompt the dialog message to the user indicating that their appointment has been registered successfully? Is there any way to solve this problem ? The code is shown as below :
private void addAppointment() {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("idPatient", FirebaseAuth.getInstance().getCurrentUser().getUid());
hashMap.put("idDoctor", idDoctor);
hashMap.put("time", time.getText().toString());
hashMap.put("date", day.getText().toString());
hashMap.put("status", "On hold");
DatabaseReference reference1 = FirebaseDatabase.getInstance().getReference("Appointment");
reference1.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
for (DataSnapshot data : snapshot.getChildren()) {
Appointment relationship = data.getValue(Appointment.class);
if (relationship.getTime().equals(time.getText().toString()) && relationship.getDate().equals(day.getText().toString()) && relationship.getIdDoctor().equals(idDoctor)) {
new SweetAlertDialog(BookAppointmentActivity.this, SweetAlertDialog.ERROR_TYPE)
.setTitleText("Oops...")
.setContentText("The date and time has been booked")
.show();
}else if(snapshot.exists()){
reference.child("Appointment").push().setValue(hashMap);
new SweetAlertDialog(BookAppointmentActivity.this, SweetAlertDialog.SUCCESS_TYPE)
.setTitleText("Congratulations")
.setContentText("Your appointment is registered successfully")
.setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
#Override
public void onClick(SweetAlertDialog sweetAlertDialog) {
Intent intent = new Intent(BookAppointmentActivity.this, MainActivity.class);
startActivity(intent);
}
})
.show();
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
Also check the firebase rules, read and write are set to true as per your requirements
so first of all you can create a modal class for all the children (example - doctor, time, status )
package com.example.chatapp;
public class UserModel {
String name;
String phone;
String about;
String url;
String uid;
public UserModel(){
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAbout() {
return about;
}
public void setAbout(String about) {
this.about = about;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
}
then declare this modal class
Usermodal modal = new Modal class
then insert data like
modal.setname(yourdata);
then write
databaseref.child(key).setvalue(modal);
you can refer to this link for more.

Android Firebase pass data iterate map

I want to pass data from first activity to second activity. But when I want to read the data in the second activity response is null. How can I solve this problem?
Firebase Node
Pro_:
Basket:
ID_1:
cat: “Tech”
info:”iOS”
orderid:”Ref_1”
ID_2:
cat: “Tech”
info:”Android”
orderid:”Ref_2”
name:”Mike”
My struct
public class Name_Struct implements Serializable {
private String name;
private Map<String, Object> basket;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#PropertyName("Basket")
public Map<String, Object> getBasket() {
return basket;
}
public void setBasket(Map<String, Object> basket) {
this.basket = basket;
}
}
First Activity
private void retrieve_data(){
Query query = FirebaseDatabase.getInstance().getReference()
.child(“Pro_”);
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
mName.clear();
for(DataSnapshot snp : snapshot.getChildren()){
Name_Struct names = snp.getValue(Name_Struct.class);
mName.add(names);
}
mName_adapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
EDIT_1 First Adapter
final Name_Struct newModel = Name_Struct(position);
Bundle bundle1=new Bundle();
bundle1.putSerializable("basket", (Serializable) newModel.getBasket());
intent.putExtras(bundle1);
EDIT_1 Second Activity
Map <String, Object> map1 = (Map)bundle.getSerializable("basket");
Detail_Struct detail = new Detail_Struct(
map1.get("cat"),map1.get("info"),map1.get("orderid"));
mDetail_Struct.add(detail);
for (Map.Entry<String, Object> entry : map1.entrySet()) {
Log.d("Map_array:"+ map1);
Map_array:{ID_1={cat=Tech, info=iOS, orderid=Ref_1}, D_2={cat=Tech,
info=Android, orderid=Ref_2}}
Log.d("Map_value:"+ entry.getValue());
Map_value:{cat=Tech, info=iOS, orderid=Ref_1} Map_value:{cat=Tech,
info=Android, orderid=Ref_2}
Log.d("Map_object:"+ map1.get("orderid"));
null line???
Map_object:null Map_object:null
}
EDIT_1 Detail Struct
public class Detail_Struct {
private String cat;
private String info;
private String orderid;
public String getCat() {
return cat;
}
public void setCat(String cat) {
this.cat = cat;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public String getOrderid() {
return orderid;
}
public void setOrderid(String orderid) {
this.orderid = orderid;
}

How to set Retrofit response body data in Listview

In Daata function try to fetch data from server. Successfully fetched, but data cant be set in ArrayList
List<FlowerListModel>flowerListModels=new ArrayList<>();
Cause i want to set flowerListModels data in FlowerAdapter and show in listview
public void Daata() {
Call<List<FlowerListData>>listCall=apiInterface.getflowers();
listCall.enqueue(new Callback<List<FlowerListData>>() {
#Override
public void onResponse(Call<List<FlowerListData>> call, Response<List<FlowerListData>> response) {
Log.d("DataCheck",new Gson().toJson(response.body()));
List<FlowerListModel>flowerListModels=new ArrayList<>();
FlowerAdapter flowerAdapter = new FlowerAdapter(getApplicationContext(),flowerListModels);
listView.setAdapter(flowerAdapter);
}
#Override
public void onFailure(Call<List<FlowerListData>> call, Throwable t) {
Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_SHORT).show();
}
});
}
Here is FlowerListModel class
package bdservers.com.schoolmanagement.Model;
public class FlowerListModel {
private String category;
private String instructions;
private String photo;
private String name;
private String price;
public FlowerListModel(){}
public FlowerListModel(String category, String instructions, String photo, String name,String price){
this.category=category;
this.instructions=instructions;
this.photo=photo;
this.name=name;
this.price=price;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getInstructions() {
return instructions;
}
public void setInstructions(String instructions) {
this.instructions = instructions;
}
public String getPhoto() {
return photo;
}
public void setPhoto(String photo) {
this.photo = photo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
You are setting empty ArrayList to your adapter, I have highlighted the line where you have made the error, and also the correct line that you need
public void Daata() {
Call<List<FlowerListData>>listCall=apiInterface.getflowers();
listCall.enqueue(new Callback<List<FlowerListData>>() {
#Override
public void onResponse(Call<List<FlowerListData>> call, Response<List<FlowerListData>> response) {
Log.d("DataCheck",new Gson().toJson(response.body()));
/**
* You are setting this empty list to adapter
*List<FlowerListModel>flowerListModels=new ArrayList<>();
*/
List<FlowerListModel> flowerListModels = new ArrayList<>();
flowerListModels = response.body();
FlowerAdapter flowerAdapter = new FlowerAdapter(getApplicationContext(),flowerListModels);
listView.setAdapter(flowerAdapter);
}
#Override
public void onFailure(Call<List<FlowerListData>> call, Throwable t) {
Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_SHORT).show();
}
});
}
You are creating new empty List here: List<FlowerListModel>flowerListModels=new ArrayList<>();
You can try something like this:
#Override
public void onResponse(Call<List<FlowerListData>> call, Response<List<FlowerListData>> response) {
Log.d("DataCheck",new Gson().toJson(response.body()));
FlowerAdapter flowerAdapter = new FlowerAdapter(getApplicationContext(),response.body());
listView.setAdapter(flowerAdapter);
}
Create BaseResponse model like this
public class BaseResponse {
#SerializedName("data")
private List<Object> alObjects;
public BaseResponse(List<Object> alObjects) {
this.alObjects = alObjects;
}
public List<Object> getAlObjects() {
return alObjects;
}
public void setAlObjects(List<Object> alObjects) {
this.alObjects = alObjects;
}
}
Then get data from server
#POST(Constants.URL_API_DATA)
BaseResponse executeBaseResponse(#Body String mData);
Cheers!!

Correct way to update a Realm database?

This is the structure of my Realm database:
public class ARDatabase extends RealmObject
{
#PrimaryKey
private String uid;
private String namex;
private String desc;
private boolean isVideo;
private boolean isDeleted;
private String urlImg;
private String urlApp;
private int updates;
private boolean isDownloaded;
private String location;
public ARDatabase(){}
public String getUid()
{
return uid;
}
public void setUid(String uid)
{
this.uid = uid;
}
public String getNamex()
{
return namex;
}
public void setNamex(String namex)
{
this.namex = namex;
}
public String getDesc()
{
return desc;
}
public void setDesc(String desc)
{
this.desc = desc;
}
public boolean getIsVideo()
{
return isVideo;
}
public void setIsVideo(boolean isVideo)
{
this.isVideo = isVideo;
}
public boolean getIsDeleted()
{
return isDeleted;
}
public void setIsDeleted(boolean isDeleted)
{
this.isDeleted = isDeleted;
}
public String getUrlImg()
{
return urlImg;
}
public void setUrlImg(String urlImg)
{
this.urlImg = urlImg;
}
public String getUrlApp()
{
return urlApp;
}
public void setUrlApp(String urlApp)
{
this.urlApp = urlApp;
}
public int getUpdates()
{
return updates;
}
public void setUpdates(int updates)
{
this.updates = updates;
}
public boolean getIsDownloaded()
{
return isDownloaded;
}
public void setIsDownloaded(boolean isDownloaded)
{
this.isDownloaded = isDownloaded;
}
public String getLocation()
{
return location;
}
public void setLocation(String location)
{
this.location = location;
}
}
And I can successfully add objects to the database.
The problem comes when I need to update an object.
This is what I tried:
private void downloadUpdateDatabase(String uid,String location_address) throws RealmException
{
mRealm.beginTransaction();
ARDatabase db = new ARDatabase();
db.setUid(uid);
db.setIsDownloaded(true);
db.setLocation(location_address);
mRealm.copyToRealmOrUpdate(db);
mRealm.commitTransaction();
Log.e("TAG","DOWNLOAD UPDATE COMPLETED");
}
The problem here is when I invoke this method. The mentioned fields get updated, but the not mentioned fields in this method become null or zero.
Of course I can set values for all fields by invoking their setters, however from where I invoke this method, I can't get all the field values.
So, the Question is: How do I update my realm database in such a way that the existing fields don't become null ?
P.S.:
My Realm version is :0.84.1, compile 'io.realm:realm-android:0.84.1'
the field that are mentioned gets updated, however the fields that are not mentioned in this method becomes null or zero
Well, yes, all fields are their defaults at this point.
ARDatabase db = new ARDatabase();
Have you tried to query for the current record, then update the fields, then put that object back?
In other words, you have String uid, so something like
private void downloadUpdateDatabase(String uid,String location_address) throws RealmException
{
mRealm.beginTransaction();
ARDatabase db = mRealm.where(ARDatabase.class).equalTo("uid", uid).findFirst();
db.setIsDownloaded(true);
db.setLocation(location_address);
mRealm.copyToRealmOrUpdate(db);
mRealm.commitTransaction();
}
Or, probably better in async fashion.
private void downloadUpdateDatabase(final String uid, final String location_address) throws RealmException
{
mRealm.executeTransactionAsync(new Realm.Transaction() {
#Override
public void execute(Realm realm) {
ARDatabase db = realm.where(ARDatabase.class).equalTo("uid", uid).findFirst();
db.setIsDownloaded(true);
db.setLocation(location_address);
}
}, new Realm.Transaction.OnSuccess() {
#Override
public void onSuccess() {
// Transaction was a success.
}
}, new Realm.Transaction.OnError() {
#Override
public void onError(Throwable error) {
// Transaction failed and was automatically canceled.
}
});
}
Instead of
mRealm.beginTransaction();
ARDatabase db = new ARDatabase();
db.setUid(uid);
db.setIsDownloaded(true);
db.setLocation(location_address);
mRealm.copyToRealmOrUpdate(db);
mRealm.commitTransaction();
Log.e("TAG","DOWNLOAD UPDATE COMPLETED");
There should be
mRealm.executeTransaction(new Realm.Transaction() {
#Override
public void execute(Realm realm) {
ARDatabase db = realm.where(ARDatabase.class).equalTo("uid", uid).findFirst();
if(db == null) {
db = realm.createObject(ARDatabase.class, uid);
}
db.setIsDownloaded(true);
db.setLocation(location_address);
}
});
Log.e("TAG","DOWNLOAD UPDATE COMPLETED");

Retrieve a child of a child in the Firebase Recycler View

Gettin the child "sonyTV" in the Firebase Recycler View.
the problem : this child is not a direct child of "Users"
Got the name and the date ,but not "sonyTV"
#Override
protected void onBindViewHolder(#NonNull UsersViewHolder usersViewHolder, int i, #NonNull ALL_USERS all_users) {
usersViewHolder.setName(all_users.getName());
usersViewHolder.setDate(all_users.getDate());
usersViewHolder.setUserSoldItems(all_users.getUserSoldItems());
setUserSoldItems method
public void setUserSoldItems(ALL_USERS.UserSoldItems userSoldItems) {
TextView SonyTvView = mView.findViewById(R.id.showTVsony);
SonyTvView.setText("Sony TV : "+userSoldItems);
}
ALL_USERS class
public class ALL_USERS {
private String name;
private long date;
private UserSoldItems userSoldItems;
public ALL_USERS() {}
public ALL_USERS(String name, long date, UserSoldItems userSoldItems) {
this.name = name;
this.date = date;
this.userSoldItems = userSoldItems;
}
public String getName() { return name; }
public long getDate() { return date; }
public UserSoldItems getUserSoldItems() { return userSoldItems; }
public class UserSoldItems {
private long sonyTV;
public UserSoldItems() {}
public UserSoldItems(long sonyTV) {
this.sonyTV = sonyTV;
}
public long getSonyTV() { return sonyTV; }
}
}
but it gives me null values, although you can check it in my database its not null
This is how i post to child("sonyTV")
users.child(user.getUid()).child("UserSoldItems").child("sonyTV").runTransaction(new Transaction.Handler() {
#NonNull
#Override
public Transaction.Result doTransaction(#NonNull MutableData mutableData) {
Long value = mutableData.getValue(Long.class);
if (value == null) {
mutableData.setValue(0);
}
else {
mutableData.setValue(value + 1);
}
return Transaction.success(mutableData);
}
#Override
public void onComplete(DatabaseError databaseError, boolean b,
DataSnapshot dataSnapshot) {
}
});
According to your question:
Dear Alex i have one more thing i cant get the child sonyTV in the RecyclerView ,is it beacause its not a direct child of Users node ??!!!
Now seeing what you are trying to achieve, the simplest solution I can think of is to add an extra property of type UserSoldItems in your User class. So your User class should look like this:
public class ALL_USERS {
private String name;
private long date;
private UserSoldItems userSoldItems;
public ALL_USERS() {}
public ALL_USERS(String name, long date, UserSoldItems userSoldItems) {
this.name = name;
this.date = date;
this.userSoldItems = userSoldItems;
}
public String getName() { return name; }
public long getDate() { return date; }
public UserSoldItems getUserSoldItems { return userSoldItems; }
}
And the UserSoldItems should look like this:
public UserSoldItems {
private long sonyTV;
public UserSoldItems() {}
public UserSoldItems(int sonyTV) {
this.sonyTV = sonyTV;
}
public long getSonyTV() { return sonyTV; }
}
Or even simpler:
public UserSoldItems {
public long sonyTV;
}
And please note, the name of the node should be UserSoldItems and not User Sold Items in order to make it work. So it should not contains any spaces. So when adding data, please add it without any space. In the end, just clear the actual data from the database and add fresh one.
So we introduced a new UserSoldItems level in your JSON tree, so you we can ensure that your Java classes reflect that.

Categories

Resources