无法设置textView值

时间:2019-12-01 12:34:14

标签: android firebase android-layout firebase-realtime-database

经过数小时的搜索,没有结果。我不得不问这个问题。 问题是我无法在addListenerForSingleValueEvent()方法内设置文本视图值。我成功地从数据库中获取了要显示的值,但文本视图的值未更改。我需要了解为什么以及如何解决该问题。这是我的片段类。

public class ProfilFragment extends Fragment {


private FirebaseAuth auth;
private FirebaseDatabase database;
private DatabaseReference usersRef;
private TextView firstName, email;

View myView;

public ProfilFragment(){
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    auth = FirebaseAuth.getInstance();
    database = FirebaseDatabase.getInstance();
    usersRef = database.getReference("Users");

    myView= inflater.inflate(R.layout.fragment_profil,container,false);

    ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Profile");

    ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_profil, null);

    firstName   = root.findViewById(R.id.profileFirstNameTextView);
    email       = root.findViewById(R.id.profileEmailTextView);

    FirebaseUser firebaseUser = auth.getCurrentUser();
    email.setText("bla bla bla");

    if(firebaseUser != null){
        final String userId = firebaseUser.getUid();
        usersRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if(dataSnapshot.hasChild(userId)){
                    User user = dataSnapshot.child(userId).getValue(User.class);
                    Log.d(TAG, "user: " + user.getFirstName());
                    showTextViews(user);

                } else{
                    Toast.makeText(getActivity(), "Couldn't find the user!!",
                            Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

    } else {
        Toast.makeText(getActivity(), "Problem in getting data from Database",
                Toast.LENGTH_SHORT).show();
    }
    return myView;

}

private void showTextViews(User user) {
    Log.d(TAG, "showTextViews: we entered this function");
    firstName.setText(user.getFirstName());
    email.setText(user.getEmail());
}

呈现了两个日志,因此逻辑应该正确。即使在该方法之外将文本视图设置为“ bla bla bla”,它也不起作用。

1 个答案:

答案 0 :(得分:1)

为什么您两次inflate进行相同的观看?尝试使用myView代替root来查找子视图,如下所示:

@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    ....

    myView= inflater.inflate(R.layout.fragment_profil,container,false);
    ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Profile");

    //ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_profil, null);

    firstName   = myView.findViewById(R.id.profileFirstNameTextView);
    email       = myView.findViewById(R.id.profileEmailTextView);

    ....

    return myView;

}