我在Firestore
和RecycleView
有一个数据库来放置数据。我向query
发送了一些Firestore
,根据我的要求,我会得到结果。但是,如果我想根据要求从发给我的集合中取3个随机结果。例如,我得到了20个结果,并且每次发送请求时都希望只获得3个结果。每一次,它必须是随机的3个结果来自这个20?
我发现只有一个相同问题的解决方案:
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference studentsCollectionReference = rootRef.collection("students");
studentsCollectionReference.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
List<Students> studentList = new ArrayList<>();
for (DocumentSnapshot document : task.getResult()) {
Student student = document.toObject(Student.class);
studentList.add(student);
}
int studentCount = studentList.size();
int randomNumber = new Random().nextInt(studentCount);
List<Students> randomStudentList = new ArrayList<>();
for(int i = 1; i < 10; i++) {
randomStudentList.add(studentList.get(randomNumber));
}
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});
但这是关于如何从Firestore
获取一些随机数据,而不是从请求中的数据样本中获取。它有什么解决方案吗?
我的RecycleView
课程:
public class Myactivity extends AppCompatActivity {
public RecyclerView mResultList;
public FirebaseFirestore mFirestore;
public com.google.firebase.firestore.Query query;
public FirestoreRecyclerAdapter<Places, PlaceViewHolder> firestoreRecyclerAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycle_activity);
mResultList = findViewById(R.id.list_result);
Boolean l_check1 = getIntent().getExtras().getBoolean("1");
Boolean l_check2 = getIntent().getExtras().getBoolean("2");
Boolean l_check3 = getIntent().getExtras().getBoolean("3");
mFirestore = FirebaseFirestore.getInstance();
if (l_check1) {
query = mFirestore.collection("Places").whereEqualTo("colour", "1").limit(20);
query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
List<Places> placesList = new ArrayList<>();
for (DocumentSnapshot document : task.getResult()) {
Places place = document.toObject(Places.class);
placesList.add(place);
}
int placeCount = placesList.size();
int randomNumber = new Random().nextInt(placeCount);
List<Places> randomPlaceList = new ArrayList<>();
for (int i=1; i<=3; i++) {
randomPlaceList.add(placesList.get(randomNumber));
}
}
}
});
} else if (l_check2) {
query = mFirestore.collection("Places").whereEqualTo("colour", "2").limit(3);
} else if (l_check3) {
query = mFirestore.collection("Places").whereEqualTo("colour", "3").limit(3);
}
mResultList.setLayoutManager(new LinearLayoutManager(this));
FirestoreRecyclerOptions<Places> options = new FirestoreRecyclerOptions.Builder<Places>()
.setQuery(query, Places.class)
.build();
firestoreRecyclerAdapter = new FirestoreRecyclerAdapter<Places, PlaceViewHolder>(options) {
@Override
protected void onBindViewHolder(PlaceViewHolder holder, int position, Places model) {
holder.setDetails(getApplicationContext(), model.getName(), model.getImage());
}
@Override
public PlaceViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_layout, parent, false);
return new PlaceViewHolder(view);
}
};
mResultList.setAdapter(firestoreRecyclerAdapter);
}
@Override
protected void onStart() {
super.onStart();
firestoreRecyclerAdapter.startListening();
}
class PlaceViewHolder extends RecyclerView.ViewHolder {
View mView;
public PlaceViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setDetails(Context context, String placeName, String placeImage) {
final TextView place_Name = mView.findViewById(R.id.text_image_id);
ImageView place_Image = mView.findViewById(R.id.image_id);
place_Name.setText(placeName);
Glide.with(context).load(placeImage).into(place_Image);
place_Name.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), Card_activity.class);
intent.putExtra("qwerty", place_Name.getText());
startActivity(intent);
}
});
}
}
@Override
protected void onStop() {
super.onStop();
if (firestoreRecyclerAdapter != null) {
firestoreRecyclerAdapter.stopListening();
}
}
}
我从另一个Extra
获取了一些activity
并根据此结果并在if
循环中进行检查,我在query
中创建了Firestore
,然后从搜索样本中我取3个结果,抓住&#34;名称&#34;和&#34;图像&#34;字段并在RecycleView
中设置它们。但如果条件相同,那么结果是一样的,
只需按Firestore
中的前三个字段按顺序排列,如果符合条件,则会显示在RecycleView
中。我需要这样每次,它的3个结果将是随机的。
已编辑:我的Place_class:
public class Places {
private String image, name;
public Places() { }
public Places(String image, String name) {
this.image = image;
this.name = name;
}
public String getImage() { return image; }
public String getName() { return name; }
}
答案 0 :(得分:1)
我看到你使用了here的确切答案,这是我实际写的。可以通过一种非常简单的方式修改答案,以达到您想要的效果,但您需要使用Query
。所以请添加/修改以下代码行:
Query query = studentsCollectionReference.orderBy("name").limit(20);
query.get().addOnCompleteListener(/* ... */);
然后更改for循环以仅返回3个随机记录:
List<Students> randomStudentList = new ArrayList<>();
for(int i = 1; i <= 3; i++) {
randomStudentList.add(studentList.get(randomNumber));
}