0

Am trying to create dynamic checkboxes based on contents of two tables. I am able to create the checkboxes but the problem comes on the OnCheckedChangeListener where i cant pick the index of the clicked checkbox. Below is my code. Any assistance is highly appreciated

I call getViews() on the onCreate() Method

public void getViews(){

    // TableLayout ref from xml where we want to Display list
    final TableLayout proF = (TableLayout) findViewById(R.id.TableLayout_views);

    // Get the database and run the query       
    final SQLiteDatabase datb = mDatabase.getWritableDatabase();

    Cursor curs = datb.rawQuery("SELECT * FROM " + tarea.AREA_TABLE_NAME
            + " WHERE " + tarea.AREA_ID + " LIKE '" +"1"+ "' ", null);      
    curs.moveToFirst();

    if (curs.getCount() > 0) {

        for (int i = 0; i < curs.getCount(); i++) {
            area_id = curs.getInt(curs.getColumnIndex(tarea.AREA_ID));
            curs.moveToNext();
        }
    }

    System.out.println("Area Id is : " + area_id);

    Cursor cur = datb.rawQuery("SELECT * FROM " + tclass.CLASS_TABLE_NAME
            + " WHERE " + tclass.CLASS_AREA_ID + " LIKE '" +area_id+ "' ", null);       
    cur.moveToFirst();

    clss = new String[cur.getCount()];

    if (cur.getCount() > 0) {
        int i ;
        for (i = 0; i < cur.getCount(); i++) {
            clss[i] = cur.getString(cur.getColumnIndex(tclass.CLASS_ITEM));
            System.out.println("Class item : " + clss[i]);

            class_id = cur.getInt(cur.getColumnIndex(tclass.CLASS_ID));

            System.out.println("Class Id is : " + class_id);

            Cursor c = datb.rawQuery("SELECT * FROM " + tmonitoring.MONITORING_TABLE_NAME
                    + " WHERE " + tmonitoring.MONITORING_CLASS_ID + " LIKE '" +class_id+ "' ", null);

            c.moveToFirst();

            avail = new CheckBox[cur.getCount()][c.getCount()];
            edtbox = new EditText[cur.getCount()][c.getCount()];
            System.out.println("cur.getCount()>>" + cur.getCount());
            System.out.println("c.getCount()>>" + c.getCount());
            System.out.println("avail.length >> "+avail.length);

            // class Dynamic table
            TableRow RowArea = new TableRow(this);
            TableRow tRow = new TableRow(this);
            TableRow items = new TableRow(this);
            int color  = getResources().getColor(R.color.font_color_bg);
            int color2  = getResources().getColor(R.color.font_color_white);

            TextView tResult = new TextView(this);
            tResult.setText("MON ITEM");
            tResult.setTextColor(color2);

            TextView tResult1 = new TextView(this);
            tResult1.setText("_____________");
            tResult1.setTextColor(Color.rgb(255, 106, 0));

            TextView tResults = new TextView(this);
            tResults.setText("RESULT");
            tResults.setTextColor(color2);                      

            TextView tResult2 = new TextView(this);
            tResult2.setText("____________");
            tResult2.setTextColor(Color.rgb(255, 106, 0));

            TextView tResults1 = new TextView(this);
            tResults1.setText("ACTION");
            tResults1.setTextColor(color2);

            TextView tResults2 = new TextView(this);
            tResults2.setText("____________");
            tResults2.setTextColor(Color.rgb(255, 106, 0));

            TextView tResults3 = new TextView(this);
            tResults3.setText("ACTION");
            tResults3.setTextColor(color2);

            TextView tResult3 = new TextView(this);
            tResult3.setText("____________");
            tResult3.setTextColor(Color.rgb(255, 106, 0));

            TextView AreaResults = new TextView(this);              
            AreaResults.setBackgroundColor(color);
            AreaResults.setGravity(Gravity.CENTER | Gravity.CENTER_HORIZONTAL);
            AreaResults.setTextColor(color2);
            AreaResults.setText(clss[i]);

            RowArea.addView(AreaResults);
            tRow.addView(tResult);
            items.addView(tResult1);
            tRow.addView(tResults);
            items.addView(tResult2);
            tRow.addView(tResults1);
            items.addView(tResults2);
            tRow.addView(tResults3);
            items.addView(tResult3);
            proF.addView(RowArea);
            proF.addView(tRow);
            proF.addView(items); 

            if (c.getCount() > 0) {

                int j;
                for (j = 0; j < c.getCount(); j++) {
                    System.out.println("Monitoring item : " + c.getString(c.getColumnIndex(tmonitoring.MONITORING_ITEM)));

                    TableRow newRow = new TableRow(this);
                    newRow.setGravity(Gravity.CENTER);
                    newRow.setTag(c.getString(c.getColumnIndex(tmonitoring.MONITORING_CLASS_ID)));

                    // column 1 mon item
                    TextView nameCol = new TextView(this);
                    nameCol.setWidth(20);
                    nameCol.setMaxWidth(8);
                    nameCol.setTypeface(nameCol.getTypeface(), Typeface.BOLD);
                    nameCol.setText(" " + c.getString(c.getColumnIndex(tmonitoring.MONITORING_ITEM)));

                    //column 2 checkbox
                    CheckBox chkbox = new CheckBox(this);
                    chkbox.setId(j);
                    chkbox.setText("Yes");
                    avail[i][j] = chkbox;
                    System.out.println("avail["+i+"]["+j+"]>> " + chkbox);  
                    System.out.println("avail id->" + avail[i][j].getId()); 
                    avail[i][j].setOnCheckedChangeListener(checkListener);

                    //column 3 take pic
                    Button picCol = new Button(this);
                    picCol.setText("Take Pic");
                    picCol.setTag(c.getInt(c.getColumnIndex(tmonitoring.MONITORING_CLASS_ID)));
                    picCol.setOnClickListener(new View.OnClickListener() {

                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            SQLiteDatabase db = mDatabase.getReadableDatabase();
                            Cursor cursor = db.rawQuery("SELECT * FROM "
                                    + shelfImage.IMAGE_TABLE_NAME + ";", null);
                            if (cursor.getCount() > 25) {
                                Log.v(TAG, "Alert SRR to upload data");
                                AlertDialog al = showAlertUpload();
                                al.show();

                            } else {
                                Intent myIntent = null;
                                myIntent = new Intent(Design_Template1.this, Capturephoto.class);

                                Bundle bund = new Bundle();
                                bund.putInt("r", r);
                                bund.putString("user", name);
                                bund.putInt("userid", userid);
                                bund.putString("dealer", dealer);
                                bund.putString("week", weekno);
                                bund.putString("ticket", Ticket);
                                bund.putString("picArray", picArray);
                                myIntent.putExtras(bund);

                                Design_Template1.this.startActivityForResult(myIntent, 0);
                            }

                            cursor.close();
                            db.close();
                        }

                    });

                    // column 4 view pic
                    Button viewCol = new Button(this);
                    viewCol.setText("View Guideline");
                    viewCol.setTag(c.getInt(c.getColumnIndex(tmonitoring.MONITORING_CLASS_ID)));
                    viewCol.setOnClickListener(new View.OnClickListener() {

                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            Integer id = (Integer) v.getTag();
                        }

                    });

                    newRow.addView(nameCol);
                    newRow.addView(chkbox);
                    newRow.addView(picCol);
                    newRow.addView(viewCol);
                    proF.addView(newRow);

                    c.moveToNext();
                }
            }

            c.close();

            cur.moveToNext();
        }
    }

    curs.close();
    cur.close();
    datb.close();


}

My click listener is as below

private OnCheckedChangeListener checkListener = new OnCheckedChangeListener() {

    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // TODO Auto-generated method stub
        // Test avail Checkbox
        StringBuffer r = new StringBuffer();
        int clss;
        db1 = mDatabase.getReadableDatabase();

        System.out.println("area_id on click listener : " + area_id);

        Cursor cur = db1.rawQuery("SELECT * FROM " + tclass.CLASS_TABLE_NAME
                + " WHERE " + tclass.CLASS_AREA_ID + " LIKE '" +area_id+ "' ", null);   

        cur.moveToFirst();
        for(int i = 0; i < cur.getCount(); i++){

            clss = cur.getInt(cur.getColumnIndex(tclass.CLASS_ID));

            System.out.println("Classid on click listener : " + clss);

            results = db1.rawQuery("SELECT "+tmonitoring.MONITORING_ITEM+" FROM " + tmonitoring.MONITORING_TABLE_NAME
                    + " WHERE " + tmonitoring.MONITORING_CLASS_ID + " LIKE '" +clss+ "' ", null);



            for (int u = 0; u < results.getCount(); u++) {
                if (avail[u] != null) {
                    r.append("\nResults->" + u + " : ").append(
                            avail[i][u].isChecked());

                }
            }
            cur.moveToNext();
        }
        cur.close();
        results.close();
        db1.close();
    }
};

The part where i check if isChecked() throws ArrayIndexOutOfBoundsException. Please help

manlio
  • 18,345
  • 14
  • 76
  • 126

1 Answers1

0

setting the tag will help you to recognize the created checkbox on later stage.

avail = new CheckBox[cur.getCount()][c.getCount()]; avail.setTag(i);//add this line

And inside

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) Integer i=(Integer)buttonView.getTag();

above line gets the checkbox on which the check/uncheck event is performed. this way you would be able to find out the index.

Amit Saxena
  • 596
  • 7
  • 18
  • thanks Amit Saxena for your suggestion. avail is a two dimensional array, and even after adding avail[i][j].setTag(i) i could printout the tag as suggested, kept returning null on click. and at the same time i still have a problem checking if the checkbox ischecked – jeffa.david Apr 15 '14 at 12:51
  • did you try (Integer)buttonView.getTag(); for getting the index ? later on you can use paranteView.findViewByTag(tag) to get the checkbox; – Amit Saxena Apr 15 '14 at 13:04
  • Yes i did, and it returns null. I tried it like this Integer k =(Integer)buttonView.getTag(); if(isChecked){ Toast.makeText(getApplicationContext(), "checkbox button : " + k, Toast.LENGTH_SHORT).show(); } – jeffa.david Apr 15 '14 at 13:09
  • Ooops, my bad, was setting the id instead of the Tag. I used avail[i][j].setTag(passed the 1st id of my cursor). I can now get the Tag of the selected index. I will appreciate if u can show me how to use the second way like u mentioned using paranteView.findViewByTag(tag) to get the checkbox; cheers bro – jeffa.david Apr 15 '14 at 13:30
  • Still waiting for your feedback. a little example from the code would be good to elaborate how to achieve the proposal... – jeffa.david Apr 16 '14 at 07:15
  • please see this for the above proposal http://stackoverflow.com/questions/4865244/android-using-findviewbyid-with-a-string-in-a-loop – Amit Saxena Apr 16 '14 at 08:52