Firebase android从数据库中获取Child

时间:2017-09-02 15:23:41

标签: android firebase firebase-realtime-database

在android我试图在android中只获取一个键值意味着我将我的数据插入firebase,在firebase数据库中的用户数组中,我正在插入名称和电子邮件作为键。我想检索android中键“name”的值。但是当我重新搜索数据时,它会显示名称中的空值 Here is my firebase database screen shot 这是我的代码

package joinus.joinus.com.firebasedata;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.firebase.database.ChildEventListener;
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;

public class MainActivity extends AppCompatActivity {
    private DatabaseReference mDatabase;
    private TextView txtDetails;
    private EditText inputName, inputEmail;
    private Button btnSave;
    private FirebaseDatabase mFirebaseInstance;
    private String userId;

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

        txtDetails = (TextView) findViewById(R.id.txt_user);
        inputName = (EditText) findViewById(R.id.name);
        inputEmail = (EditText) findViewById(R.id.email);
        btnSave = (Button) findViewById(R.id.btn_save);
        mDatabase = FirebaseDatabase.getInstance().getReference();
        mFirebaseInstance = FirebaseDatabase.getInstance();

        // get reference to 'users' node
        mDatabase = mFirebaseInstance.getReference("users");

        // store app title to 'app_title' node
        mFirebaseInstance.getReference("app_title").setValue("Realtime Database");

        // app_title change listener
        mFirebaseInstance.getReference("app_title").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {


                String appTitle = dataSnapshot.getValue(String.class);

                // update toolbar title
                getSupportActionBar().setTitle(appTitle);
            }

            @Override
            public void onCancelled(DatabaseError error) {
                // Failed to read value

            }
        });
        addUserChangeListener();
        // Save / update the user
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name = inputName.getText().toString();
                String email = inputEmail.getText().toString();

                // Check for already existed userId
//                if (TextUtils.isEmpty(userId)) {
                createUser(name, email);
//                }
            }
        });


    }

    private void createUser(String name, String email) {

        userId = mDatabase.push().getKey();

        Users user = new Users(name, email);

        mDatabase.child(userId).setValue(user);
        addUserChangeListener();
    }

    private void addUserChangeListener() {
        // User data change listener
        mDatabase.child("name").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                // User user = dataSnapshot.getValue(User.class);

                for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
                    // easy
                    Users user = new Users();
                    String name = "" + dataSnapshot1.getValue(Users.class);
                    user.setName(name);
                    //String str = user.name;
                    Toast.makeText(MainActivity.this, name, Toast.LENGTH_SHORT).show();
                    String value = dataSnapshot1.getValue(String.class);
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }


}

1 个答案:

答案 0 :(得分:1)

    mDatabase.child("name").addValueEventListener(new ValueEventListener() {

该行错误因为您没有调用节点"名称"

如果您想获得特定用户名,则必须使用

    mDatabase.child("users").child.(UID).child("name")addValueEventListener(new ValueEventListener() {

如果您要查找特定名称,可以使用

    mDatabase.child("user").orderby("/name").equal("YOUR_NAME_HERE").addValueEventListener(new ValueEventListener() {