用图像过滤列表视图

时间:2012-05-10 09:14:00

标签: android listview

我已经创建了自己的自定义ListView适配器,所以我试图过滤一些项目,如何我能做到这一点。在标准适配器很容易,所以我尝试了这个,但它不起作用:

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class lista_imagini extends ListActivity{

    String[] items={"lorem", "ipsum", "dolor", "sit", "amet",
            "consectetuer", "adipiscing", "elit", "morbi", "vel",
            "ligula", "vitae", "arcu", "aliquet", "mollis",
            "etiam", "vel", "erat", "placerat", "ante",
            "porttitor", "sodales", "pellentesque", "augue",
            "purus"};

    ImageAndTextAdapter adaptor = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.clist);

        final ListView ls = getListView();
        if (ls == null) Log.d("DHA", "Cacat");
        ls.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                Toast.makeText(getApplicationContext(), ls.getItemAtPosition(arg2).toString(), Toast.LENGTH_LONG).show();

            }

        });

        adaptor = new ImageAndTextAdapter(getApplicationContext(),R.layout.customlist,items);
        setListAdapter(adaptor);


    }

    public class ImageAndTextAdapter extends ArrayAdapter<String> {

        private LayoutInflater mInflater;

        private String[] mStrings;

        private int mViewResourceId;

        public ImageAndTextAdapter(Context ctx, int viewResourceId,
                String[] strings) {
            super(ctx, viewResourceId, strings);

            mInflater = (LayoutInflater)ctx.getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE);

            mStrings = strings;

            mViewResourceId = viewResourceId;
        }

        @Override
        public int getCount() {
            return mStrings.length;
        }

        @Override
        public String getItem(int position) {
            return mStrings[position];
        }

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = mInflater.inflate(mViewResourceId, null);

            ImageView iv = (ImageView)convertView.findViewById(R.id.icon2);
            iv.setImageResource(R.drawable.fail);

            TextView tv = (TextView)convertView.findViewById(R.id.label2);
            tv.setText(mStrings[position]);

            return convertView;
        }
    }

    public void testnow(View V)
    {
        items[3] = "Heeee";
        adaptor.getFilter().filter("ipsum");
        adaptor.notifyDataSetChanged();
        getListView().refreshDrawableState();

    }

}

1 个答案:

答案 0 :(得分:1)

您必须扩展Filter类。那么你将获得更改getCount和getItem。 check this answer for an example