0
votes

Hello i have created firebase database.there is a one table users and in that i am insrting name and email. from that user i want to only get the value of name column in my android app. i want to toast the value of name.there is no need to get all value from firebase database only i want to get name column value

here is my code

        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

            }
        });

        // 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("users").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Users user = new Users();
                String name = "" + dataSnapshot.getValue(Users.class);
                user.setName(name);
                //String str = user.name;
                Toast.makeText(MainActivity.this, name, Toast.LENGTH_SHORT).show();
                // Check for null
                if (user == null) {

                    return;
                }


                // Display newly updated name and email
                txtDetails.setText(user.name + ", " + user.email);

                // clear edit text
//                inputEmail.setText("");
//                inputName.setText("");

            }

            @Override
            public void onCancelled(DatabaseError error) {
                // Failed to read value
            }
        });
    }
1
key and value are presentmanjiri
in users json array i am inserting name as a key and email as a key. if there is any solution for this provide memanjiri
Please specify your problem clearly :)Malavan

1 Answers

0
votes

First of all, there are not tables in firebase, it is noSQL.

I suggest you to read this , it is really simple and short. If you have the path to a key, you can get the value by attaching a listener to it.

In your situation it should be mDatabase.child("users").child(idOfUser).child(name).addValueEventListener...

It depends on the structure of database.