没有在函数外部分配值

时间:2019-10-18 11:28:01

标签: android

我没有在函数外部获得分配给变量名称的值。一开始也尝试过初始化值。


public class HomeActivity extends BaseActivity {
    FirebaseAuth mFirebaseAuth;
    TextView tVWelcome;
    String name,email;
    private static final String TAG = "HomeActivity";
    private FirebaseAuth.AuthStateListener mAuthStateListener;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        tVWelcome = (TextView) findViewById(R.id.tvWelcome);

        FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
        String userid = user.getUid();
        DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Users");
        ref.child(userid).addListenerForSingleValueEvent(new ValueEventListener() {
                 @Override
                 public void onDataChange(DataSnapshot dataSnapshot) {
                     name = dataSnapshot.child("fname").getValue().toString();
                     Log.d(TAG,"Name11"+name);
                     email= dataSnapshot.child("email").getValue().toString();
                 }

                 @Override
                 public void onCancelled(DatabaseError databaseError) {

                 }
         });



        String text="Welcome "+name+"!";
        Log.d(TAG,"text:"+text);
            SpannableString ss=new SpannableString(text);
            ClickableSpan clickableSpan=new ClickableSpan() {
                @Override
                public void onClick(View view) {
                    Intent ProfileIntent = new Intent(HomeActivity.this, ProfileActivity.class);
                    startActivity(ProfileIntent);
                }
            };
            ss.setSpan(clickableSpan,8,text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            tVWelcome.setText(ss);
            tVWelcome.setMovementMethod(LinkMovementMethod.getInstance());

    }

}

此外,明智地输出,它是在分配作业之前设置TextView值

输出日志:

2019-10-18 22:13:33.738 10399-10399/com.example.treasurehuntapp D/HomeActivity: text:Welcome null!
2019-10-18 22:13:33.763 10399-10461/com.example.treasurehuntapp D/FA: Logging event (FE): screen_view(_vs), Bundle[{firebase_event_origin(_o)=auto, firebase_previous_class(_pc)=LoginActivity, firebase_previous_id(_pi)=4300107314116723691, firebase_screen_class(_sc)=HomeActivity, firebase_screen_id(_si)=4300107314116723692}]
2019-10-18 22:13:36.556 10399-10399/com.example.treasurehuntapp D/HomeActivity: Name11Joe

请告诉我我在做错什么。

1 个答案:

答案 0 :(得分:0)

我的建议是提取从firebase检索数据的逻辑,以及将数据设置为textView的方法。下面是一个示例:

private void loadData(String userId) {
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Users");
    ref.child(userId).addListenerForSingleValueEvent(new ValueEventListener() {
             @Override
             public void onDataChange(DataSnapshot dataSnapshot) {
                 if (dataSnapshot != null)
                   renderDataToView(dataSnapshot.child("fname").getValue().toString());
             }

             @Override
             public void onCancelled(DatabaseError databaseError) {

             }
     });
}

将数据设置为视图的第二种方法:

private void renderDataToView (String name) {
  if (name != null ) {
     <yourTextView>.setText(name);
   }
}