如何过滤Android中的自定义ListAdapter?

我搜查了很多,但没有任何解决方案为我工作。我正在创建一个具有imageview和textview的自定义arrayadapter。我保留一个edittext,我想添加文本,我希望得到这些给定的文本过滤的列表,当我使用字符串类型的数组适配器这很好,但它不工作在我的自定义适配器,这里是我的代码:

public class CurrenciesFrom extends Activity implements OnItemClickListener {

    ListView lvCurrencies;
    String[] countryCode = { "BDT Bangladesh","BBF bafa" ,"USD America", "UMK Umaka","KRW Korea",
            "INR India" };
    int[] flag = { R.drawable.bdt, R.drawable.usd, R.drawable.aud,
            R.drawable.ic_launcher ,R.drawable.usd, R.drawable.aud};
    ArrayAdapter adapter;
    List l;
    EditText etsearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.currencies);
        initializer();
        lvCurrencies.setAdapter(adapter);
        lvCurrencies.setOnItemClickListener(this);
        etsearch.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
               //TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
               //TODO Auto-generated method stub

            }

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


                CurrenciesFrom.this.adapter.getFilter().filter(s.toString());
            }
        });
    }

    private List getModel() {
        List list = new ArrayList();
        for (int i = 0; i < countryCode.length; i++) {

            list.add(get(countryCode[i], flag[i]));
        }

        list.get(1).setSelected(true);//select one item by default
        return list;
    }

    private Currency get(String s, int place) {
        return new Currency(s, place);
    }

    private void initializer() {

        lvCurrencies = (ListView) findViewById(R.id.lvCurrencies);
        etsearch=(EditText)findViewById(R.id.etSearch);
        lvCurrencies.setTextFilterEnabled(true);
       //countryCode = getResources().getStringArray(R.array.Currencies);
        l = getModel();

        Collections.sort(l, new Comparator() {
            @Override
            public int compare(Currency c1, Currency c2) {
                return c1.getName().compareToIgnoreCase(c2.getName());
            }
        });

        adapter = new Customarrayadapter(this, l);
    }

我的自定义适配器类是:

package com.powergroupbd.yahoo;

import java.util.List;

import android.app.Activity;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

public class Customarrayadapter extends ArrayAdapter {

    private final List list;
    private final Activity context;

    public Customarrayadapter(Activity context, List list) {
        super(context, R.layout.showlist, list);
        this.context = context;
        this.list = list;
    }

    static class ViewHolder {
        protected TextView text;
        protected ImageView sub;
        protected CheckBox checkbox;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
            view = inflator.inflate(R.layout.showlist, null);
            final ViewHolder viewHolder = new ViewHolder();
            viewHolder.text = (TextView) view.findViewById(R.id.label);
            viewHolder.text.setTextColor(Color.WHITE);
            viewHolder.sub = (ImageView) view.findViewById(R.id.sub);

            viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
            viewHolder.checkbox
                    .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView,
                                boolean isChecked) {
                            Currency element = (Currency) viewHolder.checkbox.getTag();
                            element.setSelected(buttonView.isChecked());
                            System.out.println("Checked : " + element.getName());
                        }
                    });
            view.setTag(viewHolder);
            viewHolder.checkbox.setTag(list.get(position));
        } else {
            view = convertView;
            ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
        }
        ViewHolder holder = (ViewHolder) view.getTag();
        holder.text.setText(list.get(position).getName());
        holder.sub.setImageResource(list.get(position).getPlace());
        holder.checkbox.setChecked(list.get(position).isSelected());
        return view;
    }
}

请帮帮我 :(

1
额外 编辑
意见: 1

2 答案

使用此代码来过滤文本

public void onTextChanged(CharSequence s, int start,
int before, int count)
{

    textlength = edittext.getText().length();
    text_sort.clear();
    image_sort.clear();

    for (int i = 0; i < text.length; i++)
    {
        if (textlength <= text[i].length())
        {
            if (edittext.getText().toString().
                    equalsIgnoreCase((String) text[i].subSequence(0, textlength)))
            {
                text_sort.add(text[i]);
                image_sort.add(image[i]);
            }
        }
    }

    listview.setAdapter(new MyCustomAdapter(text_sort, image_sort));

    }
}

for more, refer this site it may help you http://android-helper.blogspot.in/2011/07/android-search-in-custom-listview.html

1
额外
@Reyjohn:text_sort和image_sort分别是过滤的文本和图像,更多信息请参阅链接man
额外 作者 Aerrow,
什么是text_sort,image_sort和文本在这里?请解释
额外 作者 Reyjohn,
你链接thaseems是有帮助的,它是我的错,我不能用我的方式
额外 作者 Reyjohn,
我的customadapter类是在另一个java文件中,所以我不能链接一些变量,你确实喜欢两个类中的text.size,所以我不能使用这个
额外 作者 Reyjohn,

被接受的答案很具误导性,并且忽略了实际问题。 OP不正确地继承 ArrayAdapter 。您不应该跟踪自己的数据列表,而是通过适当的适配器方法访问该数据。例如, getItem(position)ArrayAdapter 不保证您传递给它的 List 实例保持不变。

事实上,在过滤操作期间,它将数据复制到一个新的 ArrayList 实例。这意味着您在子类中使用的列表不再引用 ArrayAdapter 中的同一个列表。由于您正在访问 getView()方法中的自己的列表,而不是从 getItem()拉,任何过滤更改都将被忽略。事实上,在这一点上,你也打破了从适配器添加或删除的能力。

子类化 ArrayAdapter 时最重要的规则。永远不要保留外部数据列表。请始终引用 ArrayAdapter 来代替。

0
额外