将搜索功能添加到自定义listView

时间:2017-02-28 11:21:45

标签: android listview search custom-adapter

我有自定义列表视图,我想在列表视图中添加搜索功能。

主要活动

 //other code
                ContentAdapter contentAdapter = new ContentAdapter(context, names, phones);
                listView.setAdapter(contentAdapter);

EditText contactSearch = (EditText) findViewById(R.id.contactSearch);
        contactSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {  
                //contentAdapter.getFilter().filter(s); -- This is not working
                // What I do here to search contacts
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

ContentAdapter

 public class ContentAdapter extends ArrayAdapter<String> {
    private Context context;
    private String[] names;
    private String[] phones;
    public static Dialog dialog;

    ContentAdapter(Context ctx, String[] name, String[] phone){
        super(ctx, R.layout.contact_row,R.id.txtName,name);
        context = ctx;
        names = name;
        phones = phone;
        dialog = new Dialog(context);
    }

    private class ViewHolder{
        TextView name, phone;
        ViewHolder(View view){
            name = (TextView) view.findViewById(R.id.txtName);
            phone = (TextView) view.findViewById(R.id.txtPhoneNumber);
            view.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    Toast.makeText(context, "Item click", Toast.LENGTH_SHORT).show();
                    String phoneNumber = phone.getText().toString();
                    String userName = name.getText().toString();
                    //final Dialog dialog = new Dialog(context);
                    dialog.setContentView(R.layout.custom_dialog);
                    dialog.setTitle(userName);
                    EditText etxtContactNumber = (EditText) dialog.findViewById(R.id.etxtContactNumber);
                    etxtContactNumber.setText(phoneNumber);
                    dialog.show();
                }
            });
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ViewHolder viewHolder = null;
        if(row == null) {
            LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = layoutInflater.inflate(R.layout.contact_row, parent, false);
            viewHolder = new ViewHolder(row);
            row.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) row.getTag();
        }

        viewHolder.name.setText(names[position]);
        viewHolder.phone.setText(phones[position]);

        return row;
    }

}

此代码工作正常,但我想在其中添加搜索功能。我在主要活动中创建编辑文本。当有人输入任何名称时,它将从列表视图中搜索此名称并显示它。

0 个答案:

没有答案