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

huangapple go评论97阅读模式
英文:

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

问题

我正在尝试从 Firebase 实时数据库中检索数据,并希望将它们显示在 TextView 上,但是遇到了错误。

我的数据库结构如下:

错误在 logcat 中显示为:

UserFragment 类的代码如下:

package com.example.prj;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

public class UserFragment extends Fragment {
    ImageView profile;
    TextView shopName, ownerName, address, phoneNumber , gstNumber, email, policyLink;
    DatabaseReference myRef;

    public UserFragment() {
    }

    public static UserFragment newInstance(String param1, String param2) {
        UserFragment fragment = new UserFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_user, container, false);
        String u = FirebaseAuth.getInstance().getCurrentUser().getUid();
        myRef = FirebaseDatabase.getInstance().getReference(u).child("User");
        profile = v.findViewById(R.id.profile_image);
        shopName = v.findViewById(R.id.shop_name);
        ownerName = v.findViewById(R.id.owner_name);
        address = v.findViewById(R.id.address);
        phoneNumber = v.findViewById(R.id.phone_number);
        gstNumber = v.findViewById(R.id.gst_number);
        email = v.findViewById(R.id.email);
        policyLink = v.findViewById(R.id.policy_link);

        myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for(DataSnapshot detailSnapshot:dataSnapshot.getChildren()) {
                    User user= detailSnapshot.getValue(User.class);
                    shopName.setText(user.getShopname());
                    ownerName.setText(user.getOwner());
                    address.setText(user.getAddress());
                    String p = user.getPhone1() + " , " + user.getPhone2();
                    phoneNumber.setText(p);
                    gstNumber.setText(user.getGst());
                    email.setText(user.getEmail());
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
            }
        });
        return v;
    }
}

UserFragment 布局的 XML 文件如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".UserFragment">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:background="@drawable/profile_pic"
    android:layout_marginRight="20dp"
    android:layout_marginLeft="20dp"
    android:id="@+id/logo"/>

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginRight="20dp"
    android:layout_marginLeft="20dp"
    android:layout_below="@+id/logo"
    android:layout_marginTop="5dp"
    android:textSize="20dp"
    android:id="@+id/shop_name"/>

<!-- 其他 TextView 元素 -->

</RelativeLayout>

User 类的代码如下:

package com.example.prj;

public class User {
    String address, email, gst, owner, phone1, phone2, shopname;

    public User() {
    }

    // 其他构造方法和 getter、setter 方法
}

你试图在 Firebase 中显示这些子节点的值,但由于错误而无法实现。

英文:

I am trying to retrieve data from Firebase Realtime database and want to display them on the screen in the TextView but facing error.
The structure of my database iscom.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.prj.User

The error displayed int the logcat is com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.prj.User

The code of UserFragment class is given below

package com.example.prj;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
/**
* A simple {@link Fragment} subclass.
* Use the {@link UserFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class UserFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = &quot;param1&quot;;
private static final String ARG_PARAM2 = &quot;param2&quot;;
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
ImageView profile;
TextView shopName, ownerName, address, phoneNumber , gstNumber, email, policyLink;
DatabaseReference myRef;
public UserFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment UserFragment.
*/
// TODO: Rename and change types and number of parameters
public static UserFragment newInstance(String param1, String param2) {
UserFragment fragment = new UserFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v =  inflater.inflate(R.layout.fragment_user, container, false);
String u = FirebaseAuth.getInstance().getCurrentUser().getUid();
myRef = FirebaseDatabase.getInstance().getReference(u).child(&quot;User&quot;);
profile = v.findViewById(R.id.profile_image);
shopName = v.findViewById(R.id.owner_name);
ownerName = v.findViewById(R.id.owner_name);
address = v.findViewById(R.id.address);
phoneNumber = v.findViewById(R.id.phone_number);
gstNumber = v.findViewById(R.id.gst_number);
email = v.findViewById(R.id.email);
policyLink = v.findViewById(R.id.policy_link);
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot detailSnapshot:dataSnapshot.getChildren())
{
User user= detailSnapshot.getValue(User.class);
shopName.setText(user.getShopname());
ownerName.setText(user.getOwner());
address.setText(user.getAddress());
String p = user.getPhone1()+&quot; , &quot;+user.getPhone2();
phoneNumber.setText(p);
gstNumber.setText(user.getGst());
email.setText(user.getEmail());
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
return  v;
}
}

