将搜索功能添加到自定义列表视图

时间:2017-02-28 08:18:03

标签: android listview search

我为联系人创建自定义listView,为搜索内容创建EditText。 这是搜索代码。

主要活动

    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是静态变量,它在另一个类中定义,该类位于不同的文件中,此类从AysnTask扩展

BackgroundWorker的

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

我将它设为静态变量,因为我想从mainActivity类访问它,如上所述 没有错误但搜索功能无法正常工作。我尝试搜索联系人,但无法搜索。我不知道问题是什么。没有错误,但搜索工作不正常。

更新 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;
}

}

1 个答案:

答案 0 :(得分:0)

在适配器中使用此代码。

public void filter(String charText) {
        charText = charText.toLowerCase(Locale.getDefault());
        names.clear();
        if (charText.length() == 0) {
            names.addAll(arraylist);
        } else {
            for (Names wp : arraylist) {
                if (wp.toLowerCase(Locale.getDefault())
                        .contains(charText)) {
                    names.add(wp);
                }
            }
        }
        notifyDataSetChanged();
    }