dimanche 31 juillet 2016

How to Set ID's on programmatically created checkboxes?

Hello everyone and coding lovers,

at the moment i am working on a small app that helps me to track my nutrition.

Now, i build a Layout with an Adapter.

For this purpose im using a SimpleCursorAdapter because all needed data comes from my database.

Everything went fine except one thing:

All checkboxes has the same id. So im not able to check each of them. There is one solution i know how it can be solved but in this case i cant use an xml file to generate ids because i dont know how many checkboxes will be next time i run the app.

Here is my code:

public void getIdAndDate()
{
    db = new DATABASE(this);

    SQLiteDatabase sqldb = db.getReadableDatabase();

    String[] Columns = {"_id", "date"};
    int[] toViews = {R.id.entry_id, R.id.entry_date};

    Cursor cursor1;

    cursor1 = sqldb.query(db.TABLE_DAILY_CONSUMPTION, Columns, null, null, null, null, null);

    numRows = cursor1.getCount();

    int id[] = new int[numRows];
    String date[] = new String[numRows];

    for(int i = 0; i<numRows; i++)
    {
        cursor1.moveToNext();
        id[i] = cursor1.getInt(0);
        date[i] = cursor1.getString(1);
    }

    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.layout_entry, cursor1, Columns, toViews, 0);
    entries.setAdapter(adapter);
}

And this is my Layout:

<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/entry_id"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:textSize="20sp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/entry_date"
    android:layout_gravity="center_horizontal"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:textSize="20sp"/>

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/entry_selection"
    android:layout_gravity="right"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    />

I really try to figure out how i can solve the problem but i cant even imagine which possibilities i have.

I'm still looking for a solution.




Aucun commentaire:

Enregistrer un commentaire