我正在从Firebase的Firestore DB加载一个列表来填充一个微调器 . 这样可行 . 我想在微调器中的ItemSelected上填充RecyclerView .
我有这个代码来填充RecyclerView:
private void populateRecycler(final String RootCollection, final String UserID) {
final String UserID = getIntent().getExtras().getString(UserIDKey);
mQuery = mFirestore
.collection(RootCollection)
.whereEqualTo("UID", UserID)
.orderBy("DateAdded", Query.Direction.DESCENDING);
mAdapter = new OrdersAdapter(mQuery, HomeActivity.this);
mOrdersRecycler.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
mOrdersRecycler.setAdapter(mAdapter);
}
如果从onCreate放置或调用此代码,则此代码可以正常工作 . 但是我想稍后修改代码,以便稍后在spinner中填充String . (可能只是为了添加另一个.whereEqualto)
当我从这里打电话给 populateRecycler() 时,它不会使用ButterKnife,fyi):'t work (I'
@OnItemSelected(R.id.storespinner)
public void spinnerItemSelected(Spinner spinner, int position) {
String spinnerValue = mStoreSpinner.getItemAtPosition(position).toString();
Toast.makeText(getBaseContext(), "Selected item " + spinnerValue, Toast.LENGTH_SHORT).show();
populateRecycler("test", UserID);
}
或者,如果我从另一个onSuccess侦听器(Firebase Firestore数据库调用)调用populateRecycler,它也不起作用:
public void onSuccess(DocumentSnapshot documentSnapshot) {
UIDOrg uidOrg = documentSnapshot.toObject(UIDOrg.class);
mOrgTextView.setText(uidOrg.getOrg());
RootCollection = uidOrg.getRootCollection();
populateRecycler("test", UserID);
有人可以指点我正确的方向吗?
为什么 populateRecycler 仅在从onCreate调用时才起作用?
编辑:这是我填充微调器数组的方式 .
public void onSuccess(DocumentSnapshot documentSnapshot) {
UIDOrg uidOrg = documentSnapshot.toObject(UIDOrg.class);
mOrgTextView.setText(uidOrg.getOrg());
RootCollection = uidOrg.getRootCollection();
//Populate Spinner here
readData(new FirestoreCallBack() {
@Override
public void onCallback(List list) {
ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mStoreSpinner.setAdapter(adapter);
}
});
//End of Spinner
}
在readData()中它是:
public void onComplete(@NonNull Task task) {
if (task.isSuccessful()) {
for (DocumentSnapshot document : task.getResult()) {
spinnerArray.add(String.valueOf(document.getId()));
}
firestoreCallBack.onCallback(spinnerArray);
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}