gpt4 book ai didi

java - 根据两个键值从 Firebase 数据库获取值

转载 作者:行者123 更新时间:2023-12-01 16:34:45 25 4
gpt4 key购买 nike

我正在使用 JAVA 和 FIREBASE 开发 Android 应用程序。我已将用户详细信息存储在 Firebase 实时数据库中。

Database image

我的问题是首先我想根据所有学生的类(class)和年份访问他们。例如,我想检索选择 BCA 类(class)且一年级的学生。我不知道如何实现这一目标。

这是我的代码。

主要 Activity

public class MainActivityStudentList extends AppCompatActivity {

private Button year1,year2,year3;
private RecyclerView studentRecyclerView;
private String branch,year = "1";

private DatabaseReference databaseReference;
private Query query;

private ShowStudentsAdapter showStudentsAdapter;
private List<AddStudents> addStudents;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_list);

assert getSupportActionBar() != null;
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

branch = getIntent().getStringExtra("branch");

year1 = findViewById(R.id.button1);
year2 = findViewById(R.id.button2);
year3 = findViewById(R.id.button3);

year1.setText("YEAR 1");
year2.setText("YEAR 2");
year3.setText("YEAR 3");

studentRecyclerView = findViewById(R.id.showRecyclerView);

studentRecyclerView.setHasFixedSize(true);
studentRecyclerView.setLayoutManager(new LinearLayoutManager(this));
addStudents = new ArrayList<>();

databaseReference = FirebaseDatabase.getInstance().getReference("STUDENTS");

getData();

year1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
year1.setBackgroundColor(getResources().getColor(R.color.white));
year2.setBackgroundColor(getResources().getColor(R.color.lightBlue));
year3.setBackgroundColor(getResources().getColor(R.color.lightBlue));
addStudents.clear();
year = "1";
getData();
}
});

year2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
year1.setBackgroundColor(getResources().getColor(R.color.lightBlue));
year2.setBackgroundColor(getResources().getColor(R.color.white));
year3.setBackgroundColor(getResources().getColor(R.color.lightBlue));
addStudents.clear();
year = "2";
getData();
}
});

year2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
year1.setBackgroundColor(getResources().getColor(R.color.lightBlue));
year2.setBackgroundColor(getResources().getColor(R.color.lightBlue));
year3.setBackgroundColor(getResources().getColor(R.color.lightBlue));
addStudents.clear();
year = "3";
getData();
}
});
}

@Override
public void onBackPressed() {
super.onBackPressed();
addStudents.clear();
}

private void getData(){
query = databaseReference.orderByChild("course").equalTo(branch);
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
AddStudents addStudent = dataSnapshot1.getValue(AddStudents.class);
addStudents.add(addStudent);
}
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainActivityStudentList.this);
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
studentRecyclerView.setLayoutManager(linearLayoutManager);
showStudentsAdapter = new ShowStudentsAdapter(MainActivityStudentList.this, addStudents);
studentRecyclerView.setAdapter(showStudentsAdapter);
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(MainActivityStudentList.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}

适配器类

public class ShowStudentsAdapter extends RecyclerView.Adapter<ShowStudentsAdapter.showStudentViewHolder> {

private Context dContext;
private List<AddStudents> addStudents;

public ShowStudentsAdapter(Context context,List<AddStudents> addStudent){
dContext = context;
addStudents = addStudent;
}

@NonNull
@Override
public showStudentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(dContext).inflate(R.layout.show_faculty_cardview,parent,false);
return new showStudentViewHolder(v);
}

@Override
public void onBindViewHolder(@NonNull showStudentViewHolder holder, int position) {
AddStudents dAddStudent = addStudents.get(position);
holder.name.setText(dAddStudent.getName());
holder.course.setText(dAddStudent.getCourse());
holder.year.setText(dAddStudent.getYear());
}

@Override
public int getItemCount() {
return addStudents.size();
}

public class showStudentViewHolder extends RecyclerView.ViewHolder{
TextView name,course,year;
ImageView delete;

public showStudentViewHolder(@NonNull View itemView) {
super(itemView);
name = itemView.findViewById(R.id.fName);
course = itemView.findViewById(R.id.fEmail);
year = itemView.findViewById(R.id.fPosition);
delete = itemView.findViewById(R.id.fDelete);
}
}
}

我在主要 Activity 中有 3 个按钮,称为 Year1、year2、year3,当我单击 Year1 时,我想显示所有一年级的 BCA 学生,其他按钮也是如此。

任何帮助都会很棒。

最佳答案

您可以通过过滤列表来完成此操作。

List<AddStudents> addStudentFilterd = new ArrayList<AddStudents>();

for (int i = 0; i < addStudent.size(); i++) {
if( addStudent.get(i).getCourse.equals("BCA") && addStudent.get(i).getYear.equals("1")){
addStudentFilterd.add(addStudent.get(i));
}

单击按钮时将过滤后的列表传递给适配器,最后 .notifyDataSetChanged();

关于java - 根据两个键值从 Firebase 数据库获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61982824/

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