1

I want to display Login user details in RecyclerView from Firebase database but I am facing the following error:

com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type.

This is my model class:

 public class StreetClass {

private String id;
private String semail;
private String sname;
private String stype;
private String sdetail;
private String slocation;
private String sdate;
private String imgurl;

public StreetClass(){}

public StreetClass(String id, String semail, String sname, String stype, String sdetail, String slocation, String sdate, String imgurl) {

    this.id = id;
    this.semail = semail;
    this.sname = sname;
    this.stype = stype;
    this.sdetail = sdetail;
    this.slocation = slocation;
    this.sdate = sdate;
    this.imgurl = imgurl;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getSemail() {
    return semail;
}

public void setSemail(String semail) {
    this.semail = semail;
}

public String getSname() {
    return sname;
}

public void setSname(String sname) {
    this.sname = sname;
}

public String getStype() {
    return stype;
}

public void setStype(String stype) {
    this.stype = stype;
}

public String getSdetail() {
    return sdetail;
}

public void setSdetail(String sdetail) {
    this.sdetail = sdetail;
}

public String getSlocation() {
    return slocation;
}

public void setSlocation(String slocation) {
    this.slocation = slocation;
}

public String getSdate() {
    return sdate;
}

public void setSdate(String sdate) {
    this.sdate = sdate;
}

public String getImgurl() {
    return imgurl;
}

public void setImgurl(String imgurl) {
    this.imgurl = imgurl;
}

}

recyclerview Adapter class:

   public class StreetAdapter extends RecyclerView.Adapter<StreetAdapter.ViewHolder> {

Context context;
private List<StreetClass>listdata ;

public StreetAdapter(Context context, List<StreetClass> list) {

    this.listdata = list;
    this.context = context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.show_items, parent, false);
    ViewHolder viewHolder = new ViewHolder(view);
    return viewHolder;
}

@Override
public void onBindViewHolder(final ViewHolder holder, int position) {


    StreetClass AllDetails = listdata.get(position);
    holder.NameTextView.setText(AllDetails.getSname());
    holder.DetailTextView.setText(AllDetails.getSdetail());
    holder.DateTextView.setText(AllDetails.getSdate());
    holder.LocationTextView.setText(AllDetails.getSlocation());
    holder.TypeTextView.setText(AllDetails.getStype());
    Picasso.with(context).load(AllDetails.getImgurl()).resize(120, 60).into(holder.ImageTextView);

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            int position = holder.getAdapterPosition();
            final Intent intent;
            if (position == 0){
                intent =  new Intent(context, ShowStreetDetails.class);
            } else if (position == 1){
                intent =  new Intent(context, ElectricityActivity.class);
            } else {
                intent =  new Intent(context, Water_Supply_Activity.class);
            }
            context.startActivity(intent);
        }
    });
}


@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);
    }
   }
   }

Here I store my all data:

      StreetClass street = new StreetClass();

                            street.setSname(nameString);
                            street.setStype(typeString);
                            street.setSdetail(detailString);
                            street.setSlocation(locationString);
                            street.setSdate(dateString);
                            street.setImgurl(ImageUrl);

                            String RecordIDFromServer = databaseReference.push().getKey();
                            // Adding the both name and number values using student details class object using ID.
                            databaseReference.child(RecordIDFromServer).setValue(street);
                            // Showing Toast message after successfully data submit.
                            Toast.makeText(StreetActivity.this,"Data Inserted Successfully into Firebase Database", Toast.LENGTH_LONG).show();

Mainactivity

   DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference databaseStatus = rootRef.child("Street Problems");
    String uid = firebaseAuth.getInstance().getCurrentUser().getUid();
    databaseStatus.child(uid).addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot snapshot) {

            for (DataSnapshot ds : snapshot.getChildren()) {

                for (DataSnapshot dSnapshot : ds.getChildren()) {

                    StreetClass streetClass = dSnapshot.getValue(StreetClass.class);
                    Log.d("Show", streetClass.getSname() == null ? "" : streetClass.getSname());
                    list.add(streetClass);

                }

                adapter = new StreetAdapter(ShowStreetDetails.this, list);
                recyclerView.setAdapter(adapter);
                adapter.notifyDataSetChanged();
                progressDialog.dismiss();
            }

and this is my database structure image

2

2 Answers 2

3

To get the name of a single user, please use the following code:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference yourRef = rootRef.child("Street Problems").child(uid);
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            StreetClass streetClass = ds.getValue(StreetClass.class);
            Log.d("TAG", streetClass.getSname());
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
yourRef.addListenerForSingleValueEvent(eventListener);

I see in your picture that you are using the push() method to create another node which is not necessary. You can simply add those properties beneath the uid.

Sign up to request clarification or add additional context in comments.

Comments

0

The problem is that you have an extra forEach loop in your onDataChange method. Simply remove it and it should be fine:

        @Override
        public void onDataChange(DataSnapshot snapshot) {

                for (DataSnapshot dSnapshot : snapshot.getChildren()) {

                    StreetClass streetClass = dSnapshot.getValue(StreetClass.class);
                    Log.d("Show", streetClass.getSname() == null ? "" : streetClass.getSname());
                    list.add(streetClass);

                }

                adapter = new StreetAdapter(ShowStreetDetails.this, list);
                recyclerView.setAdapter(adapter);
                adapter.notifyDataSetChanged();
                progressDialog.dismiss();
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.