I want display user info on a fragment. I am getting data from firebase, I've checked this by logging user phone number. The problem is, that data is not displaying on the fragment (in TextView). Below is the respective fragment's code.
package com.example.testmechin;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.testmechin.Common.Common;
public class Userprofile_frag extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
LayoutInflater layoutInflater= getActivity().getLayoutInflater();
View view = layoutInflater.inflate(R.layout.fragment_userprofile_frag, container, false); //pass the correct layout name for the fragment
TextView username = (TextView) view.findViewById(R.id.usernameedittxt);
TextView phonenumber = (TextView) view.findViewById(R.id.phoneedittxt);
TextView useremail = (TextView) view.findViewById(R.id.emailedittxt);
username.setText(Common.userName());
useremail.setText(Common.currentUser!=null?Common.currentUser.getEmail():"");
phonenumber.setText(Common.currentUser!=null?Common.currentUser.getPhonenumber():"");
Log.d("userPhone: ",Common.currentUser.getEmail());
return inflater.inflate(R.layout.fragment_userprofile_frag, container, false);
}
}
CodePudding user response:
Return the inflated view associated with your textviews, not a newly inflated one:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
LayoutInflater layoutInflater= getActivity().getLayoutInflater();
View view = layoutInflater.inflate(R.layout.fragment_userprofile_frag, container, false); //pass the correct layout name for the fragment
TextView username = (TextView) view.findViewById(R.id.usernameedittxt);
TextView phonenumber = (TextView) view.findViewById(R.id.phoneedittxt);
TextView useremail = (TextView) view.findViewById(R.id.emailedittxt);
username.setText(Common.userName());
useremail.setText(Common.currentUser!=null?Common.currentUser.getEmail():"");
phonenumber.setText(Common.currentUser!=null?Common.currentUser.getPhonenumber():"");
Log.d("userPhone: ",Common.currentUser.getEmail());
return view; //not return inflater.inflate(R.layout.fragment_userprofile_frag, container, false);
}