Listview中的CursorAdapter [英] CursorAdapter in Listview

查看:259
本文介绍了Listview中的CursorAdapter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用CursorAdapter在listview中读取数据库。
i在列表的每个项目都有一个复选框,当复选框被用户选中时,我的数据库中的最喜欢的列更改是和添加到收藏夹的项目。



一切正常,并且收藏列更改,但当我向上和向下滚动列表时,复选框将取消选中。
如果您重新启动应用程序的复选框已经检查



这个问题应该怎么办:



对不起英语的朋友:



CursorAdapter类别

  public class MyAdapter extends CursorAdapter {

上下文b;
LayoutInflater inflater;
@SuppressWarnings(deprecation)
public MyAdapter(Context context,Cursor c){
super(context,c);
inflater = LayoutInflater.from(context);
b =(Context)context;
}

@SuppressWarnings(unused)
@Override
public void bindView(View view,Context context,final Cursor cursor){
/ / TODO自动生成方法存根

TextView tv1 =(TextView)view.findViewById(R.id.txt_name);
TextView tv2 =(TextView)view.findViewById(R.id.txt_numer);

tv1.setText(cursor.getString(2));
tv2.setText(cursor.getString(3));

final int pos = cursor.getPosition();

final CheckBox repeatChkBx =(CheckBox)view.findViewById(R.id.favorite_check);

String me = cursor.getString(cursor.getColumnIndex(like));

if(me.equals(yes)){
repeatChkBx.setChecked(true);
} else {
repeatChkBx.setChecked(false);
}

repeatChkBx.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View view){
MyDatabase MyDatabase = new MyDatabase(b);
SQLiteDatabase mydb = MyDatabase.getWritableDatabase();
cursor.moveToPosition(pos);

if(repeatChkBx.isChecked()){
mydb.execSQL(update list set like ='yes'where id =+ cursor.getString(1));

} else {
mydb.execSQL set like ='no'where id =+ cursor.getString(1));

}
}
});

}

protected上下文getActivity(){
// TODO自动生成方法存根
return null;
}

@Override
public View newView(Context context,Cursor cursor,ViewGroup parent){
return inflater.inflate(R.layout.item,parent,假);
}
}

屏幕截图 p>

解决方案

问题是当你更新你的数据库只是数据库更新不适应光标适配器的光标,所以你必须使用

  changeCursor(newcursor);更新数据库后,您的适配器中的


希望这有助于你。


i'm using CursorAdapter for reading database in listview. i have a checkbox in the each item of the list that when the checkbox was checked by user the favorite Column in my database change the yes and the item added to the favorite.

everything is ok and the favorite column changed but when i scroll up and down the list the checkbox going to unchecked. and if you restarting the app the checkbox have been checked

what should i do for this problem:

sorry for my bad english:

CursorAdapter class:

public class MyAdapter extends CursorAdapter {

    Context b;   
    LayoutInflater inflater;
    @SuppressWarnings("deprecation")
    public MyAdapter(Context context, Cursor c) {
        super(context, c);
        inflater = LayoutInflater.from(context);
        b= (Context) context;
    }

    @SuppressWarnings("unused")
    @Override
    public void bindView(View view, Context context, final Cursor cursor) {
        // TODO Auto-generated method stub

        TextView tv1 = (TextView)view.findViewById(R.id.txt_name);
        TextView tv2 = (TextView)view.findViewById(R.id.txt_numer);

        tv1.setText(cursor.getString(2));
        tv2.setText(cursor.getString(3));

        final int pos = cursor.getPosition();

        final CheckBox repeatChkBx = (CheckBox)view.findViewById(R.id.favorite_check);

        String me = cursor.getString(cursor.getColumnIndex("like"));

        if (me.equals("yes")) {
            repeatChkBx.setChecked(true);
        } else {
            repeatChkBx.setChecked(false);
        }

        repeatChkBx.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                MyDatabase MyDatabase = new MyDatabase(b);
                SQLiteDatabase mydb = MyDatabase.getWritableDatabase();
                cursor.moveToPosition(pos);

                if (repeatChkBx.isChecked()) {                   
                    mydb.execSQL("update list set like = 'yes' where id = " + cursor.getString(1));

                }else{
                    mydb.execSQL("update list set like = 'no' where id = " + cursor.getString(1));           

                }
            }
        });

        }

        protected Context getActivity() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            return inflater.inflate(R.layout.item, parent, false);
        }
    }

screenshot:

解决方案

problem is when you update your database just database going to update not cursor that adapt your cursor adapter ,so you have to use

 changeCursor(newcursor);

in your adapter after updating your database. hope this help you.

这篇关于Listview中的CursorAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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