mardi 13 juillet 2021

Android Cursor Problem with CustomAdapter CheckBox

I have a list view that is filled with filtered data from a SQLite DB with a SimpleCursorAdapter

However, I always get the _id from the next item in the ListView; the cursor is always one item further.

for example: If I click on the item with 7142 it returns 7243; at 7243 the id 7037 and so on like here: _id 7242 7243 7037 7116

What's wrong in my adapter? How do I have to change the code? Can someone show me the solution best regards

With this adapter I fill the ListView

MyDataAdapter a = new MyDataAdapter(getActivity(), R.layout.fts_card, crs, from, to);
          searchLv.setSelection(0);
          searchLv.setAdapter(a);
          a.notifyDataSetChanged();

and this is my CustomAdapter

enter public class MyDataAdapter extends SimpleCursorAdapter {
private Cursor c;
private Context context;
String rowID;
int columnvalue;
MY_DB db;

private ArrayList<Boolean> itemChecked = new ArrayList<Boolean>();

public MyDataAdapter(Context context, int layout, Cursor c, String[] from,
                     int[] to ) {

    super(context, layout, c, from, to);

    this.c = c;
    this.context = context;

    for (int i = 0; i < this.getCount(); i++) {
        itemChecked.add(i, false);
    }
}

public View getView(final int pos, View inView, ViewGroup parent) {
    if (inView == null) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         inView = inflater.inflate(R.layout.fts_card, null);
    }

    this.c.moveToPosition(pos);

    ((TextView) inView.findViewById(R.id.fts_tvTitel)).setText(c.getString(c.getColumnIndex("titel")));

    final CheckBox cBox = inView.findViewById(R.id.fts_chBoxWichtig); // my CheckBox
   

    final View toastInView = inView;

    rowID = c.getString(c.getColumnIndex("_id"));

    cBox.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            CheckBox cb = (CheckBox) v.findViewById(R.id.fts_chBoxWichtig);

            if (cb.isChecked()) {
                itemChecked.set(pos, true);

                // Update the DB

                Toast.makeText(toastInView.getContext(), rowID  , Toast.LENGTH_SHORT).show();

            } else if (!cb.isChecked()) {
                itemChecked.set(pos, false);
                // do some operations here

                cb.setText("Is nothing");
            }
            notifyDataSetChanged();
        }
    });
    cBox.setChecked(itemChecked.get(pos));  
    return inView;
}

}




Aucun commentaire:

Enregistrer un commentaire