简体   繁体   English

从firebase数据库填充textView

[英]Populate textView from firebase database

I am trying to populate a TextView from a firebase database. 我试图从firebase数据库填充TextView。 Here is the sample json file. 这是示例json文件。

{
 "Player" : {
  "Club" : "Valley Rovers",
  "Name" : "John Murphy"
 }
}

Here is my android code: 这是我的android代码:

public class MainActivity extends AppCompatActivity {

private   TextView mPlayer;
private DatabaseReference mDatabase;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mPlayer = (TextView) findViewById(R.id.player);;

    mDatabase = FirebaseDatabase.getInstance().getReference("Player").child("Name");

    final String player = mDatabase.push().getKey();

    mDatabase.child("Name").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            // Check for null
            if (player == null) {
                Log.e(TAG, "User data is null!");
                return;
            }




            mPlayer.setText(player);




        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}

I want to add the name John Murphy to the firebase database and doing so the TextView mPlayer will populate with John Murphy. 我想将名称John Murphy添加到firebase数据库中,并且这样做TextView mPlayer将填充John Murphy。

You are getting the child "Name" twice on your code (leading to Player/Name/Name). 您在代码上两次获取子名称“Name”(导致播放器/名称/名称)。 Remove it from the mDatabase initialization: 从mDatabase初始化中删除它:

mDatabase = FirebaseDatabase.getInstance().getReference("Player");

And you're never really getting the value from the DataSnapshot received. 而且你从来没有真正从收到的DataSnapshot获得价值。 To do so, use this: 为此,请使用此:

mDatabase.child("Name").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String playerName = dataSnapshot.getValue(String.class);
            mPlayer.setText(playerName);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

In case someone wants to use POJO approach to this problem in future, follow the example below: 如果有人想在将来使用POJO方法解决此问题,请按照以下示例操作:

Remove the child("Name") from mDatabase.child("Name").addValueEventListener(new ValueEventListener() unless you want to fetch the "Name" child only. Also remember to effect the correction made on mDatabase initialization. mDatabase.child("Name").addValueEventListener(new ValueEventListener()删除子mDatabase.child("Name").addValueEventListener(new ValueEventListener()除非您只想获取“Name”子项。还记得对mDatabase初始化进行校正。

mDatabase.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        // String playerName = dataSnapshot.getValue(String.class);
        PlayerModel playerModel =  dataSnapshot.getValue(PlayerModel.class);
         ((TextView)findViewById(R.id.textviewName)).setText(playerModel.getName());
        ((TextView)findViewById(R.id.textviewClub)).setText(playerModel.getClub());
        // mPlayer.setText(playerName);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM