美文网首页
利用Litepal的查询功能做一个搜索栏

利用Litepal的查询功能做一个搜索栏

作者: 今天也要努力呀y | 来源:发表于2019-07-23 21:09 被阅读0次

    搜索出来的在RecyclerView显示出来:
    find_Adapter

    public class find_Adapter extends RecyclerView.Adapter<find_Adapter.ViewHolder>{
        private List<Student> findStudent = new ArrayList<>();
        Context context;
        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.activity_list_item,viewGroup,false);
            return new ViewHolder(view);
        }
        public find_Adapter(List<Student> findStudent,Context context){
            this.findStudent =findStudent;
            this.context = context;
        }
        @Override
        public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int i) {
            final Student student = findStudent.get(i);
            Glide.with(context).load(student.getImagePath()).into(viewHolder.student_picture);
            viewHolder.stusent_major.setText(student.getMajor());
            viewHolder.student_name.setText(student.getName());
            viewHolder.student_id.setText(student.getStudent_number());
            viewHolder.student_grade.setText(student.getGrade());
            viewHolder.detail.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //Toast.makeText(v.getContext(),"你点击了第"+i+"个按钮",Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(context, ShowStudent.class);
                    intent.putExtra("position1",i);
                    intent.putExtra("path",student.getImagePath());
                    context.startActivity(intent);
                }
                });
        }
    
        @Override
        public int getItemCount() {
            return findStudent.size();
        }
    
        public class ViewHolder extends RecyclerView.ViewHolder {
            TextView student_name;
            TextView student_id;
            TextView student_grade;
            TextView stusent_major;
            ImageView student_picture;
            LinearLayout detail;
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                student_grade = itemView.findViewById(R.id.student_grade);
                student_id = itemView.findViewById(R.id.student_id);
                student_name = itemView.findViewById(R.id.student_name);
                stusent_major = itemView.findViewById(R.id.student_major);
                student_picture = itemView.findViewById(R.id.student_image);
                detail = itemView.findViewById(R.id.detail);
            }
        }
    }
    
    private void iniRecyclerView() {
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
            recyclerView.setLayoutManager(linearLayoutManager);
            find_adapter = new find_Adapter(findStudentList, context);
            recyclerView.setAdapter(find_adapter);
            findStudentList.clear();
        }
    
    public static List<Student> findStudentList = new ArrayList<>();
        public static find_Adapter find_adapter;
    
    
    private void init() {
            ////litepal的查询操作
    
            String mess = fill_id.getText().toString();
            List<Student> findStudentList1 = DataSupport.where("student_number like ?","%"+mess+"%").find(Student.class);
            if (findStudentList1.size() == 0){
                Toast.makeText(context, "没有找到类似学号的学生", Toast.LENGTH_SHORT).show();
            }else {
                for (Student student : findStudentList1) {
                    findStudentList.add(student);
                }
            }
            find_adapter.notifyDataSetChanged();
            //Toast.makeText(context, ""+findStudentList.size(), Toast.LENGTH_SHORT).show();
        }
    

    相关文章

      网友评论

          本文标题:利用Litepal的查询功能做一个搜索栏

          本文链接:https://www.haomeiwen.com/subject/xkkzlctx.html