Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CursorAdapter in Listview

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:

enter image description here

like image 431
hamedjj Avatar asked May 17 '14 21:05

hamedjj


People also ask

How do I use CursorAdapter?

1) CursorAdapterIn BaseAdapter, view is created in getView method; in CursorAdapter, however, view is created in newView() method and elements are populated in bindView(). In the newView() method, you simply inflate the view your custom xml and return it. In the bindView() method, you set the elements of your view.

What is the role of the Simplecursoradapter?

An easy adapter to map columns from a cursor to TextViews or ImageViews defined in an XML file. You can specify which columns you want, which views you want to display the columns, and the XML file that defines the appearance of these views.

What is the use of adapter object in Android SDK explain Arrayadapter in detail?

You can use this adapter to provide views for an AdapterView , Returns a view for each object in a collection of data objects you provide, and can be used with list-based user interface widgets such as ListView or Spinner .


2 Answers

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.

like image 20
max Avatar answered Oct 15 '22 04:10

max


The checked items are not recycled. You have to save the checked items to some sort of array - dynamic or static. A boolean array would be well suited for this purpose.

Boolean[] myCheckedItems = new Boolean[SIZE];
like image 174
Björn Hallström Avatar answered Oct 15 '22 05:10

Björn Hallström