[英]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.