DatabaseException:无法将java.lang.String类型的对象转换为StreetClass类型

时间:2018-02-16 09:10:15

标签: java android firebase firebase-realtime-database android-recyclerview

我想在Firebase数据库的await中显示登录用户详细信息,但我遇到以下错误:

RecyclerView

这是我的模特课:

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

}

recyclerview适配器类:

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

这里我存储了我的所有数据:

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

Mainactivity

      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();

这是我的数据库结构 image

2 个答案:

答案 0 :(得分:2)

要获取单个用户的名称,请使用以下代码:

getCroppedCanvas

我在你的图片中看到你正在使用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); 方法创建另一个不必要的节点。您只需在push()

下添加这些属性即可

答案 1 :(得分:1)

问题是你的onDataChange方法中有一个额外的forEach循环。只需删除它,它应该没问题:

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