列表视图搜索不显示搜索结果



首先,请耐心等待,因为我只是学习Android的初学者。

我想要的是,当用户从另一个活动添加项目时,详细信息将从 MainActivity 中的列表视图中显示(我对搜索结果使用正则表达式(。当用户尝试搜索项目时,我希望显示搜索结果。

从下面的代码中,仅显示添加的项目,并且不会显示搜索结果。

这是我从MainActivity中获取的代码片段.java

ArrayList<Student> studentArrayList = new ArrayList<>();
ArrayList<Student> findlist = new ArrayList<>();
CustomAdapter adapter, anotheradapter;
private Uri imageUri;
ListView lv;
AlertDialog.Builder show_builder;
AlertDialog dialog;
LinearLayout layout;
ImageView imageView;
TextView stud_lname, stud_fname, stud_course;
AdapterView.AdapterContextMenuInfo info;
//
EditText txtsearch;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lv = (ListView) findViewById(R.id.student_listview);
    txtsearch = (EditText) findViewById(R.id.textsearch);

    anotheradapter = new CustomAdapter(this, findlist);//adapter for finding the list
    adapter = new CustomAdapter(this, studentArrayList);//adapter for displaying the added student
    adapter.notifyDataSetChanged();
    lv.setAdapter(adapter);
    lv.setAdapter(anotheradapter);
    registerForContextMenu(lv);

    lv.setOnItemClickListener(this);
    //
    show_builder = new AlertDialog.Builder(this);
    txtsearch.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            findlist.clear();
            //using regular expressions
            String s1 = s.toString();
            Pattern pattern = Pattern.compile(s1);
            for(int i=0; i<studentArrayList.size(); i++){
                Matcher matcher = pattern.matcher(studentArrayList.get(i).getStudlname());
                if(matcher.find()){
                    findlist.add(studentArrayList.get(i));
                    anotheradapter.notifyDataSetChanged();
                }//end if
            }
            //update the listview
            anotheradapter.notifyDataSetChanged();
        }
        @Override
        public void afterTextChanged(Editable s) {
        }
    });
}

自定义适配器.java

public class CustomAdapter extends BaseAdapter {
    Context context;
    //data container
    ArrayList<Student> list;
    LayoutInflater inflater;
    //contructor

    public CustomAdapter(Context context, ArrayList<Student> list) {
        this.context = context;
        this.list = list;
        this.inflater = LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        return list.size();
    }
    @Override
    public Object getItem(int position) {
        return list.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView == null){
            holder = new ViewHolder();
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.custom_layout, parent, false);
            holder.iv = (ImageView) convertView.findViewById(R.id.imageView);
            holder.lname = (TextView) convertView.findViewById(R.id.textLastname);
            holder.fname= (TextView) convertView.findViewById(R.id.textFirstname);
            holder.course = (TextView) convertView.findViewById(R.id.textCourse);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        //inflate
        holder.iv.setImageURI(list.get(position).getUriImage());
        holder.lname.setText(list.get(position).getStudlname());
        holder.fname.setText(list.get(position).getStudfname());
        holder.course.setText(list.get(position).getStudcourse());
        return convertView;
    }
    //creating a static class
    static class ViewHolder{
        ImageView iv;
        TextView lname, fname,course;
    }
}

学生.java

public class Student {
    Uri uriImage;
    String studlname, studfname, studcourse;
    //constructor
    public Student(Uri uriImage, String studlname, String studfname, String studcourse) {
        super();
        this.uriImage = uriImage;
        this.studlname = studlname;
        this.studfname = studfname;
        this.studcourse = studcourse;
    }
    //getters and setters
    public Uri getUriImage() {
        return uriImage;
    }
    public void setUriImage(Uri uriImage) {
        this.uriImage = uriImage;
    }
    public String getStudlname() {
        return studlname;
    }
    public void setStudlname(String studlname) {
        this.studlname = studlname;
    }
    public String getStudfname() {
        return studfname;
    }
    public void setStudfname(String studfname) {
        this.studfname = studfname;
    }
    public String getStudcourse() {
        return studcourse;
    }
    public void setStudcourse(String studcourse) {
        this.studcourse = studcourse;
    }
}

按如下方式更新代码

