我有一个文档集合,我正在尝试将每个“文档”字段“名称”放入卡堆栈中。集合称为“用户”,文档称为“ userId”。每个文档都包含该用户的“名称”和“性别”字段。到目前为止,iv能够将当前登录的用户名获取到卡栈,但是在将数据库中的其他用户获取到栈时遇到了问题。这是我正在使用的代码.Logcat中没有出现任何错误或遇到任何崩溃。只是不添加数据。有什么建议?
public class MainActivity extends AppCompatActivity {
public static final String TAG = MainActivity.class.getSimpleName();
TextView profileName,gender,StrangerName;
FirebaseAuth mAuth;
FirebaseFirestore dataBase;
String userID;
ImageView ProfilePic;
private SwipeCardsView swipeCardsView;
private List<Model> modelList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuth = FirebaseAuth.getInstance();
dataBase = FirebaseFirestore.getInstance();
userID = mAuth.getCurrentUser().getUid();
ProfilePic = findViewById(R.id.profilePic);
StrangerName = findViewById(R.id.name);
gender = findViewById(R.id.thisperson);
profileName = findViewById(R.id.userName);
//SwipeCards
swipeCardsView = findViewById(R.id.swipeCardsView);
swipeCardsView.retainLastCard(false);
swipeCardsView.enableSwipe(true);
getUserDB();
getData();
}
// Method to get Users Documents
private void getUserDB () {
dataBase.collection("Users").get().addOnCompleteListener(new
OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
List<String> list = new ArrayList<>();
for (QueryDocumentSnapshot document : task.getResult()) {
list.add(document.getString("Name"));
}
Log.d(TAG, list.toString());
String strangers = list.toString();
StrangerName.setText(strangers);
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}});
}
// CardAdpater method
private void getData() {
modelList.add(new Model(StrangerName,ProfilePic));
CardAdpater cardAdpater = new CardAdpater(modelList,this);
swipeCardsView.setAdapter(cardAdpater);
}
private class CardStackTransformer implements ViewPager.PageTransformer {
@Override
public void transformPage(@NonNull View page, float position) {
}
//Logout onClick
public void LogOut(View view) {
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(getApplicationContext(), Login.class));
finish();
}
}
}
答案 0 :(得分:2)
尝试以下代码:我使用list来输入所有名称。随意询问:)
dataBase.collection("Users").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
List<String> list = new ArrayList<>();
for (QueryDocumentSnapshot document : task.getResult()) {
list.add(document.getString("Name"););
}
Log.d(TAG, list.toString());
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}});