【问题标题】:Get values from Firebase Database on basis of two key values [duplicate]根据两个键值从 Firebase 数据库获取值 [重复]
【发布时间】:2020-05-24 07:23:52
【问题描述】:

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

我的问题是首先我想根据他们的课程和年份访问所有学生。例如,我想检索选择了 BCA 课程并且在第一年的学生。我不知道如何实现这一点。

这是我的代码。

主要活动

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);
        }
    }
}

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

任何帮助都会很棒。

【问题讨论】:

    标签: java android android-studio firebase-realtime-database


    【解决方案1】:

    你可以通过过滤列表来做到这一点。

    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();

    【讨论】:

    • 什么是 .size(), .get() ?如何使用它?它在我的活动中显示错误。
    猜你喜欢
    • 1970-01-01
    • 2018-10-03
    • 1970-01-01
    • 1970-01-01
    • 2018-06-18
    • 2020-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多