从Android中的扩展ArrayAdapter搜索项目

时间:2017-03-05 05:25:01

标签: android listview search android-arrayadapter

我正在使用ArrayAdapter来显示ListView中的列表项。每件事情都运行良好,但知道我想从此列表中搜索项目。实际上这些是包含姓名和电话的联系人列表。我只想根据名字进行搜索。

我为搜索编写了一些代码,其中没有错误,也没有任何运行时错误,但搜索功能无效。

MainActivity

        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);
            }

            @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, List<String> rows){
    super(ctx, R.layout.contact_row,R.id.txtName, rows);
    context = ctx;
    dialog = new Dialog(context);
}*/
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;
}

public Filter getFilter() {
    return new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            Filter.FilterResults filterResults = new FilterResults();
            ArrayList<String> values = new ArrayList<>();
            for (int i=0; i < getCount(); i++) {
                if (names[i].contains(constraint)) {
                    values.add(names[i]);
                }
                filterResults.values = values;
                filterResults.count = values.size();
            }
            return filterResults;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence contraint, FilterResults results) {
            if (results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }
    };
}

}

在代码的末尾,您可以看到我用于搜索的搜索功能。我是android的新手我不知道我在那里缺少什么。

1 个答案:

答案 0 :(得分:0)

EditText contactSearch = (EditText) findViewById(R.id.contactSearch);
    contactSearch.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) {
           contentAdapter.getFilter().filter(s);
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

试试这个