Android 简单光标适配器

发布于 2024-11-03 15:00:48 字数 3169 浏览 5 评论 0原文

您好,我的列表视图使用光标适配器,我的问题是当我将应用程序置于后台时,重新调整时我的屏幕为空,在 onresume 中我已打开数据库并创建光标,但仍然存在问题,我该如何解决我的问题,请帮助我。

searchCursor= dbReaderContact.rawQuery(query, null);
startManagingCursor(searchCursor);
String[] from=new String[] {ALDbAdapter.TITLE,DbAdapter.CATID,DbAdapter.LTID,DbAdapter.RK,DbAdapter.SUBTITLE};
                        int [] to=new int[] {R.layout.ctllist_item};
catSearchAdapter=new CategorySearchAdapter(context, R.layout.ctllist_item, searchCursor, from, to);

//////////////Adapter calss

public class CategorySearchAdapter extends SimpleCursorAdapter implements Filterable{

    private Context context;
    private int layout;
    private ALDbAdapter dbadapter;

    /**
     * @param context
     * @param layout
     * @param c
     * @param from
     * @param to
     */
    public CategorySearchAdapter(Context context, int layout, Cursor c, String[] from,  int[] to) {
        super(context, layout, c, from, to);
        this.layout=layout;
        this.context=context;

        dbadapter=new ALDbAdapter(context);
        try {
            dbadapter.openAngiesListDb();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

     @Override
        public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
            if (getFilterQueryProvider() != null) { return getFilterQueryProvider().runQuery(constraint); }

            StringBuilder buffer = null;
            String[] args = null;
            if (constraint != null) {
                buffer = new StringBuilder();
                buffer.append("UPPER(");
                buffer.append("name");
                buffer.append(") GLOB ?");
                args = new String[] { constraint.toString().toUpperCase() + "*" };
            }
            Cursor c=null;
           // c=dbadapter.getPartialNamesSearch(constraint.toString());
            return c;
        }
     @Override
        public void bindView(View v, Context context, Cursor c) {

            TextView subTitle=(TextView)v.findViewById(R.id.ctlName);
            TextView title=(TextView)v.findViewById(R.id.cltAssocationName);
            ImageView imgIcon=(ImageView)v.findViewById(R.id.ctlLogo);

            int subTitInd=c.getColumnIndex(ALDbAdapter.SUBTITLE);
            int titInd=c.getColumnIndex(ALDbAdapter.TITLE);
            int ltId=c.getColumnIndex(ALDbAdapter.LTID);
            int ltype=c.getInt(ltId);
            if(!c.getString(subTitInd).equalsIgnoreCase("")){
                subTitle.setText(c.getString(subTitInd));
                title.setText(c.getString(titInd));
            }else{
                subTitle.setText(c.getString(titInd));
            }
            if(ltype==1){
                imgIcon.setImageResource(R.drawable.logo1);
            }else if(ltype==2){
                imgIcon.setImageResource(R.drawable.logo2); 
            }else{
                imgIcon.setImageResource(R.drawable.logo3); 
            }
        }

}
//On post i closed the cursor
///On Resume

searchCursor= dbReaderContact.rawQuery(query, null);
            startManagingCursor(searchCursor);

hi am using cursor adapter for my listview, my problems is when i put my application in background ,on retuning my screen is empty, in onresume i have open the database and creating cursor still i have the problem, how can i solve my problem ,pls help me .

searchCursor= dbReaderContact.rawQuery(query, null);
startManagingCursor(searchCursor);
String[] from=new String[] {ALDbAdapter.TITLE,DbAdapter.CATID,DbAdapter.LTID,DbAdapter.RK,DbAdapter.SUBTITLE};
                        int [] to=new int[] {R.layout.ctllist_item};
catSearchAdapter=new CategorySearchAdapter(context, R.layout.ctllist_item, searchCursor, from, to);

//////////////Adapter calss

public class CategorySearchAdapter extends SimpleCursorAdapter implements Filterable{

    private Context context;
    private int layout;
    private ALDbAdapter dbadapter;

    /**
     * @param context
     * @param layout
     * @param c
     * @param from
     * @param to
     */
    public CategorySearchAdapter(Context context, int layout, Cursor c, String[] from,  int[] to) {
        super(context, layout, c, from, to);
        this.layout=layout;
        this.context=context;

        dbadapter=new ALDbAdapter(context);
        try {
            dbadapter.openAngiesListDb();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

     @Override
        public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
            if (getFilterQueryProvider() != null) { return getFilterQueryProvider().runQuery(constraint); }

            StringBuilder buffer = null;
            String[] args = null;
            if (constraint != null) {
                buffer = new StringBuilder();
                buffer.append("UPPER(");
                buffer.append("name");
                buffer.append(") GLOB ?");
                args = new String[] { constraint.toString().toUpperCase() + "*" };
            }
            Cursor c=null;
           // c=dbadapter.getPartialNamesSearch(constraint.toString());
            return c;
        }
     @Override
        public void bindView(View v, Context context, Cursor c) {

            TextView subTitle=(TextView)v.findViewById(R.id.ctlName);
            TextView title=(TextView)v.findViewById(R.id.cltAssocationName);
            ImageView imgIcon=(ImageView)v.findViewById(R.id.ctlLogo);

            int subTitInd=c.getColumnIndex(ALDbAdapter.SUBTITLE);
            int titInd=c.getColumnIndex(ALDbAdapter.TITLE);
            int ltId=c.getColumnIndex(ALDbAdapter.LTID);
            int ltype=c.getInt(ltId);
            if(!c.getString(subTitInd).equalsIgnoreCase("")){
                subTitle.setText(c.getString(subTitInd));
                title.setText(c.getString(titInd));
            }else{
                subTitle.setText(c.getString(titInd));
            }
            if(ltype==1){
                imgIcon.setImageResource(R.drawable.logo1);
            }else if(ltype==2){
                imgIcon.setImageResource(R.drawable.logo2); 
            }else{
                imgIcon.setImageResource(R.drawable.logo3); 
            }
        }

}
//On post i closed the cursor
///On Resume

searchCursor= dbReaderContact.rawQuery(query, null);
            startManagingCursor(searchCursor);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜巴黎 2024-11-10 15:00:48

您正在谈论 ListView 并使用 SimpleCursorAdapter。您是否认为创建自己的 ArrayAdaptor 不是更有效:您创建自己的类来保存数据库请求中所需的数据(迭代游标),然后填充该类的 ArrayList。
然后将这个 ArrayList 交给一个扩展 ArrayAdaptor 的类。您应该将 ArrayList 存储在 ArrayAdaptor 中,并重写构造函数和公共 View getView(intposition,ViewconvertView,ViewGroupparent) 方法来创建 ListView 的每个视图。

您可以谷歌自定义ListView(第一个结果:http://www .softwarepassion.com/android-series-custom-listview-items-and-adapters/)。

You are talking about ListView and using SimpleCursorAdapter. Don't you think creating your own ArrayAdaptor isn't more efficient : you create a class of your own to hold the data you want from your DB request (iterate over your Cursor), then you populate an ArrayList of that class.
Then you give this ArrayList to a class which extens ArrayAdaptor. You should store the ArrayList in the ArrayAdaptor and override contructor and the public View getView(int position, View convertView, ViewGroup parent) method to create each view of your ListView.

You can google custom ListView (first result : http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/ ).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文