英文:
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 is
The error displayed int the logcat is
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 = "param1";
private static final String ARG_PARAM2 = "param2";
// 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("User");
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()+" , "+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
<?xml version="1.0" encoding="utf-8"?>
<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
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:layout_below="@+id/shop_name"
android:layout_marginTop="5dp"
android:textSize="20dp"
android:id="@+id/owner_name"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:layout_below="@+id/owner_name"
android:layout_marginTop="5dp"
android:textSize="20dp"
android:id="@+id/address"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:layout_below="@+id/address"
android:layout_marginTop="5dp"
android:textSize="20dp"
android:id="@+id/phone_number"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:layout_below="@+id/phone_number"
android:layout_marginTop="5dp"
android:textSize="20dp"
android:id="@+id/gst_number"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:layout_below="@+id/gst_number"
android:layout_marginTop="5dp"
android:textSize="20dp"
android:id="@+id/email"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:layout_below="@+id/email"
android:layout_marginTop="5dp"
android:textSize="20dp"
android:id="@+id/policy_link"
/>
</RelativeLayout>
The code for User class is given below
package com.example.prj;
public class User {
private static final String TAG = "User";
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);
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论