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