0

I'm currently working on a simple Android application which will allow a user to add/remove/delete a record from a sqlite DB using checkboxes. The main activity has a listview which renders objects from an exercise adapter. The adapter extends from cursor adapter. The issue I'm having is when selecting a checkbox, then scrolling down the list so that the checkbox is out of view, the state is lost. Here are extracts of my main activity and my exercise adapter:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        dbManager = new DBManager(this);
        dbManager.open();
        adapter = new ExerciseAdapter(this, dbManager.fetch());
        listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(adapter);
    }

    public void deleteExercise(View view) {
        for (int i = 0; i < adapter.getCount(); i++) {
            CheckBox c = listView.getChildAt(i).findViewById(R.id.checkBox);
            if (c.isChecked()) {
                deleteIds.add(adapter.getItemId(i));
            }
        }
        for (Long deleteId : deleteIds) {
            dbManager.delete(deleteId);
            adapter.update(dbManager.fetch());
        }
    }

ExerciseAdapter:

public class ExerciseAdapter extends CursorAdapter {

    public ExerciseAdapter(Context context, Cursor cursor) {
        super(context, cursor, 0);
    }

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

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        // Find fields to populate in inflated template
        TextView exerciseTitle = view.findViewById(R.id.exerciseTitle);
        TextView exerciseDesc = view.findViewById(R.id.exerciseDescription);
        TextView exerciseDate = view.findViewById(R.id.exerciseDate);

        // Extract properties from cursor
        String title = cursor.getString(cursor.getColumnIndexOrThrow("title"));
        String desc = cursor.getString(cursor.getColumnIndexOrThrow("description"));
        String date = cursor.getString(cursor.getColumnIndexOrThrow("date"));

        // Populate fields with extracted properties
        exerciseTitle.setText(title);
        exerciseDesc.setText(String.valueOf(desc));
        exerciseDate.setText(String.valueOf(date));
    }

    public void update(Cursor cursor) {
        this.swapCursor(cursor);
        this.notifyDataSetChanged();
    }
}

This is adopted code so would like to keep the classes similar to how they are now, unless there is no other option and a big change is required.

Thanks.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
dane_griffiths
  • 339
  • 5
  • 10
  • Possible duplicate : https://stackoverflow.com/a/2408987/3780625 – Maxouille Feb 28 '19 at 14:41
  • They are similar, but in my version, the checkboxes are being declared in the main activity, not the adapter. Adding the checkboxes to the adapter could be a solution, but I want to keep the code as close to the original as possible, so would like to see if the checkboxes can stay in the main activity rather than move them to the adapter – dane_griffiths Feb 28 '19 at 22:19
  • What's the point of keeping the checkboxes in the mainActivity ? – Maxouille Mar 01 '19 at 08:07
  • As previously stated, the code has been inherited, and. was looking for a solution to keep the fix as close to the original as possible. Most examples online put the checkboxes in the adapter; the example I have posted does not do this I know, but want to know if it is possible to achieve what I want without moving the checkboxes to the adapter? – dane_griffiths Mar 03 '19 at 17:18

0 Answers0