Issue
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 issrc="https://i.stack.imgur.com/VfKcK.png" alt="">
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.
Solution
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);
...
Answered By - Frank van Puffelen
Answer Checked By - Timothy Miller (JavaFixing Admin)