简体   繁体   中英

How to get the user's unique id of the item clicked on listview

 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();
                phonenumber=ds.getKey();
                list_user_id = getRef(position).getKey;
//                    Log.d("TAG", name);
                al.add(name);

            }
            listView.setAdapter(adapter);
        }

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

I have a all users activity... If i click on a user i want to fetch their unique id but im unable to do that... error in line - list_user_id = getRef(position).getKey; is my way of fetching id wrong or is the method obsolete?

Since you have a listview you can do the following:

 lists.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            final String selectedFromList = (String)lists.getItemAtPosition(i);
            db= FirebaseDatabase.getInstance().getReference().child("Users");
            db.orderByChild("username").equalTo(selectedFromList).addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    for(DataSnapshot data: dataSnapshot.getChildren()){
                            String ids=datas.getKey();
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });



        }
    });

Assuming you have a listview containing usernames and assuming you have this in your db:

 Users
   userid
      username://name
   userid
      username: //name

Using this final String selectedFromList = (String)lists.getItemAtPosition(i); you are able to get the item in this case the username . Then using the query equalTo() it will check the database and return to you the id of that user using getKey()

You can get all the key values from Firebase DB by using Map in onDataChange method like this:

        Map<String, Object> td = null;
        for (com.google.firebase.database.DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
            td = (HashMap<String, Object>) postSnapshot.getValue();
            String userName= (String) td.get("userName");
            String mobileNUM= (String) td.get("mobileNUM");
            }

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