gpt4 book ai didi

java - DatabaseException : Can't convert object of type java. lang.String 类型 StreetClass

转载 作者:搜寻专家 更新时间:2023-11-01 07:43:56 25 4
gpt4 key购买 nike

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

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

这是我的模型类:

 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 适配器类:

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

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

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

主要 Activity

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

这是我的数据库结构 image

最佳答案

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

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() 方法创建另一个不必要的节点。您可以简单地将这些属性添加到 uid 下。

关于java - DatabaseException : Can't convert object of type java. lang.String 类型 StreetClass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48823356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com