在适配器中使用addTextChangedListener

时间:2015-03-26 13:25:46

标签: android android-listview android-adapter

我正在使用列表适配器将数据加载到列表视图中。我在我的适配器中使用了addTextChangedListener,我遇到了2个错误。任何人都可以帮我纠正这2个错误。 我在ActivitySearchResults中使用了这个代码然后它工作但我想在适配器中使用这个代码。

已更新

java代码

        public class CustomListSearch extends BaseAdapter {

    private Context context;
    private final List<String> descriptions;
    private final List<String> menudescriptions;
    ArrayAdapter<String> adapter;
    EditText inputSearch;

    public CustomListSearch(Context c, List<String> descriptions,
            List<String> menudescriptions) {
        this.context = c;
        this.descriptions = descriptions;
        this.menudescriptions = menudescriptions;
    }

    @Override
    public int getCount() {
        return descriptions.size();
    }

    @Override
    public Object getItem(int position) {
        return descriptions.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private class ViewHolder {
        private EditText inputSearch;
        private TextView tvMenudescriptions;
        private TextView tvDescriptions;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();

            convertView = LayoutInflater.from(context).inflate(
                    R.layout.search_list_item, parent, false);

            holder.tvDescriptions = (TextView) convertView
                    .findViewById(R.id.product_name);
            holder.tvMenudescriptions = (TextView) convertView
                    .findViewById(R.id.product_description);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tvDescriptions.setText(descriptions.get(position));
        holder.tvMenudescriptions.setText(menudescriptions.get(position));

        holder.inputSearch = (EditText) convertView
                .findViewById(R.id.inputSearch);

        addTextChangedListener(new TextWatcher() { // null point
                                                    // exception
            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2,
                    int arg3) {
                // When user changed the Text
                adapter.getFilter().filter(cs);

            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1,
                    int arg2, int arg3) {
                // TODO Auto-generated method stub
            }

            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub
            }
        });

        return convertView;

    }

    private void addTextChangedListener(TextWatcher textWatcher) {
        // TODO Auto-generated method stub

    }

}

1 个答案:

答案 0 :(得分:0)

将其粘贴到if(convertView == null)

inputSearch = (EditText) convertView.findViewById(R.id.inputSearch);

在getView参数中生成convertView final

@Override
public View getView(final int position,final View convertView, ViewGroup parent)