0
我想從某個表中獲取recieverUID,然後從另一個表中獲取用戶名的值以應對UID。 我認爲正在發生的事情是,在我設置receivedName Array列表爲常量時,因爲App運行良好,所以Adapter被設置在receivedNames Array List被提取之前。 請告訴我什麼是錯在這裏,如果這是這樣做 這裏是我的數據庫看起來像正確的方法, Firebase DB如何等待從Firebase數據庫中提取數據
我對標籤3片段
public class Tab3 extends Fragment {
private static RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
private static RecyclerView recyclerView;
private static ArrayList<String> reminderMessages;
protected static ArrayList<String> receiverNames;
String senderUID;
private DatabaseReference mDatabase;
//Overriden method onCreateView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//Change R.layout.tab1 in you classes
View v = inflater.inflate(R.layout.tab3, container, false);
//get current user
final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
reminderMessages = new ArrayList<>();
receiverNames = new ArrayList<>();
senderUID = user.getUid();
mDatabase = FirebaseDatabase.getInstance().getReference();
Query query = mDatabase.child("reminders").orderByChild("senderUID").equalTo(senderUID);
//Setting size of recycler view as constant
recyclerView = (RecyclerView) v.findViewById(R.id.my_recycler_view);
recyclerView.setHasFixedSize(true);
//Setting Linear Layout
layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
//Getting message from database
String message = ds.child("reminderMessage").getValue(String.class);
//Adding database to ArrayList
reminderMessages.add(message);
//Getting ReceiverUID
String rUID = ds.child("receiverUID").getValue(String.class);
//Getting corresponding username of the ReceiverUID
GetName getName = new GetName();
getName.GetName(rUID);
reminderMessages.add(message);
// Log.d("String names", receiverNames.toString());
}
adapter = new DataAdapter(reminderMessages,receiverNames);
recyclerView.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
//Error in Reaching Database
Toast.makeText(getContext(), "Something went Wrong!", Toast.LENGTH_SHORT).show();
}
});
//Returning the layout file after inflating
return v;
}
這裏的代碼是我的GetName類
public class GetName {
private FirebaseAuth auth;
ListView listView;
private DatabaseReference mDatabase;
String receiverName;
public void GetName(String UID){
mDatabase = FirebaseDatabase.getInstance().getReference().child("users");
Query query = mDatabase.orderByKey().equalTo(UID);
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot2) {
// dataSnapshot is the "issue" node with all children with id 0
for (DataSnapshot users : dataSnapshot2.getChildren()) {
receiverName = users.child("username").getValue(String.class);
receiverNames.add(receiverName);
Log.d("name retrieved",receiverNames.toString());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
謝謝,完美的工作。 –