gpt4 book ai didi

android - 如何使用 Firebase 数据库仅在 Recycler View 中检索特定数据?

转载 作者:行者123 更新时间:2023-12-04 10:05:09 25 4
gpt4 key购买 nike

我在从 Firebase 实时数据库检索特定数据时遇到问题。我的问题是,我只想在 RecyclerView 中显示具有 Course_ID(如下图所示)等于 Course_ID 的 Material (请参阅类(class) -> Firebase 中的教师类(class))。我怎样才能完成那件事?我将附上 RecyclerView 中使用的代码和包含模型的类。
提一下: 1.我尝试从 Firebase 添加所有类(class) ID 并将它们存储到一个列表中,但该应用程序没有显示任何内容和 2. 在另一个类(class)中,我有一个 Intent 将我发送到这里并发送一个我已访问的带有 Course_ID 的额外字符串。
我在等待您的回复。谢谢!

文件 Material 类

public class FileMaterial {

private String Course_ID;
private String Denumire_material;
private String Locatie_material;
private String Teacher_ID;

public FileMaterial() {
}

public FileMaterial(String course_ID, String denumire_material, String locatie_material, String teacher_ID) {
Course_ID = course_ID;
Denumire_material = denumire_material;
Locatie_material = locatie_material;
Teacher_ID = teacher_ID;
}

public String getCourse_ID() {
return Course_ID;
}

public void setCourse_ID(String course_ID) {
Course_ID = course_ID;
}

public String getDenumire_material() {
return Denumire_material;
}

public void setDenumire_material(String denumire_material) {
Denumire_material = denumire_material;
}

public String getLocatie_material() {
return Locatie_material;
}

public void setLocatie_material(String locatie_material) {
Locatie_material = locatie_material;
}

public String getTeacher_ID() {
return Teacher_ID;
}

public void setTeacher_ID(String teacher_ID) {
Teacher_ID = teacher_ID;
}

类(class) Material .class
public class CourseMaterial extends AppCompatActivity {

private RecyclerView recyclerView;
private DatabaseReference reference, userReference;
private FirebaseAuth mAuth;
FirebaseRecyclerOptions<FileMaterial> options;
FirebaseRecyclerAdapter<FileMaterial, CourseMaterial.FileViewHolder> adapter;
ImageView btnAddMaterial;
ImageView deleteMaterial;
StorageReference storageReference;
FirebaseStorage firebaseStorage;
String urlReference;
String value;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_course_material);
value = getIntent().getStringExtra("course id").toString();
mAuth = FirebaseAuth.getInstance();
reference = FirebaseDatabase.getInstance().getReference().child("Materials").child(mAuth.getCurrentUser().getUid());
recyclerView = findViewById(R.id.recyclerView_fileMaterials);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));

btnAddMaterial = findViewById(R.id.addMaterials);
btnAddMaterial.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(CourseMaterial.this, UploadFile.class));
}
});

}

@Override
public void onStart() {
super.onStart();

options = new FirebaseRecyclerOptions.Builder<FileMaterial>().setQuery(reference, FileMaterial.class).build();
adapter = new FirebaseRecyclerAdapter<FileMaterial, FileViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull final FileViewHolder fileViewHolder, int i, @NonNull final FileMaterial fileMaterial) {
fileViewHolder.denumire_material.setText(fileMaterial.getDenumire_material());

}


@NonNull
@Override
public FileViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.youtube_view,parent, false);
FileViewHolder fileViewHolder = new FileViewHolder(v);

return fileViewHolder;
}
};
adapter.startListening();
recyclerView.setAdapter(adapter);
}

public static class FileViewHolder extends RecyclerView.ViewHolder{
TextView denumire_material, dataMaterial;
ImageView deleteMaterial;
public FileViewHolder(@NonNull View itemView) {
super(itemView);
denumire_material = itemView.findViewById(R.id.txtDenMaterial);
deleteMaterial = itemView.findViewById(R.id.imgDeleteMaterial);
}
}

最佳答案

也许做一个 query并将其传递给 options适配器:

@Override
public void onStart() {
super.onStart();

Query query = reference.orderByChild("Course_ID").equalTo(value);

options = new FirebaseRecyclerOptions.Builder<FileMaterial>().setQuery(query, FileMaterial.class).build();

.......
.......
.......

关于android - 如何使用 Firebase 数据库仅在 Recycler View 中检索特定数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61622893/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com