简体   繁体   English

有没有办法只显示与 Spinner 的一项有关的文档而不是其他人一起堆积的文档?

[英]Is there any way to show only Docs pertaining to one item of Spinner not others pilling up one together?

I have Spinner having Student classNames(ist-8th).我有 Spinner 有学生类名(ist-8th)。 when I select One class, firestore multiple docs related to that item show up in recyclerview .当我 select 一个 class 时,在recyclerview中显示与该项目相关的多个文档 But when I select another class, the docs get pilled up below previous one.但是当我 select 另一个 class 时,文档被堆积在前一个之下。 I want only those docs to remain on recyclerview which have same filed of className of Spinner.我只希望那些文档保留在 recyclerview 上,它们具有相同的 Spinner 类名。

The code for ClassViewActivity Is.. ClassViewActivity 的代码是..

public class ViewClasswise extends AppCompatActivity {
    private static final String TAG = ViewClasswise.class.getSimpleName();
    private RecyclerView recyclerViewCW;
    ArrayList<ModelViewClasswise>classlist;
    FirebaseFirestore db;
    FirebaseAuth fAuth;
    ClasswiseAdapter classwiseAdapter;
    Spinner s1;

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

        recyclerViewCW = findViewById(R.id.recyclerviewCW);
        recyclerViewCW.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));

        recyclerViewCW.setLayoutManager(new LinearLayoutManager(this));
        classlist=new ArrayList<>();
        classwiseAdapter=new ClasswiseAdapter(classlist);
        recyclerViewCW.setAdapter(classwiseAdapter);
        s1=findViewById(R.id.spinnerSearch);

        db= FirebaseFirestore.getInstance();
        fAuth= FirebaseAuth.getInstance();
        FirebaseUser user = fAuth.getCurrentUser();

        //spinner Listener
        s1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View view, int position, long id) {
                String selectedClass = (String) parentView.getItemAtPosition(position);

                db.collection("Schools").document(user.getUid()).collection("Students")
                        .whereEqualTo("className",selectedClass)
                        .get()
                        .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                            @Override
                            public void onSuccess(QuerySnapshot queryDocumentSnapshots) {

                                List<DocumentSnapshot> list=queryDocumentSnapshots.getDocuments();

                                for(DocumentSnapshot d:list){

                                    ModelViewClasswise obj=d.toObject(ModelViewClasswise.class);
                                    classlist.add(obj);
                                }
                                //Update Adapter
                                classwiseAdapter.notifyDataSetChanged();
                            }
                        });


            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }


}

Code For Adapter is....适配器代码是....

public class ClasswiseAdapter extends RecyclerView.Adapter<ClasswiseAdapter.studentViewHolder>{
    private static final String TAG =ClasswiseAdapter.class.getSimpleName() ;
    private ArrayList<ModelViewClasswise> classlist;
    private ArrayList<ModelViewClasswise> newClassList;



    public ClasswiseAdapter(ArrayList<ModelViewClasswise> classlist) {

        this.classlist = classlist;



    }
    public void addnewList(ArrayList<ModelViewClasswise> newClassList){
        this.classlist = newClassList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ClasswiseAdapter.studentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.row_classwise,parent,false);
        return new ClasswiseAdapter.studentViewHolder(view);


    }

    @Override
    public void onBindViewHolder(@NonNull studentViewHolder holder, int position) {

        holder.t1.setText(classlist.get(position).getName());
        holder.t2.setText(classlist.get(position).getClassName());
        holder.t3.setText(classlist.get(position).getRollNumber());


    }







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

    }





    class studentViewHolder extends RecyclerView.ViewHolder{
        TextView t1,t2,t3;

        public studentViewHolder(@NonNull View itemView) {
            super(itemView);
            t1=itemView.findViewById(R.id.nameC);
            t2=itemView.findViewById(R.id.classC);
            t3=itemView.findViewById(R.id.rollNumC);

        }

    }

}

Thank you everyone!谢谢大家! I added我添加了

classlist.clear()

and it worked它奏效了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 仅更改一项微调器 android 的样式 - Change style of only one item spinner android JavaFX:有没有办法只禁用微调器的一个按钮? - JavaFX: Is there a way to disable only one button of the Spinner? RecyclerView 只显示一项 - RecyclerView show only one item 如何显示列表中一项的视图并隐藏其他项? - How to show view for one item in a list and hide from others.? JComboBox仅显示数据库中的一项 - JComboBox only show one item from database 当用户单击微调器中的一个项目时,它应该显示另一个项目数据将从JSON Web服务获取 - when user clicks on one item from spinner then it should show another item data will be get from JSON webservice 有什么方法可以仅将依赖项应用于一个软件包吗? - Is there any way to apply dependency to one package only? Android当微调器选择了一项时,它将根据数据库中的数据名称在listview中显示信息 - Android when spinner selected one of the item, it will show the information in listview according to their data name from database 如何在一个片段中显示我的微调框 - How to show my spinner within one fragment 为什么我的Android ListAdapter只显示一个项目? - Why does my Android ListAdapter only show one item?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM