Find RecordID from Database and attach to checkbox in Android Development
<p>I am just trying to learn Android development and my first little project will be a to-do list. I have now worked on the project for weeks, but got stuck and don't seem to move at all.</p>

<p>I have an ExpandableListView, which shows me the List of tasks (Parents), when expanded, I get the additional information for that task (Child). Additional I want a checkbox attached to the Parent, where I can set the status of the task. Everythign comes from the database. </p>

<p>I have found some sourcecode on internet and changed it a little bit to work for me. Everything seems to be fine, up to the point when I want to update the status. I am not sure, how I get the RecordID from the task when I click on the checkbox. There are lots of information on internet, but nothing I could easily adapt for my code and since it is working that far, I am scared to change it again, since this is not the first one I had working and everytime failed somewhere. I am attaching the Sourcecode, maybe someone can give me an advice. Thank you very much!</p>

<pre><code>public class CheckableListActivity extends ExpandableListActivity
{
private static final int CHECK_CHANGED = 1;
private static final String STR_REGISTERED = " has registered!";
private static final String STR_UNREGISTERED = " has unregistered!";

DatabaseHelper dbHelper;
TextView textProjectName;
TextView textProjectStartdate;
TextView textProjectEnddate;

private ArrayList&lt;Parent&gt; parents;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.project_details);
    getExpandableListView().setGroupIndicator(null);
    getExpandableListView().setDivider(null);
    getExpandableListView().setDividerHeight(0);
    registerForContextMenu(getExpandableListView());

    textProjectName = (TextView) findViewById(R.id.textProjectName);
    textProjectStartdate = (TextView) findViewById(R.id.textProjectStartdate);
    textProjectEnddate = (TextView) findViewById(R.id.textProjectEnddate);

    dbHelper=new DatabaseHelper(this);
    Cursor cur= dbHelper.getProject(1);

    setProjectDetails(cur);

    final ArrayList&lt;Parent&gt; dummyList = buildDummyData(cur);
    loadHosts(dummyList);

}



/**
 * Get Tasks from DB and create Expendable ListView
 * @return Array with Parents and Children
 */
private ArrayList&lt;Parent&gt; buildDummyData(Cursor cur)
{
    final ArrayList&lt;Parent&gt; list = new ArrayList&lt;Parent&gt;();

    cur.moveToFirst();

    int i = 0;
    while (cur.isAfterLast() == false) 
    {
        final Parent parent = new Parent();
        parent.setName("Parent " +cur.getString(cur.getColumnIndex("TaskName")));
        if(cur.getString(cur.getColumnIndex("Status")).equals("closed"))
        {
            parent.setChecked(true);

        }

        parent.setChildren(new ArrayList&lt;Child&gt;());

        for( int n = 0 ; n &lt; 1 ; ++n ) 
        {
            final Child child = new Child();

            child.setName(cur.getString(cur.getColumnIndex("TaskInfo")));
            parent.getChildren().add(child);
        }
        i++;
        cur.moveToNext();
        list.add(parent);

    }

    return list;
}

private void loadHosts(final ArrayList&lt;Parent&gt; newParents)
{
    if (newParents == null)
        return;
    parents = newParents;
    Log.d("StackOverFlow", "parents arrived: " + newParents.size());
    if (this.getExpandableListAdapter() == null)
    {
        final MyExpandableListAdapter mAdapter = new MyExpandableListAdapter();
        this.setListAdapter(mAdapter);
    }
    else
        ((MyExpandableListAdapter)getExpandableListAdapter()).notifyDataSetChanged();
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}


// React to clicks on the action Bar Menu
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {

            case R.id.menu_dashboard:
                Intent intent = new Intent(this, MainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                return true;

            default:
                return super.onOptionsItemSelected(item);

        }

}


private void setProjectDetails(Cursor cur) 
{   
      cur.moveToFirst();
      String ProjectName = cur.getString(cur.getColumnIndex("ProjectName"));
      String dateString = cur.getString(cur.getColumnIndex("Startdate"));

      // The format of your date string
      SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
      // The format of your target date string
      SimpleDateFormat postformat = new SimpleDateFormat("E, dd-MMM-yyyy");

      // The calendar instance which adds a locale to the date
      final Calendar cal = Calendar.getInstance();

            try {
                // Parse the date string to return a Date object
                Date date = formatter.parse(dateString);

                // Format the Start Date 
                String formattedStartdate = postformat.format(date);

                // Set the calendar with our date object
                cal.setTime(date);
                cal.add(Calendar.DAY_OF_MONTH, 40); // add the extra days

                // Format the End Date 
                String formattedEnddate = postformat.format(cal.getTime());

                // Set Details 
                textProjectName.setText(ProjectName);
                textProjectStartdate.setText("Planned Startdate: " +formattedStartdate);
                textProjectEnddate.setText("Planned Enddate: " +formattedEnddate);

            } catch (ParseException e) {
                e.printStackTrace();
            } 

    }

private class MyExpandableListAdapter extends BaseExpandableListAdapter
{
    private Handler checkChangeHander = new Handler()
    {
        public void handleMessage(Message msg) 
        {
            final Parent parent = (Parent)msg.obj;
            switch (msg.what)
            {
                case CHECK_CHANGED:
                    ((MyExpandableListAdapter)getExpandableListAdapter()).notifyDataSetChanged();
                    break;
                default:
                    break;
            }
            final Boolean checked = parent.isChecked();
            Toast.makeText(getApplicationContext(), parent.getName() + " " + (checked ? STR_REGISTERED : STR_UNREGISTERED), 
                    Toast.LENGTH_SHORT).show();

            /*
            dbHelper=new DatabaseHelper(this);
            dbHelper.updateTaskStatus(boolean isChecked); 
            */
        };
    };


    private final class CheckUpdateListener implements OnCheckedChangeListener
    {
        private final Parent parent;
        /**
         * @param child
         */
        private CheckUpdateListener(Parent child)
        {
            this.parent = child;
        }
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            parent.setChecked(isChecked);
            final Message msg = new Message();
            msg.what = CHECK_CHANGED;
            msg.obj = parent;
            checkChangeHander.sendMessage(msg);


        }
    }

    private LayoutInflater inflater;

    public MyExpandableListAdapter()
    {
        inflater = LayoutInflater.from(CheckableListActivity.this);
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, 
            View convertView, ViewGroup parentView)
    {
        final Parent parent = parents.get(groupPosition);
        convertView = inflater.inflate(R.layout.group_row, parentView, false);
        ((TextView) convertView.findViewById(R.id.parentname)).setText(parent.getName());
        CheckBox check1 = (CheckBox) convertView.findViewById(R.id.check1);


        check1.setChecked(parent.isChecked());
        if (parent.isChecked())
            convertView.setBackgroundResource(R.color.green);
        else
            convertView.setBackgroundResource(R.color.red);
        check1.setOnCheckedChangeListener(new CheckUpdateListener(parent));



        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, 
            View convertView, ViewGroup parentView)
    {
        final Parent parent = parents.get(groupPosition);
        final Child child = parent.getChildren().get(childPosition);
        convertView = inflater.inflate(R.layout.child_row, parentView, false);
        ((TextView) convertView.findViewById(R.id.childname)).setText(child.getName());
        return convertView;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition)
    {
        return parents.get(groupPosition).getChildren().get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition)
    {
        return childPosition;
    }

    @Override
    public int getChildrenCount(int groupPosition)
    {
        return parents.get(groupPosition).getChildren().size();
    }

    @Override
    public Object getGroup(int groupPosition)
    {
        return parents.get(groupPosition);
    }

    @Override
    public int getGroupCount()
    {
        return parents.size();
    }

    @Override
    public long getGroupId(int groupPosition)
    {
        return groupPosition;
    }

    @Override
    public void notifyDataSetChanged()
    {
        super.notifyDataSetChanged();
    }

    @Override
    public boolean isEmpty()
    {
        return ((parents == null) || parents.isEmpty());
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition)
    {
        return true;
    }

    @Override
    public boolean hasStableIds()
    {
        return true;
    }

    @Override
    public boolean areAllItemsEnabled()
    {
        return true;
    }
}
</code></pre>

<p>}</p>
