Я пытаюсь получить доступ к UserInfo, я храню в базе данных Firebase Realtime со ссылкой на базу данных, как это уведомление -> SenderUserId -> ReceiverUserId.
Пожалуйста, проверьте изображение, которое я хочу получить доступ ко всем дочерним элементам узла ReceiverId как Я новичок, картинка не появится, есть только ссылка: Ссылка на скриншот, чтобы вы, ребята, могли ясно понять
Я пытаюсь ответить, но это не сработало для меня. 1) Ссылка на ответ
2) Ссылка на второй ответ
3) Ссылка на третий ответ
Как я храню эти данные:
private void AllNotificationInfo()
{
Calendar calForDate = Calendar.getInstance();
SimpleDateFormat currentDate = new SimpleDateFormat("dd-MMMM-yyyy");
saveCurrentDate = currentDate.format(calForDate.getTime());
Calendar calForTime = Calendar.getInstance();
SimpleDateFormat currentTime = new SimpleDateFormat("HH:mm");
saveCurrentTime = currentTime.format(calForTime.getTime());
UsersRef.child(SenderUserId).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists())
{
String userprofileImage = dataSnapshot.child("profileimage").getValue().toString();
String fullname = dataSnapshot.child("Full_Name").getValue().toString();
HashMap postsMap = new HashMap( );
postsMap.put("time", saveCurrentTime);
postsMap.put("date", saveCurrentDate);
postsMap.put("profileimage", userprofileImage);
postsMap.put("fullname", fullname);
NotificationRef.child(SenderUserId).child(ReceiverUserId).updateChildren(postsMap)
.addOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
if (task.isSuccessful()){
Toast.makeText(PersonProfileActivity.this, "Friend Request Sent!", Toast.LENGTH_SHORT).show();
}
else
{
String message = task.getException().getMessage();
Toast.makeText(PersonProfileActivity.this, "Error! "+message, Toast.LENGTH_SHORT).show();
}
}
});
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
Данные хранятся правильно. Нет проблем, но я не могу получить свои данные
, и я храню данные в одном действии и извлекаю в другом действии.
Вот как я пытался получить информацию:
public class NotificationsActivity extends AppCompatActivity {
private Toolbar NotificationToolbar;
private RecyclerView RnotificationList;
private DatabaseReference NotificationRef, UsersRef;
private FirebaseAuth mAuth;
private String notification_sender_id,CurrentUserId;
List<String> NKeyList;
List<NotificationModel> NotificationList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notifications);
mAuth = FirebaseAuth.getInstance();
CurrentUserId = mAuth.getCurrentUser().getUid();
NotificationToolbar = (Toolbar) findViewById(R.id.notification_toolbar_layout);
setSupportActionBar(NotificationToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setTitle("Notifications");
RnotificationList = (RecyclerView) findViewById(R.id.notification_list);
RnotificationList.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
RnotificationList.setLayoutManager(linearLayoutManager);
NotificationRef = FirebaseDatabase.getInstance().getReference().child("Notifications");
NKeyList = new ArrayList<>();
NotificationList = new ArrayList<>();
NotificationRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists())
{
for (DataSnapshot dataSnapshot1: dataSnapshot.getChildren())
{
NKeyList.add(dataSnapshot1.getKey());
// Here I am getting the senderKey and converting into a string, I tried one only at index 0 for testing.
//then I am passing the key to a method where I am using this key for DatabaseReference.
String key = NKeyList.get(0);
GettingRequestSenderInfo(key);
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
private void GettingRequestSenderInfo(final String key) {
DatabaseReference ReqSenderRef = FirebaseDatabase.getInstance().getReference().child("Notifications")
.child(key).child(CurrentUserId);
//Here above "key" is SenderUserId and CurrentuserId is ReceiverUserId
ReqSenderRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists())
{
for (DataSnapshot dataSnapshot1: dataSnapshot.getChildren())
{
NotificationModel model = dataSnapshot1.getValue(NotificationModel.class);
NotificationList.add(model);
}
NotificationAdapter notificationAdapter1 = new NotificationAdapter(NotificationsActivity.this, NotificationList, NKeyList);
RnotificationList.setAdapter(notificationAdapter1);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
Но я получаю NotificationList пустым. значения нет ... Я использую RecyclerView, есть один класс модели и класс адаптера, если необходимо, я включу оба класса. Но я получаю Null NotificationList Все, что я хочу получить доступ к данным, показанным на изображении выше, пожалуйста, проверьте.