[英]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);
在字符串list_user_id = getRef(Position).getKey()的行中:我猜这返回的是null,因为当用户单击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;
他将被定向到聊天活动...一旦我打开聊天活动,即时通讯在此行上获取空的pointerexception
MessageRecieverId = getIntent().getExtras().get("visit_user_id").toString();
我哪里出问题了? 尝试制作聊天应用程序,因此需要获取用户的唯一ID。数据库屏幕快照http://ibb.co/cwOQtx
再次得到一个空错误
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();
}
误差在线路rootref.chil(“消息”)........错误- http://ibb.co/h4D2dx
更改代码(在ValueEventListener内部):
String list_user_id = getRef(position).getKey();
对此:
String list_user_id = ds.getKey();
for(DataSnapshot ds : dataSnapshot.getChildren())
此处的ds是在直接子级中进行迭代的,这些子级是唯一的ID,然后使用上述方法,您将能够检索到唯一的ID。
另外根据firebase文档:
getRef()不接受任何参数。
更多信息在这里:-
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.