简体   繁体   English

如何获得用户在列表视图上单击的商品的唯一ID

[英]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; 我有一个所有用户的活动...如果我单击某个用户,我想获取其唯一ID,但是我无法执行此操作...行中的错误-list_user_id = getRef(position).getKey; is my way of fetching id wrong or is the method obsolete? 我获取id的方式错误还是该方法过时了?

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); 使用final String selectedFromList = (String)lists.getItemAtPosition(i); you are able to get the item in this case the username . 在这种情况下,您可以获取该项目的username Then using the query equalTo() it will check the database and return to you the id of that user using getKey() 然后使用查询equalTo() ,它将检查数据库并使用getKey()向您返回该用户的ID。

You can get all the key values from Firebase DB by using Map in onDataChange method like this: 您可以通过在onDataChange方法中使用Map从Firebase DB获取所有键值,如下所示:

        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");
            }

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

相关问题 如何使用listview.GetItemAtPosition(e.Position)获取用户单击的ListView项目的数据? - How to get the data of a ListView item the user has clicked on with listview.GetItemAtPosition(e.Position)? 如何获取ListView中单击的项目的路径 - How to get the path of the clicked item in a ListView Android:如何获取ListView中单击的项目位置? - Android: how to get the clicked item position in the ListView? 如何在Android ListView中为每个项目设置唯一ID? - How to set unique ID for each Item in Android ListView? 如何在onScroll的ListView中获得第一项的ID? - How to get Id of the top item in ListView at onScroll? 如何从下拉列表视图JavaFX 8中获取文本框中的单击项? - How to get clicked item in textbox from dropdown listview javafx 8? 如何获取Android中列表视图中单击的项目的字符串值 - How to get String value of item clicked in listview in Android 如何从Android中的“自定义ListView”获取被点击项的位置? - How to get clicked item position from Custom ListView in android? 如何获取ListView项的ID,而不是ListView上的位置? - How can I get the ID of the ListView Item instead of the position on the listview? 无法获取项目的唯一ID,请单击Listview - Not able to fetch Unique id of an item click on Listview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM