TextWatcher,它使用两个自定义ArrayAdapter更改两个ListView

时间:2016-08-28 17:56:12

标签: android listview android-arrayadapter android-textwatcher

我有TextWatcher的代码,它使用2个不同的ArrayAdapter更改了2个不同的listView。

private TextWatcher createAndReturnTextWatcher()
{
    final List<String> list = dbExtractor.getDataForSearchAdapters();
    adapterForSearch = new MyAdapter(this, R.layout.list_item2, R.id.product_name,
            list);
    adapter = new ArrayAdapter(this, R.layout.list_item2, R.id.product_name,
        dbExtractor.getDataForSearchAdapters());




    TextWatcher watcher1;

    watcher1 = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {

            if (cs.length() == 0) {

                lv.setAdapter(null);
                lv2.setAdapter(null);

                dc.clickedOnce = true;
                dc.decrement();
                dc.checkDimCounter();
            } else {

                lv.setAdapter(adapterForSearch);
                adapterForSearch.getFilter().filter(cs);

                lv2.setAdapter(adapter);
                adapter.getFilter().filter(cs);

                if (dc.clickedOnce) {
                    dc.increment();
                    dc.checkDimCounter();
                    dc.clickedOnce = false;
                }

            }

        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                      int arg3) {
        }

        @Override
        public void afterTextChanged(Editable arg0) {

        }
    };

    return watcher1;
}

问题是:

当我使用一个标准的ArrayAdapter类和一个自定义的ArrayAdapter类时,为什么这个工作正常,但是如果我尝试使用两个自定义ArrayAdapter它就不起作用。这只是改变了两个listView,就像它们都使用一个自定义适配器一样?

提前致谢!

MY CUSTOM ADAPTERS

第一

公共类MyTurkishAdapter扩展了ArrayAdapter {

protected Filter myTurkFilter;
protected List<String> mObjects;
protected List<String> mOriginalValues;

public MyTurkishAdapter(Context context, int resource, int textViewResourceId, List objects) {
    super(context, resource, textViewResourceId, objects);
    myTurkFilter = new MyTurkFilter();
    mObjects = objects;
}

@Override
public Filter getFilter() {
    return myTurkFilter;
}

private class MyTurkFilter extends Filter
{

    @Override
    protected FilterResults performFiltering(CharSequence prefix) {


        FilterResults results = new FilterResults();


        if (mOriginalValues == null) {
            {
                mOriginalValues = new ArrayList<String>(mObjects);
            }
        }

        if (prefix == null || prefix.length() == 0) {
            ArrayList<String> list;
            {
                list = new ArrayList<>(mOriginalValues);
            }
            results.values = list;
            results.count = list.size();
        } else {
            java.lang.String prefixString = prefix.toString().toLowerCase();

            ArrayList<String> values;
            {
                values = new ArrayList<>(mOriginalValues);
            }

            final int count = values.size();
            final ArrayList<String> newValues = new ArrayList<String>();

            for (int i = 0; i < count; i++) {

                final String value = values.get(i);
                final java.lang.String valueText = value.toLowerCase();

                if (valueText.startsWith(prefixString)) {
                    newValues.add(reverse(value));
                }

            }

            results.values = newValues;
            results.count = newValues.size();
        }

        return results;
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {

        mObjects.clear();
        mObjects.addAll((List<String>) results.values);

        if (results.count > 0) {
            notifyDataSetChanged();
        } else {
            notifyDataSetInvalidated();
        }

    }
}

private java.lang.String reverse(java.lang.String couples)
{
    java.lang.String[] arr = couples.split(" — ");
    return arr[1]+ " — " + arr[0];

}

}

第二

public class MyAdapter extends ArrayAdapter {


protected Filter filter;
protected List<String> mObjects;
protected List<String> mOriginalValues;


public MyAdapter(Context context, int resource, int textViewResourceId, List<String> objects) {
    super(context, resource, textViewResourceId,  objects);
    filter = new MyFilter();
    mObjects = objects;
}

@Override
public Filter getFilter() {
    return filter;
}

private class MyFilter extends Filter
{

    @Override
    protected FilterResults performFiltering(CharSequence prefix) {

        FilterResults results = new FilterResults();


        if (mOriginalValues == null) {
            {
                mOriginalValues = new ArrayList<String>(mObjects);
            }
        }

        if (prefix == null || prefix.length() == 0) {
            ArrayList<String> list;
            {
                list = new ArrayList<String>(mOriginalValues);
            }
            results.values = list;
            results.count = list.size();
        } else {
            java.lang.String prefixString = prefix.toString().toLowerCase();

            ArrayList<String> values;
            {
                values = new ArrayList<String>(mOriginalValues);
            }

            final int count = values.size();
            final ArrayList<String> newValues = new ArrayList<String>();
            //DictionaryActivity2.turkish.clear();

            for (int i = 0; i < count; i++) {

                final String value = values.get(i);
                final java.lang.String valueText = value.toString().toLowerCase();

                if (valueText.startsWith(prefixString)) {
                    newValues.add(value);
                }

           }

            results.values = newValues;
            results.count = newValues.size();
        }

        return results;

    }







    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        mObjects.clear();
        mObjects.addAll((List<String>) results.values);

        if (results.count > 0) {
            notifyDataSetChanged();
        } else {
            notifyDataSetInvalidated();
        }

    }
}

}

P.S。他们只有一个线差异。我知道模板方法模式,并在我找到这个问题的答案之后立即实现:)

0 个答案:

没有答案