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

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

本文介绍了将搜索功能添加到自定义列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I create custom listView for contacts and EditText for search contents.Here is search code.

主要活动

    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

contentAdapter is static variable which is define in another class which is in different file this class is extend from AysnTask

BackgroundWorker

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

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

I make it static variable because I want to access it from mainActivity class as define above.
There is no error But search functionality does not work properly. I try to search contact but it can't search. I don't know what the problem is. There is no error but searching is not working fine.

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

}

推荐答案

在适配器中使用此代码.

Use this code in adapter.

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

这篇关于将搜索功能添加到自定义列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 10:39