ArrayList<Student> studentArrayList = new ArrayList<>();
ArrayList<Student> findlist = new ArrayList<>();
CustomAdapter adapter, anotheradapter;
private Uri imageUri;
ListView lv;
AlertDialog.Builder show_builder;
AlertDialog dialog;
LinearLayout layout;
ImageView imageView;
TextView stud_lname, stud_fname, stud_course;
AdapterView.AdapterContextMenuInfo info;
//
EditText txtsearch;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lv = (ListView) findViewById(R.id.student_listview);
    txtsearch = (EditText) findViewById(R.id.textsearch);

    anotheradapter = new CustomAdapter(this, findlist);//adapter for finding the list
    adapter = new CustomAdapter(this, studentArrayList);//adapter for displaying the added student
    adapter.notifyDataSetChanged();
    lv.setAdapter(adapter);
    lv.setAdapter(anotheradapter);
    registerForContextMenu(lv);

    lv.setOnItemClickListener(this);
    //
    show_builder = new AlertDialog.Builder(this);
    txtsearch.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            findlist.clear();
            //using regular expressions
            String s1 = s.toString();
            Pattern pattern = Pattern.compile(s1);
            for(int i=0; i<studentArrayList.size(); i++){
                Matcher matcher = pattern.matcher(studentArrayList.get(i).getStudlname());
                if(matcher.find()){
                    findlist.add(studentArrayList.get(i));
                    anotheradapter.refreshList(findlist)
                }//end if
            }
            //update the listview
            anotheradapter.refreshList(findlist)
        }
        @Override
        public void afterTextChanged(Editable s) {
        }
    });
}

在适配器中添加刷新列表方法

public class CustomAdapter extends BaseAdapter {
    Context context;
    //data container
    ArrayList<Student> list;
    LayoutInflater inflater;
    //contructor

    public CustomAdapter(Context context, ArrayList<Student> list) {
        this.context = context;
        this.list = list;
        this.inflater = LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        return list.size();
    }
    @Override
    public Object getItem(int position) {
        return list.get(position);
    }
    public void refreshList(ArrayList<Student> list){
        this.list = list;
        notifyDataSetChanged()
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView == null){
            holder = new ViewHolder();
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.custom_layout, parent, false);
            holder.iv = (ImageView) convertView.findViewById(R.id.imageView);
            holder.lname = (TextView) convertView.findViewById(R.id.textLastname);
            holder.fname= (TextView) convertView.findViewById(R.id.textFirstname);
            holder.course = (TextView) convertView.findViewById(R.id.textCourse);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        //inflate
        holder.iv.setImageURI(list.get(position).getUriImage());
        holder.lname.setText(list.get(position).getStudlname());
        holder.fname.setText(list.get(position).getStudfname());
        holder.course.setText(list.get(position).getStudcourse());
        return convertView;
    }
    //creating a static class
    static class ViewHolder{
        ImageView iv;
        TextView lname, fname,course;
    }
}

您需要更新适配器初始化,如下所示。

onCreate(( 方法中的更新:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lv = (ListView) findViewById(R.id.student_listview);
    txtsearch = (EditText) findViewById(R.id.textsearch);
    // initialize the findlist to show all student list by default
    findlist.addAll(studentArrayList);
    anotheradapter = new CustomAdapter(this, findlist);
    // ----- Remove these lines - as you don't need multiple adapters
    //adapter = new CustomAdapter(this, studentArrayList);//adapter for displaying the added student
    //adapter.notifyDataSetChanged();
    //lv.setAdapter(adapter);
    lv.setAdapter(anotheradapter);
    registerForContextMenu(lv);

    lv.setOnItemClickListener(this);
    show_builder = new AlertDialog.Builder(this);
    txtsearch.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            findlist.clear();
            //using regular expressions
            String s1 = s.toString();
            Pattern pattern = Pattern.compile(s1);
            for(int i=0; i<studentArrayList.size(); i++){
                Matcher matcher = pattern.matcher(studentArrayList.get(i).getStudlname());
                if(matcher.find()){
                    findlist.add(studentArrayList.get(i));
                    // Remove the below line as you don't need to update the list multipletimes
                    //anotheradapter.notifyDataSetChanged();
                }
            }
            // Add these lines to show the list of all student when the searchbox is empty. This will reset the findList to initial state.
            if (txtsearch.getText().length() == 0 && findlist.isEmpty()) {
                findlist.addAll(studentArrayList);
            }
            anotheradapter.notifyDataSetChanged();
        }
        @Override
        public void afterTextChanged(Editable s) {
        }
    });
}

最新更新