在Android中自定义的ListView搜索功能 [英] Search functionality in Android Custom ListView

查看:111
本文介绍了在Android中自定义的ListView搜索功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的CursorAdapter从数据库中显示的数据。现在,我想添加搜索功能,自定义列表视图。所以,我想这一点。但是,这是行不通的。

I have CursorAdapter to show data from database. Now, I want add search functionality to custom listview. So, I tried with this. but, this is not working.

 searchOption.addTextChangedListener(new TextWatcher() {

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

                AbstractActivity.this.cursorAdapter.getFilter().filter(cs);
            }

            @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

            }
        });

下面是适配器code

Here is the Adapter Code

public class AbstractCursorAdapter extends CursorAdapter {
    Cursor cursorOne;

    String getName;

    @SuppressWarnings("deprecation")
    public AbstractCursorAdapter(Context context, Cursor c) {
        super(context, c);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        // TODO Auto-generated method stub
        TextView title = (TextView)view.findViewById(R.id.abTitle);

        title.setText(cursor.getString(cursor.getColumnIndexOrThrow("TITLE")));
        ;

        TextView topic = (TextView)view.findViewById(R.id.abTopic);

        topic.setText(cursor.getString(cursor.getColumnIndexOrThrow("TOPIC")));

        TextView type = (TextView)view.findViewById(R.id.abType);

        type.setText(cursor.getString(cursor.getColumnIndexOrThrow("TYPE")));

        String value = cursor.getString(cursor.getColumnIndexOrThrow("_id"));

        String sqlQuery = "select abstracts_item._id AS ID,abstract_author.NAME AS NAME from abstracts_item,abstract_author,authors_abstract where abstracts_item._id = authors_abstract.abstractsitem_id and abstract_author._id = authors_abstract.abstractauthor_id and ID = "
                + value;


        cursorOne = DatabaseHelper.database.rawQuery(sqlQuery, null);

        if (cursorOne != null) {
            cursorOne.moveToFirst();
            do {

                if (cursorOne.getPosition() == 0) {

                    getName = cursorOne.getString(cursorOne.getColumnIndexOrThrow("NAME"));

                } else {

                    getName = getName + ","
                            + cursorOne.getString(cursorOne.getColumnIndexOrThrow("NAME"));

                }

            } while (cursorOne.moveToNext());
        }

        TextView authorNames = (TextView)view.findViewById(R.id.SubTitle);

        String formatterNames = "";

        String[] namesArray = getName.split(",");

       // Log.e("Length", String.valueOf(namesArray.length));

        if (namesArray.length > 1) {
            for (int i = 0; i < namesArray.length; i++) {

                if (i == namesArray.length - 1) {
                    formatterNames = formatterNames + " & " + namesArray[i];
                }else if (i == 0){
                    formatterNames = formatterNames +namesArray[i];
                }
                else {
                    formatterNames = formatterNames + " , " +namesArray[i];
                }
            }


            /*
             * Get Width
             */

            WindowManager WinMgr = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
            int displayWidth = WinMgr.getDefaultDisplay().getWidth();

            Paint paint = new Paint();
            Rect bounds = new Rect();

            int text_height = 0;
            int text_width = 0;

            //paint.setTypeface(Typeface.DEFAULT);

            paint.getTextBounds(formatterNames, 0, formatterNames.length(), bounds);

            text_height =  bounds.height();
            text_width =  bounds.width();


            Log.e("SIZE", "Text =" + String.valueOf(text_width) + "------" +"Layout = " + String.valueOf(displayWidth));

            if(text_width > displayWidth){

                String output= formatterNames.split(",")[0] + " et al. " ;
                authorNames.setText(output);

             }else{
                 authorNames.setText(formatterNames.replaceAll("((?:^|[^A-Z.])[A-Z])[a-z]*\\s(?=[A-Z])",
                         "$1.")); 
             }


        } else {

            authorNames
            .setText(getName.replaceAll("((?:^|[^A-Z.])[A-Z])[a-z]*\\s(?=[A-Z])", "$1."));
        }
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup viewgroup) {
        // TODO Auto-generated method stub
        LayoutInflater inflater = LayoutInflater.from(viewgroup.getContext());
        View returnView = inflater.inflate(R.layout.abstract_content, viewgroup, false);
        return returnView;
    }



}

我会很高兴,如果你们帮忙找出什么在我的code中的问题或如何我可以添加搜索功能,自定义列表视图。

I will be glad if you guys help to find out what's the problem in my code or how can i add search functionality to a custom listview.

推荐答案

尝试使用它

adapter.setFilterQueryProvider(new FilterQueryProvider() {
    public Cursor runQuery(CharSequence constraint) {
       // fetch all query data by input String
       // fetchDataByName take input String when search
       return dbHelper.fetchDataByName(constraint.toString());
    }
});


public void onTextChanged(CharSequence s, int start, int before,
            int count) {
     AbstractActivity.this.cursorAdapter.getFilter().filter(cs);       
}

这篇关于在Android中自定义的ListView搜索功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