The XML file of the layout of UserFragment is given below

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
tools:context=&quot;.UserFragment&quot;&gt;
&lt;ImageView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginTop=&quot;10dp&quot;
android:background=&quot;@drawable/profile_pic&quot;
android:layout_marginRight=&quot;20dp&quot;
android:layout_marginLeft=&quot;20dp&quot;
android:id=&quot;@+id/logo&quot;
/&gt;
&lt;TextView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginRight=&quot;20dp&quot;
android:layout_marginLeft=&quot;20dp&quot;
android:layout_below=&quot;@+id/logo&quot;
android:layout_marginTop=&quot;5dp&quot;
android:textSize=&quot;20dp&quot;
android:id=&quot;@+id/shop_name&quot;
/&gt;
&lt;TextView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginRight=&quot;20dp&quot;
android:layout_marginLeft=&quot;20dp&quot;
android:layout_below=&quot;@+id/shop_name&quot;
android:layout_marginTop=&quot;5dp&quot;
android:textSize=&quot;20dp&quot;
android:id=&quot;@+id/owner_name&quot;
/&gt;
&lt;TextView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginRight=&quot;20dp&quot;
android:layout_marginLeft=&quot;20dp&quot;
android:layout_below=&quot;@+id/owner_name&quot;
android:layout_marginTop=&quot;5dp&quot;
android:textSize=&quot;20dp&quot;
android:id=&quot;@+id/address&quot;
/&gt;
&lt;TextView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginRight=&quot;20dp&quot;
android:layout_marginLeft=&quot;20dp&quot;
android:layout_below=&quot;@+id/address&quot;
android:layout_marginTop=&quot;5dp&quot;
android:textSize=&quot;20dp&quot;
android:id=&quot;@+id/phone_number&quot;
/&gt;
&lt;TextView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginRight=&quot;20dp&quot;
android:layout_marginLeft=&quot;20dp&quot;
android:layout_below=&quot;@+id/phone_number&quot;
android:layout_marginTop=&quot;5dp&quot;
android:textSize=&quot;20dp&quot;
android:id=&quot;@+id/gst_number&quot;
/&gt;
&lt;TextView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginRight=&quot;20dp&quot;
android:layout_marginLeft=&quot;20dp&quot;
android:layout_below=&quot;@+id/gst_number&quot;
android:layout_marginTop=&quot;5dp&quot;
android:textSize=&quot;20dp&quot;
android:id=&quot;@+id/email&quot;
/&gt;
&lt;TextView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginRight=&quot;20dp&quot;
android:layout_marginLeft=&quot;20dp&quot;
android:layout_below=&quot;@+id/email&quot;
android:layout_marginTop=&quot;5dp&quot;
android:textSize=&quot;20dp&quot;
android:id=&quot;@+id/policy_link&quot;
/&gt;
&lt;/RelativeLayout&gt;

The code for User class is given below

package com.example.prj;
public class User {
private static final String TAG = &quot;User&quot;;
String address, email, gst, owner, phone1, phone2, shopname;
public User() {
}
public User(String address, String email, String gst, String owner, String phone1, String phone2, String shopname) {
this.address = address;
this.email = email;
this.gst = gst;
this.owner = owner;
this.phone1 = phone1;
this.phone2 = phone2;
this.shopname = shopname;
}
public static String getTAG() {
return TAG;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getGst() {
return gst;
}
public void setGst(String gst) {
this.gst = gst;
}
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
public String getPhone1() {
return phone1;
}
public void setPhone1(String phone1) {
this.phone1 = phone1;
}
public String getPhone2() {
return phone2;
}
public void setPhone2(String phone2) {
this.phone2 = phone2;
}
public String getShopname() {
return shopname;
}
public void setShopname(String shopname) {
this.shopname = shopname;
}
}

I am trying to display the value of these child node in the Firebase but unable to do so because of the error.

答案1

得分: 0

在路径 /$uid/User 下,您保存了单个用户的数据,因此当您读取该数据时,您会获得包含单个用户数据的快照。但是您的 onDataChange 循环遍历结果,好像有多个用户。您应该删除此循环,这样做:

myRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        User user = dataSnapshot.getValue(User.class);
        ...
英文:

At the path /$uid/User you keep the data for a single user, so when you read that data you get a snapshot with the data for a single user. But your onDataChange loops over the results, as if there are multiple users. You should remove this loop, so:

myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
User user= dataSnapshot.getValue(User.class);
...

huangapple
  • 本文由 发表于 2020年5月3日 18:06:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/61572700.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定