Listview搜索不显示搜索结果

时间:2019-07-17 09:21:26

标签: android regex listview

首先,请忍受我,因为我只是学习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) {

        }
    });
}

CustomAdapter.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;
    }
}

Student.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;
    }
}

2 个答案:

答案 0 :(得分:0)

如下更新代码

UIKitCore.h

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

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) {

        }
    });
}

答案 1 :(得分:0)

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

使用 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) {

        }
    });
}