0

文字列や画像 URL などのさまざまなアイテムを保持するモデル クラスがあります。

これにフィルターを実装しましたが、残念ながら、検索するたびにすべてのアイテムが消えてしまいます。フィルタの実装方法を教えてください。

これがソースコードです。

package Dummy.list.filter;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.ListView;

public class DummyListFilterActivity extends Activity {
    /** Called when the activity is first created. */


    myadapter adapter;
    EditText filterText;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        ModelClass[] str = new ModelClass[]{new ModelClass("cell",R.drawable.ic_launcher,1), 
                new ModelClass("Bus",R.drawable.ic_launcher,2),
                new ModelClass("Car",R.drawable.ic_launcher,3),
                new ModelClass("Van",R.drawable.ic_launcher,4),
                new ModelClass("Lorry",R.drawable.ic_launcher,5)};

        ListView list = (ListView)findViewById(R.id.Service_name_list);
        filterText = (EditText)findViewById(R.id.txtUserName);
        filterText.addTextChangedListener(filterTextWatcher);

        list.setTextFilterEnabled(true);

        adapter = new myadapter(this, 0, str); 

       //ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,str);

        list.setAdapter(adapter);

    }

    private TextWatcher filterTextWatcher = new TextWatcher() {

        public void afterTextChanged(Editable s) {
        }

        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            adapter.getFilter().filter(s.toString());
        }

    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        filterText.removeTextChangedListener(filterTextWatcher);
    }

}

これが私の Adapter クラスの外観です

package Dummy.list.filter;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;


    public class myadapter extends ArrayAdapter<ModelClass>
    {

        //private final List<String> contactNumbers;
            //private final String[] contactDetails;
            private final ModelClass[] contactDetails;
            private final Activity AdapterContext;


        public myadapter(Context context, int textViewResourceId,ModelClass[] objects) 
        {
            super(context, textViewResourceId, objects);
            // TODO Auto-generated constructor stub
            contactDetails = objects;
            AdapterContext = (Activity) context;
        }

        static class ViewHolder {
            protected TextView ContactNumbers;
            protected ImageView ContactImage;
        }

        public View getView(int position, View convertView, ViewGroup parent)
        {
            View view = null;
            if(convertView == null)
            {
                LayoutInflater inflator = AdapterContext.getLayoutInflater();
                view = inflator.inflate(R.layout.item, null);

                final ViewHolder holder = new ViewHolder();
                holder.ContactImage = (ImageView) view.findViewById(R.id.image);
                holder.ContactNumbers = (TextView) view.findViewById(R.id.text);    
                view.setTag(holder);
            }
            else
            {
                view = convertView;
            }

            ViewHolder holder = (ViewHolder) view.getTag();
            holder.ContactNumbers.setText(contactDetails[position].getServiceName());
            holder.ContactImage.setImageResource(contactDetails[position].getServiceImage());
            view.setTag(holder);
            return view;

        }

    }
4

1 に答える 1

0

アダプタはを実装する必要がありますFilterablemyadapterクラスを次のクラスに置き換えます。

public class myadapter extends ArrayAdapter<ModelClass> implements Filterable
{

    //private final List<String> contactNumbers;
    //private final String[] contactDetails;
    private ModelClass[] contactDetails;
    private ModelClass[] original;
    private final Activity AdapterContext;
    private Filter filter;
    private Object lock = new Object();


    public myadapter(Context context, int textViewResourceId,ModelClass[] objects) 
    {
        super(context, textViewResourceId, objects);
        // TODO Auto-generated constructor stub
        contactDetails = original = objects;
        AdapterContext = (Activity) context;
    }

    static class ViewHolder {
        protected TextView ContactNumbers;
        protected ImageView ContactImage;
    }

    public View getView(int position, View convertView, ViewGroup parent)
    {
        View view = null;
        if(convertView == null)
        {
            LayoutInflater inflator = AdapterContext.getLayoutInflater();
            view = inflator.inflate(R.layout.item, null);

            final ViewHolder holder = new ViewHolder();
            holder.ContactImage = (ImageView) view.findViewById(R.id.image);
            holder.ContactNumbers = (TextView) view.findViewById(R.id.text);    
            view.setTag(holder);
        }
        else
        {
            view = convertView;
        }

        ViewHolder holder = (ViewHolder) view.getTag();
        holder.ContactNumbers.setText(contactDetails[position].getServiceName());
        holder.ContactImage.setImageResource(contactDetails[position].getServiceImage());
        view.setTag(holder);
        return view;

    }

    @Override
    public Filter getFilter() {
    // TODO Auto-generated method stub
    if (filter == null) {
        filter = new MyFilter();
    }
    return filter;
    }

    private class MyFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
        // TODO Auto-generated method stub
        String query = constraint.toString().toLowerCase();
        FilterResults results = new FilterResults();
        ModelClass[] temp;
        synchronized (lock) {
                temp = Arrays.copyOf(contactDetails, contactDetails.length);
        }
        if (query == null || query.length() == 0) {
            synchronized (lock) {
            results.values = original;
                results.count = original.length;
            }
        } else {
            ModelClass[] filtered = new ModelClass[original.length];
            int size = 0;
            for (int i = 0; i < temp.length; i++) {
            ModelClass content = temp[i];
            if(content.getServiceName().equalsIgnoreCase(query)) {
                filtered[size++] = content;
            }
        }
        filtered = Arrays.copyOf(filtered, size);
        results.values = filtered;
        results.count = filtered.length;
        }
        return results;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint,
            FilterResults results) {
        // TODO Auto-generated method stub
        contactDetails = (ModelClass[]) results.values;
        notifyDataSetChanged();
    }

    }
}

私はそれをテストしていません。しかし、私はそれがうまくいくことを願っています。

于 2012-04-29T21:52:12.747 に答える