如何在我的listview中添加搜索功能?

时间:2014-05-24 04:48:16

标签: android listview search filter simplecursoradapter

我正在努力将搜索功能添加到我的图片列表视图中。 你能帮助我吗? 为了做到这一点,我必须添加到适配器和片段?

我的代码如下:

public class Fragment2 extends SherlockFragment {

DatabaseConnector dbConnector;
private Cursor cursor;
private ListView listView; 
private EditText inputSearch; 
ImageCursorAdapter adapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    final View view=inflater.inflate(R.layout.fragment2, container, false);

    dbConnector = new DatabaseConnector(view.getContext());
    dbConnector.open();
    cursor = dbConnector.getPlaces("");

    listView = (ListView)view.findViewById(R.id.lv);
    inputSearch = (EditText)view.findViewById(R.id.inputSearch);

    adapter = new ImageCursorAdapter(view.getContext(), R.layout.listview_each_item, cursor, new String [] { "title", "description", "picture"}, new int[] {  R.id.title, R.id.msg, R.id.pic });
    adapter.setFilterQueryProvider(new FilterQueryProvider() {

        @Override
        public Cursor runQuery(CharSequence arg0) {
            dbConnector = new DatabaseConnector(view.getContext());
            dbConnector.open();
            cursor = dbConnector.getPlaces(arg0.toString());
            dbConnector.close();
            return cursor;
        }
    });
    listView.setTextFilterEnabled(true);
    listView.setAdapter(adapter);
    dbConnector.close();

    inputSearch.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

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

        }

        @Override
        public void afterTextChanged(Editable s) {
            adapter.getFilter().filter(s.toString());
        }
    });  
    return view;
}

}

Asapter:

public class ImageCursorAdapter extends SimpleCursorAdapter {
private Cursor c;

private Context context;

String title;
String desc;
String pic;

@SuppressWarnings("deprecation")

public ImageCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
    super(context, layout, c, from, to);
    this.c = c;
    this.context = context;
}

public View getView(int pos, View inView, ViewGroup parent) {
    View v = inView;

    if (v == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.listview_each_item, null);
    }
    this.c.moveToPosition(pos);      
    title = this.c.getString(this.c.getColumnIndex("title"));
    desc = this.c.getString(this.c.getColumnIndex("description"));
    pic = this.c.getString(this.c.getColumnIndex("picture"));
    ImageView iv = (ImageView) v.findViewById(R.id.icon);
    if(pic != "")
    {
        try {

            String file_ = pic.replace("file:///", "");
            int size = 30;
            File f = new File(file_);
            Bitmap bitmap = decodeFile(f,60,80);
            iv.setImageBitmap(bitmap);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    else
    {
        iv.setImageResource(R.drawable.marker_green72);
    }
    TextView tv_title = (TextView) v.findViewById(R.id.title);
    tv_title.setText(title);
    TextView tv_desc = (TextView) v.findViewById(R.id.msg);
    tv_desc.setText(desc);
    return(v);
}

public static Bitmap decodeSampledBitmapFromPath(String path, int reqWidth,
        int reqHeight) {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);
    options.inSampleSize = calculateInSampleSize(options, reqWidth,
            reqHeight);
    options.inJustDecodeBounds = false;
    Bitmap bmp = BitmapFactory.decodeFile(path, options);
    return bmp;
}


public static int calculateInSampleSize(BitmapFactory.Options options,
        int reqWidth, int reqHeight) {
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;
    if (height > reqHeight || width > reqWidth) {
        if (width > height) {
            inSampleSize = Math.round((float) height / (float) reqHeight);
        } else {
            inSampleSize = Math.round((float) width / (float) reqWidth);
        }
    }
    return inSampleSize;
}

public static Bitmap decodeFile(File f,int WIDTH,int HIGHT){
    try {
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f),null,o);
        final int REQUIRED_WIDTH=WIDTH;
        final int REQUIRED_HIGHT=HIGHT;
        int scale=1;
        while(o.outWidth/scale/2>=REQUIRED_WIDTH && o.outHeight/scale/2>=REQUIRED_HIGHT)
            scale*=2;
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize=scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {}
    return null;
} 

}

我希望得到的答案包括修复我的代码。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

好吧,如果你有一个编辑文本,它更容易,忘记其他答案,我现在经历了同样的事情。

首先,确定编辑文本的编辑文本a;

然后添加一个文本观察器并在ontextchanged中执行过滤。

好吧,我在移动设备上并且太难打字,所以你可以通过avrsaditya@gmail.com与我联系,给我一个机会帮助你,如果满意选择我作为最佳答案,我有很多示例代码

您可能也有兴趣整合到您的操作栏