I have an ArrayAdapter which lists user's data, but it lists the last user's data, it show the correct number of row, if there are 5 users it shows 5 rows but all of them are the same, the last user.
The main activity is this:
for(DataSnapshot Ds : dS.getChildren()) {
Object key = Ds.getKey();
String StringKey = String.valueOf(key);
getname(StringKey);
}
private void getname(String ID){
final String IDfinal = ID;
DatabaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Name = dataSnapshot.child("Users").child(IDfinal).child("name").getValue(String.class);
Email = dataSnapshot.child("Users").child(IDfinal).child("email").getValue(String.class);
Gender = dataSnapshot.child("Users").child(IDfinal).child("gender").getValue(String.class);
Birthday = dataSnapshot.child("Users").child(IDfinal).child("data_birth").getValue(String.class);
UserInformation ID = new UserInformation(Name,Email,Gender,Birthday);
userInformationsList.add(ID);
list();
Toast.makeText(SearchActivity.this,Name,Toast.LENGTH_SHORT).show();
}
#Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(SearchActivity.this,"Error 404",Toast.LENGTH_SHORT).show();
}
});
}
private void list(){
customAdapter customAdapterIntent= new customAdapter(this,R.layout.userslist,userInformationsList);
mListView.setAdapter(customAdapterIntent);
}
}
UserInformation.java
public class UserInformation {
private static String Name;
private static String Gender;
private static String Email;
private static String Birthday;
public UserInformation(String Name,String Gender,String Email,String Birthday){
this.Birthday=Birthday;
this.Email=Email;
this.Gender=Gender;
this.Name=Name;
}
public static String getName(){return Name;}
public static String getEmail(){return Email;}
public static String getGender(){return Gender;}
public static String getBirthday(){return Birthday;}
}
customAdapter.java
public class customAdapter extends ArrayAdapter<UserInformation> {
public customAdapter(Context context, int layoutResource, List<UserInformation> userInformationsList) {
super(context, layoutResource, userInformationsList);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater layoutInflater = LayoutInflater.from(getContext());
view = layoutInflater.inflate(R.layout.userslist, null);
}
UserInformation userInformation = getItem(position);
if (userInformation != null) {
TextView Name = (TextView) view.findViewById(R.id.textNameList);
//name.setText(name[position]);
TextView email = (TextView) view.findViewById(R.id.textEmailList);
//email.setText(Email[position]);
TextView gender = (TextView) view.findViewById(R.id.textGenderList);
//gender.setText(Gender[position]);
TextView birthday = (TextView) view.findViewById(R.id.textBirthdayList);
//birthday.setText(Birthday[position]);
if (Name != null) {
Name.setText(UserInformation.getName());
}
if (email != null) {
email.setText(UserInformation.getEmail());
}
if (gender != null) {
gender.setText(UserInformation.getGender());
}
if (birthday != null) {
birthday.setText(UserInformation.getBirthday());
}
}
return view;
}
}
For setting values why are you using Class (UserInformation) instead of Class name use its object userInformation.
if (Name != null) {
Name.setText(userInformation.getName());
}
if (email != null) {
email.setText(userInformation.getEmail());
}
if (gender != null) {
gender.setText(userInformation.getGender());
}
if (birthday != null) {
birthday.setText(userInformation.getBirthday());
}
One more thing , make below values non-static
public class UserInformation {
private String Name;
private String Gender;
private String Email;
private String Birthday;
public UserInformation(String Name,String Gender,String Email,String Birthday){
this.Birthday=Birthday;
this.Email=Email;
this.Gender=Gender;
this.Name=Name;
}
public String getName(){return Name;}
public String getEmail(){return Email;}
public String getGender(){return Gender;}
public String getBirthday(){return Birthday;}
}
Related
I took this information out of a JSON and initialized it, but I don't understand how I can get the data out of the form to pass them to the cardView afterward. Or is there an easier way?
MainActivity
public class MainActivity extends AppCompatActivity {
Context context;
Example example;
Response responses;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView rv = (RecyclerView)findViewById(R.id.cartView);
LinearLayoutManager llm = new LinearLayoutManager(context);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("Secret Url") // For some reason I cannot show the url
.addConverterFactory(GsonConverterFactory.create())
.build();
Api api = retrofit.create(Api.class);
ArrayList arrayList = new ArrayList();
Call<Example> call = api.getRespon();
call.enqueue(new Callback<Example>() {
#Override
public void onResponse(Call<Example> call, Response<Example> response) {
if(response.code() != 200){
Toast.makeText(MainActivity.this, "Connect",Toast.LENGTH_SHORT).show();
return;
}
Example examples = response.body();
for(int i=0; i<examples.getResponse().size(); i++){
String f_name = response.body().getResponse().iterator().next().getfName();
String l_name = response.body().getResponse().iterator().next().getlName();
String birthday = response.body().getResponse().iterator().next().getBirthday();
String SpecName = response.body().getResponse().iterator().next().getSpecialty()
.iterator().next().getName();
int SpecId = response.body().getResponse().iterator().next().getSpecialty()
.iterator().next().getSpecialtyId();
}
}
#Override
public void onFailure(Call<Example> call, Throwable t) {
Toast.makeText(MainActivity.this, "Fail",Toast.LENGTH_SHORT).show();
}
});
}
}
At the moment I have not finished the class because I can not specify the data that have been implemented
PersonaAdapter
public class PersonAdapter extends RecyclerView.Adapter<PersonAdapter.PersonViewHolder> {
Context context;
Example example;
MainActivity mainActivity;
public static class PersonViewHolder extends RecyclerView.ViewHolder{
CardView cv;
TextView l_name, f_name, birthday, SpecName, Age;
ImageView avatar;
public PersonViewHolder(View itemView) {
super(itemView);
cv = itemView.findViewById(R.id.cartView);
l_name = itemView.findViewById(R.id.textLName);
f_name = itemView.findViewById(R.id.textFName);
birthday = itemView.findViewById(R.id.textBirthday);
SpecName = itemView.findViewById(R.id.textSpecName);
Age = itemView.findViewById(R.id.textAge);
}
}
// иницилизация
#Override
public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_layout,viewGroup,false);
PersonViewHolder phv = new PersonViewHolder(v);
return phv;
}
#Override
public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
//***
}
#Override
public int getItemCount() {
return example.getResponse().size();
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
}
Handler itself
Response
public class Response {
#SerializedName("f_name")
#Expose
private String fName;
#SerializedName("l_name")
#Expose
private String lName;
#SerializedName("birthday")
#Expose
private String birthday;
#SerializedName("avatr_url")
#Expose
private String avatrUrl;
#SerializedName("specialty")
#Expose
private List<Specialty> specialty = null;
public String getfName() {
return fName;
}
public void setfName(String fName) {
this.fName = fName;
}
public Response withfName(String fName) {
this.fName = fName;
return this;
}
public String getlName() {
return lName;
}
public void setlName(String lName) {
this.lName = lName;
}
public Response withlName(String lName) {
this.lName = lName;
return this;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public Response withBirthday(String birthday) {
this.birthday = birthday;
return this;
}
public String getAvatrUrl() {
return avatrUrl;
}
public void setAvatrUrl(String avatrUrl) {
this.avatrUrl = avatrUrl;
}
public Response withAvatrUrl(String avatrUrl) {
this.avatrUrl = avatrUrl;
return this;
}
public List<Specialty> getSpecialty() {
return specialty;
}
public void setSpecialty(List<Specialty> specialty) {
this.specialty = specialty;
}
public Response withSpecialty(List<Specialty> specialty) {
this.specialty = specialty;
return this;
}
}
Specialty
public class Specialty {
#SerializedName("specialty_id")
#Expose
private Integer specialtyId;
#SerializedName("name")
#Expose
private String name;
public Integer getSpecialtyId() {
return specialtyId;
}
public void setSpecialtyId(Integer specialtyId) {
this.specialtyId = specialtyId;
}
public Specialty withSpecialtyId(Integer specialtyId) {
this.specialtyId = specialtyId;
return this;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Specialty withName(String name) {
this.name = name;
return this;
}
}
I solved the problem by creating a list and putting the data into an array
List<Worker> workerList = new ArrayList<>();
for(int i = 0; i < examples.getResponse().size(); i++){
String f_name = response.body().getResponse().get(i).getfName();
String l_name = response.body().getResponse().get(i).getlName();
String birthday = response.body().getResponse().get(i).getBirthday();
String SpecName = response.body().getResponse().get(i).getSpecialty()
.iterator().next().getName();
String SpecId = response.body().getResponse().iterator().next().getSpecialty()
.iterator().next().getSpecialtyId().toString();
String AvatarUrl = response.body().getResponse().get(i).getAvatrUrl();
workerList.add(new Worker(f_name, l_name, birthday, SpecName, SpecId, AvatarUrl));
In Worker has a constructor and getter
Thanks to those who will try to help me!
First time I sent a message. I'm stuck with my parcel.
I try to send an object Neighbour in a recyclerView for a class DetailedNeighbour. After I send my variable to DetailedActivity.putExtra("DNeighbour", neighbour); is well equal to what I want and after in DetailedNeighbourActivity is equal to null.
Neighbour:
package com.openclassrooms.entrevoisins.model;
/**
* Model object representing a Neighbour
*/
public class Neighbour implements Parcelable {
/** Identifier */
private long id;
/** Full name */
private String name;
/** Avatar */
private String avatarUrl;
/** Adress */
private String address;
/** Phone number */
private String phoneNumber;
/** About me */
private String aboutMe;
/**
* Constructor
* #param id
* #param name
* #param avatarUrl
*/
public Neighbour(long id, String name, String avatarUrl, String address,
String phoneNumber, String aboutMe) {
this.id = id;
this.name = name;
this.avatarUrl = avatarUrl;
this.address = address;
this.phoneNumber = phoneNumber;
this.aboutMe = aboutMe;
}
public Neighbour (Parcel in){ //constructor //Protected ?
id =in.readLong(); //read and set saved values from parcel
name=in.readString();
avatarUrl=in.readString();
address=in.readString();
phoneNumber=in.readString();
aboutMe=in.readString();
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAvatarUrl() {
return avatarUrl;
}
public void setAvatarUrl(String avatarUrl) {
this.avatarUrl = avatarUrl;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getAboutMe() {
return aboutMe;
}
public void setAboutMe(String aboutMe) {
this.aboutMe = aboutMe;
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Neighbour neighbour = (Neighbour) o;
return Objects.equals(id, neighbour.id);
}
#Override
public int hashCode() {
return Objects.hash(id);
}
#Override
public void writeToParcel(Parcel parcel, int flags) { // In this method you add all your class properties to the parcel which are needed to transfer.
parcel.writeString(name);
parcel.writeString(phoneNumber);
parcel.writeString(avatarUrl);
parcel.writeString(aboutMe);
parcel.writeString(address);
parcel.writeLong(id); /// for favoris ?
}
public static final Parcelable.Creator<Neighbour> CREATOR = new Parcelable.Creator<Neighbour>() { ///This is the method which is used to bind everything together. Nothing much is done here.
#Override
public Neighbour createFromParcel(Parcel in) {
return new Neighbour(in);
}
#Override
public Neighbour[] newArray(int size) {
return new Neighbour[size];
}
};
public static Creator<Neighbour> getCREATOR() {
return CREATOR;
}
#Override
public int describeContents() {
return 0;
}
}
RecyclerViewAdapter:
package com.openclassrooms.entrevoisins.ui.neighbour_list;
public class MyNeighbourRecyclerViewAdapter extends RecyclerView.Adapter<MyNeighbourRecyclerViewAdapter.ViewHolder> {
private final List<Neighbour> mNeighbours;
public MyNeighbourRecyclerViewAdapter(List<Neighbour> items) {
mNeighbours = items;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.fragment_neighbour, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
Neighbour neighbour = mNeighbours.get(position);
holder.mNeighbourName.setText(neighbour.getName());
Glide.with(holder.mNeighbourAvatar.getContext())
.load(neighbour.getAvatarUrl())
.apply(RequestOptions.circleCropTransform())
.into(holder.mNeighbourAvatar);
holder.mNeighbourName.setOnClickListener(new View.OnClickListener() { /// Observe le clic sur bouton name
#Override
public void onClick(View view) {
Intent DetailedActivity = new Intent(view.getContext(), DetailedNeighbourActivity.class); //
DetailedActivity.putExtra("DNeighbour", neighbour);
view.getContext().startActivity(DetailedActivity);//
EventBus.getDefault();//
Log.i("DEBUG","l'utilisateur essaye d'ouvrir le détaille");
}
});
holder.mDeleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EventBus.getDefault().post(new DeleteNeighbourEvent(neighbour));
}
});
}
#Override
public int getItemCount() {
return mNeighbours.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
#BindView(R.id.item_list_avatar)
public ImageView mNeighbourAvatar;
#BindView(R.id.item_list_name)
public TextView mNeighbourName;
#BindView(R.id.item_list_delete_button)
public ImageButton mDeleteButton;
public ViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
}
}
}
DetailedNeighboursActivity:
public class DetailedNeighbourActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detailed_neighbour);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Intent intent = getIntent();
Neighbour neighbour = (Neighbour) getIntent().getParcelableExtra("DNeighbour");
String name = neighbour.getName();
EventBus.getDefault();
Log.i("DEBUG", "Le détaille "+ name);
}
}
I'm not really smart ! Sorry for you time .
parcel and writeparcel constructor need the same order for variables. it's ok my code work.
I created an app and I store some data in a Firebase Firestore, everytime I want to fetch the data and display them in a RecyclerView I get this
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.****, PID: 9052
java.lang.ClassCastException: java.util.HashMap cannot be cast to com.example.****.Models.Comments
at com.example.****.Utils.CommentsAdapter.onBindViewHolder(CommentsAdapter.java:43)
at com.example.****.Utils.CommentsAdapter.onBindViewHolder(CommentsAdapter.java:19)
....
I really don't know what I did wrong, can you please look over my code and tell me where I'm wrong?
Here's the Adapter:
public class CommentsAdapter extends RecyclerView.Adapter<CommentsAdapter.CommentsViewHolder> {
Context mContext;
List<Comments> mData;
public CommentsAdapter(Context mContext, List<Comments> mData) {
this.mContext = mContext;
this.mData = mData;
}
#NonNull
#Override
public CommentsViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View layout;
layout = LayoutInflater.from(mContext).inflate(R.layout.item_comments, parent, false);
return new CommentsViewHolder(layout);
}
#Override
public void onBindViewHolder(#NonNull CommentsViewHolder holder, int i) {
//bind data
holder.title.setText(mData.get(i).getTitle());
holder.content.setText(mData.get(i).getContent());
Glide.with(mContext).load(mData.get(i).getPicUploadedBy()).into(holder.pictureProfile);
}
#Override
public int getItemCount() {
return mData.size();
}
public class CommentsViewHolder extends RecyclerView.ViewHolder {
TextView content, title;
ImageView pictureProfile;
public CommentsViewHolder(#NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.textTitle);
content = itemView.findViewById(R.id.textContent);
pictureProfile = itemView.findViewById(R.id.profileUser);
}
}
}
Here's the activity where I use this:
public class ProfileActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
mAuth = FirebaseAuth.getInstance();
db = FirebaseFirestore.getInstance();
mStorageRef = FirebaseStorage.getInstance().getReference("users-images");
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if(user != null)
{
db.collection("Users").whereEqualTo("email", user.getEmail())
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
if(task.isSuccessful())
{
for(QueryDocumentSnapshot documentSnapshot: task.getResult()) {
String fullname = documentSnapshot.get("fullname").toString();
String address = documentSnapshot.get("address").toString();
String city = documentSnapshot.get("city").toString();
String state = documentSnapshot.get("state").toString();
String country = documentSnapshot.get("country").toString();
String phone = documentSnapshot.get("phone").toString();
String zipcode = documentSnapshot.get("zipcode").toString();
String type = documentSnapshot.get("type").toString();
String rate = documentSnapshot.get("rate").toString();
Boolean isActivated = documentSnapshot.getBoolean("isActivated");
List<Card> cards= (List<Card>) documentSnapshot.get("cards");
List<Comments> comments = (List<Comments>) documentSnapshot.get("comments");
List<Documents> documents = (List<Documents>) documentSnapshot.get("documents");
String profilePic = documentSnapshot.get("profilePic").toString();
String email = documentSnapshot.get("email").toString();
String password = documentSnapshot.get("password").toString();
mUser = new User(
fullname,
address,
city,
state,
country,
phone,
zipcode,
type,
rate,
isActivated,
cards,
comments,
documents,
profilePic,
email,
password
);
myProfileDo(mUser);
}
}
}
});
private void myProfileDo(User mUser) {
List<Comments> commentsList = new ArrayList<>();
fullnameView.setText(mUser.getFullname());
addressView.setText(mUser.getAddress());
rateView.setText(mUser.getRate());
Glide.with(this).load(mUser.getProfilePic()).into(profilePic);
for(int i = 0 ;i < mUser.getComments().size();i++) {
commentsList.add(mUser.getComments().get(i));
}
commentsAdapter = new CommentsAdapter(this,commentsList);
rvComments.setAdapter(commentsAdapter);
rvComments.setLayoutManager(new LinearLayoutManager(this));
}
}
And The Model "Comments" :
public class Comments {
public String uploadedBy;
public String picUploadedBy;
public String title;
public String content;
public Comments(String uploadedBy, String picUploadedBy, String title, String content) {
this.uploadedBy = uploadedBy;
this.picUploadedBy = picUploadedBy;
this.title = title;
this.content = content;
}
public String getUploadedBy() {
return uploadedBy;
}
public void setUploadedBy(String uploadedBy) {
this.uploadedBy = uploadedBy;
}
public String getPicUploadedBy() {
return picUploadedBy;
}
public void setPicUploadedBy(String picUploadedBy) {
this.picUploadedBy = picUploadedBy;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
The data is fetched well, if I println the "commentsList" it's not null, must be something on the adapter or the way I implement the adapter.
Thank you in advance!
I'm having a problem retrieving the payments data. It always says I can't convert object type to:
com.google.firebase.database.DatabaseException: Can't convert object
of type java.lang.String to type
com.pasarkaget.fajar.pasarkaget.Model.Payments
I'm think this error from my model payments but I don't know my mistake. Anyone can help me?
This My AdminUserPaymentsActivity where I must display the payments data certain user.
public class AdminUserPaymentActivity extends AppCompatActivity
{
private RecyclerView paymentsList;
RecyclerView.LayoutManager layoutManager;
private DatabaseReference paymentsRef;
private String userID = "";
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin_user_payments);
userID = getIntent().getStringExtra("uid");
paymentsList = findViewById(R.id.payments_list);
paymentsList.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
paymentsList.setLayoutManager(layoutManager);
paymentsRef = FirebaseDatabase.getInstance().getReference()
.child("Cart List").child("Admin View").child(userID).child("Payments");
}
#Override
protected void onStart()
{
super.onStart();
FirebaseRecyclerOptions<Payments> options =
new FirebaseRecyclerOptions.Builder<Payments>()
.setQuery(paymentsRef, Payments.class)
.build();
FirebaseRecyclerAdapter<Payments, PaymentsViewHolder> adapter = new FirebaseRecyclerAdapter<Payments, PaymentsViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull PaymentsViewHolder holder, int position, #NonNull Payments model)
{
holder.txtMyBankName.setText(model.getBuyerBank());
holder.txtBankAccountName.setText(model.getBuyerAccount());
holder.txtBankName.setText(model.getBank());
holder.txtMetods.setText(model.getMetods());
holder.txtTotalTransfer.setText("Rp " + model.getNominal());
holder.txtState.setText(model.getState());
holder.txtDate.setText("Pada Tanggal : " + model.getDate());
}
#NonNull
#Override
public PaymentsViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType)
{
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.payments_items_layout, parent, false);
PaymentsViewHolder holder = new PaymentsViewHolder(view);
return holder;
}
};
paymentsList.setAdapter(adapter);
adapter.startListening();
}
}
My Viewholder Class
public class PaymentsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
public TextView txtBankName, txtMyBankName, txtBankAccountName, txtTotalTransfer, txtMetods, txtState, txtDate;
private ItemClickListener itemClickListener;
public PaymentsViewHolder(View itemView)
{
super(itemView);
txtBankName = itemView.findViewById(R.id.payments_bank_destination);
txtMyBankName = itemView.findViewById(R.id.payments_bank_name);
txtBankAccountName = itemView.findViewById(R.id.payments_bank_account);
txtTotalTransfer = itemView.findViewById(R.id.payments_total_transfer);
txtMetods = itemView.findViewById(R.id.payments_bank_metods);
txtState = itemView.findViewById(R.id.payments_state);
txtDate = itemView.findViewById(R.id.payments_date);
}
#Override
public void onClick(View view)
{
itemClickListener.onClick(view, getAdapterPosition(), false);
}
public void setItemClickListener(ItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}
}
And Lastly, My Model Payments class
public class Payments
{
private String bank, buyerBank, buyerAccount, nominal, metods, state, date, time;
public Payments()
{
}
public Payments(String bank, String buyerBank, String buyerAccount, String nominal, String metods, String state, String date, String time)
{
this.bank = bank;
this.buyerBank = buyerBank;
this.buyerAccount = buyerAccount;
this.nominal = nominal;
this.metods = metods;
this.state= state;
this.date = date;
this.time = time;
}
public String getBank() {
return bank;
}
public void setBank(String bank) {
this.bank = bank;
}
public String getBuyerBank() {
return buyerBank;
}
public void setBuyerBank(String buyerBank) {
this.buyerBank = buyerBank;
}
public String getBuyerAccount() {
return buyerAccount;
}
public void setBuyerAccount(String buyerAccount) {
this.buyerAccount = buyerAccount;
}
public String getNominal() {
return nominal;
}
public void setNominal(String nominal) {
this.nominal = nominal;
}
public String getMetods() {
return metods;
}
public void setMetods(String metods) {
this.metods = metods;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}
My Firebase database structured:
my structured firebase
When you are passing your paymentsRef to the FirebaseRecyclerOptions<Payments> object it means that you are looking for Payments objects but within your reference, there are only objects of type String and not Payments, and that's why you get that error. To solve this, simply remove the call to .child("Payments"):
paymentsRef = FirebaseDatabase.getInstance().getReference()
.child("Cart List").child("Admin View").child(userID);
And you'll be able to get all Payments objects that exist within your userID node.
im making a program that can view data and image at the same time using recyclerview
MainActivity
mDatabaseRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
ArrayList<Upload> mUpload = new ArrayList<>();
String y = "report";
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()){
childcount = postSnapshot.child(userID).getChildrenCount() - 5;
String z = y+childcount;
Upload upload = postSnapshot.child(userID).child(z).getValue(Upload.class);
Upload uploads = postSnapshot.child(userID).getValue(Upload.class);
mUploads.add(upload);
mUploads.add(uploads);
mUpload.addAll(mUploads);
Log.d(TAG, "sampleData " + mUploads);
}
mAdapter = new ImageAdapter(getActivity(), mUpload);
mRecycleView.setAdapter(mAdapter);
}
ImageAdapter
#Override
public void onBindViewHolder(ImageViewHolder holder, int position) {
Upload uploadCurrent = mUploads.get(position);
holder.textViewName.setText(uploadCurrent.getiName());
holder.textViewDate.setText(uploadCurrent.getDate());
holder.textViewAddress.setText(uploadCurrent.getAddress());
holder.textViewPhone.setText(uploadCurrent.getPhone());
holder.textViewReport.setText(uploadCurrent.getReport());
Picasso.with(mContext)
.load(uploadCurrent.getImageUrl())
.fit()
.centerCrop()
.into(holder.imageView);
holder.setIsRecyclable(false);
holder.getLayoutPosition();
}
Upload Class
public class Upload {
private String mName;
private String mImageUrl;
private String date;
private String address;
private String phone;
private String iName;
private String report;
public Upload() {
//empty constructor needed
}
public Upload(String name, String imageUrl) {
if (name.trim().equals("")) {
name = "No Name";
}
mName = name;
mImageUrl = imageUrl;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getiName() {
return iName;
}
public void setiName(String iName) {
this.iName = iName;
}
public String getReport() {
return report;
}
public void setReport(String report) {
this.report = report;
}
public String getDate() { return date; }
public void setDate(String date) { this.date = date; }
public String getName() {
return mName;
}
public void setName(String name) {
mName = name;
}
public String getImageUrl() {
return mImageUrl;
}
public void setImageUrl(String imageUrl) {
mImageUrl = imageUrl;
}
}
xml file
enter image description here
output i get
enter image description here
as you can see i got wrong positioning of data, i also try to merge the 2 arraylist data which is upload and uploads but nothings change, can someone help me with this project. thank you so much