【问题标题】:How to add search functionality to my listview?如何将搜索功能添加到我的列表视图?
【发布时间】:2014-05-24 04:48:16
【问题描述】:

我正在尝试将搜索功能添加到我的图像列表视图,但没有成功。 你能帮助我吗? 为了做到这一点,我必须向适配器和片段添加什么?

我的代码如下:

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;
}

}

尽快:

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;
} 

}

我将不胜感激包括修复我的代码的答案。 提前谢谢你。

【问题讨论】:

  • 您不需要单独的 EditText 视图和 addTextChangedListener,也不需要自定义适配器
  • 您只需要setTextFilterEnabled(true),只需打开键盘开始输入
  • 很遗憾您的建议没有解决צט问题ץ
  • 为什么不呢?它是列表过滤的内置机制
  • 我不知道为什么不,这就是我寻求帮助的原因。我需要对代码进行哪些更改才能使其正常工作?你能把修改后的代码发给我吗?

标签: android listview search filter simplecursoradapter


【解决方案1】:

好吧,如果你有一个编辑文本就容易多了,忘记其他答案,我现在经历的和你经历的一样多。

首先,识别前编辑文本a的编辑文本;

然后给它添加一个text watcher,在ontextchanged中进行过滤。

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

您可能也有兴趣集成到您的操作栏

【讨论】:

  • 我给你发了我的电子邮件地址。
猜你喜欢
  • 1970-01-01
  • 2017-07-19
  • 2022-01-02
  • 1970-01-01
  • 1970-01-01
  • 2017-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多