简体   繁体   中英

i want to get the user unique id. how do i fetch it using getKey method

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference usersRef = rootRef.child("Users");
    ValueEventListener eventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            ListView listView = (ListView) rootView.findViewById(R.id.usersList);
            ArrayAdapter<String> adapter;
            adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, al);
            for(DataSnapshot ds : dataSnapshot.getChildren()) {
                name = ds.child("Name").getValue().toString();

//                    Log.d("TAG", name);
                al.add(name);
                String list_user_id = getRef(position).getKey();
            }
            listView.setAdapter(adapter);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Toast.makeText(getActivity(),"Please Check Your Internet Connection",Toast.LENGTH_SHORT).show();
        }
    };
    usersRef.addListenerForSingleValueEvent(eventListener);

In the line of string list_user_id = getRef(Position).getKey(): I guess this is returning null because when the user clicks on the item in listview

 usersList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            UserDetails.chatWith = al.get(position);
            Intent intent = new Intent(getActivity(), Chat.class);
            intent.putExtra("visit_user_id",list_user_id);
            intent.putExtra("user_name",name);
            startActivity(intent);
        }
    });
    return rootView;

He will be directed to chat activity... as soon as i open chat avtivity im getting null pointerexception on this line

   MessageRecieverId = getIntent().getExtras().get("visit_user_id").toString();

Where have i gone wrong? trying to make a chat application so need to fetch the unique id of the user... Database screenshot http://ibb.co/cwOQtx

getting a null error again

rootRef.child("Messages").child(MessageSenderId).child(MessageRecieverId)
            .addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            Messages messages = dataSnapshot.getValue(Messages.class);
            messagesList.add(messages);
            messageAdapter.notifyDataSetChanged();
        }

error in the line rootref.chil("messages")........ Error- http://ibb.co/h4D2dx

Change the code( inside of ValueEventListener) :

String list_user_id = getRef(position).getKey();

to this:

String list_user_id = ds.getKey();

for(DataSnapshot ds : dataSnapshot.getChildren()) ds here is iterating inside the direct children which are the unique ids, then by using the above you will be able to retrieve the unique ids.

Also according to the firebase docs:

getRef() does not accept any parameters.

more info here:-

https://firebase.google.com/docs/reference/android/com/google/firebase/database/DataSnapshot.html#getRef()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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