I'm developing an android application using JAVA and FIREBASE. I've stored user details in Firebase Realtime Database.
My problem is first I want to access all the students based on their course and year. For example I want to retrieve students who opted course BCA and are in year 1st. I don't know how to achieve this.
here is my code.
Main 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();
}
});
}
}
Adapter class
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);
}
}
}
I have 3 buttons in main activity called year1,year2,year3 when i click on year1 i want to display all BCA students who are in year 1st and same goes to other buttons.
Any help would be great.