CursorAdapter in Listview
Asked Answered
E

3

10

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

Evert answered 17/5, 2014 at 21:53 Comment(4)
is there anyone to answer this questionEvert
possible duplicate of Listview items Animation Stop While ScrollingInterchange
possible duplicate of Listview with CursorAdapterOverlie
the problem is ListView dose not know which item exactly clicked, you can do this by setTag and getTag for each ItemFlew
S
2

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.

Siddra answered 13/5, 2015 at 17:28 Comment(0)
A
4

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];
Albertina answered 26/5, 2014 at 21:56 Comment(0)
S
2

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.

Siddra answered 13/5, 2015 at 17:28 Comment(0)
L
1

I would use the listAdapter to implement the use of multiple selections in a ListView. Create the following Model which hold the name and the information if this element is currently selected.

First create the model class:

public class Model {

    private String name;
    private boolean selected;

    public Model(String name) {
        this.name = name;
        selected = false;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }

} 

Create the following xml file in the layouts folder:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<TextView
    android:id="@+id/label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@+id/label"
    android:textSize="30px" >
</TextView>

<CheckBox
    android:id="@+id/check"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_marginLeft="4px"
    android:layout_marginRight="10px" >
</CheckBox>
</RelativeLayout> 

Create the following Adapter. This adapter adds a listener on the Checkbox view . If the checkbox is selected the underlying data of the model is changed. Checkbox gets the corresponding model element assigned via the getTag() method.

import java.util.List;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

public class InteractiveArrayAdapter extends ArrayAdapter<Model> {

    private final List<Model> list;
    private final Activity context;

    public InteractiveArrayAdapter(Activity context, List<Model> list) {
        super(context, R.layout.rowbuttonlayout, list);
        this.context = context;
        this.list = list;
    }

    static class ViewHolder {
        protected TextView text;
        protected CheckBox checkbox;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
            view = inflator.inflate(R.layout.rowbuttonlayout, null);
            final ViewHolder viewHolder = new ViewHolder();
            viewHolder.text = (TextView) view.findViewById(R.id.label);
            viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
            viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
          Model element = (Model) viewHolder.checkbox.getTag();
          element.setSelected(buttonView.isChecked());

          }
        });
        view.setTag(viewHolder);
        viewHolder.checkbox.setTag(list.get(position));
        } else {
            view = convertView;
            ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
        }
        ViewHolder holder = (ViewHolder) view.getTag();
        holder.text.setText(list.get(position).getName());
        holder.checkbox.setChecked(list.get(position).isSelected());
    return view;
    }
}

At the end, you need to change your activity to the following:

import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class MyList extends ListActivity {

/** Called when the activity is first created. */
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    // create an array of Strings, that will be put to our ListActivity
    ArrayAdapter<Model> adapter = new InteractiveArrayAdapter(this, getModel());
    setListAdapter(adapter);
}

private List<Model> getModel() {
    List<Model> list = new ArrayList<Model>();
    //The following elements need to be changed by your elements.
    list.add(get("List number 1"));
    list.add(get("List number 2"));
    list.add(get("List number 3"));
    list.add(get("List number 4"));
    list.add(get("List number 5"));
    list.add(get("List number 6"));

    // Initially select one of the items
    list.get(1).setSelected(true);
    return list;
}

private Model get(String s) {
      return new Model(s);
}

}

Leatherneck answered 27/5, 2014 at 7:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.