Reputation: 21
I have a ListActivity with each row in the list containing 2 TextViews and 4 RadioButtons.
The 2 TextViews are populated from a SimpleCursorAdapter pulling data from a table in my database and the 4 RadioButtons are just placed into the xml code (with their ids being assigned to 4 RadioButton variables in the Java code.
Basically what I want to do is check the state of each radio button in each row to update my database accordingly.
How would I access each row of RadioButtons to check to see if they are checked or not?
I apologize if this is vague, I will try to add more detail if needed.
Many thanks.
private void processAttendance(){
int index = 0;
this.allStudentsCursor = mDbHelper.fetchEnrolledStudents(mRowId);
lvList=(ListView)findViewById(android.R.id.list);
this.allStudentsCursor.moveToFirst();
while (allStudentsCursor.isAfterLast() == false) {
this.radGroup = (RadioGroup) lvList.getChildAt(index).findViewById(R.id.attendanceGroup);
this.mAttended = (RadioButton)lvList.getChildAt(index).findViewById(R.id.presentRadio);
this.mLate = (RadioButton)lvList.getChildAt(index).findViewById(R.id.lateRadio);
this.mExcused = (RadioButton)lvList.getChildAt(index).findViewById(R.id.excusedRadio);
this.mMissed = (RadioButton)lvList.getChildAt(index).findViewById(R.id.absentRadio);
this.mStudentId = allStudentsCursor.getLong(allStudentsCursor.getColumnIndexOrThrow("_id"));
this.mResult = "";
if(mAttended.isChecked()){
this.mResult ="attended";
radGroup.clearCheck();
}
else if(mExcused.isChecked()){
this.mResult ="excused";
}
else if(mMissed.isChecked()){
this.mResult ="missed";
}
else {
this.mResult ="late";
}
Calendar cal = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
String date = dateFormat.format(cal.getTime());
this.mDbHelper.addAttendance(date, this.mResult, this.mStudentId, mRowId);
allStudentsCursor.moveToNext();
index++;
}
finish();
}
Upvotes: 2
Views: 8915
Reputation: 73494
You can't. The views are recycled in a list view so if you have say 20 items in list you may only actually have 5 instances of RadioButton. Once the view is off the Screen it gets recycled.
What you need to do, is whenever a RadioButton is clicked, change the data behind the list that determines whether the radio needs to be checked.
Upvotes: 2
Reputation: 91351
This is fundamentally flawed -- ListView doesn't inflate a different layout view hierarchy for each row, it keeps only enough needed to fill the screen. As one scrolls off the edge, it is re-used to show the data for the next one scrolling in.
The data inside of each row in the list must come from the data in the cursor. If it isn't, it will be lost when it scrolls off the screen.
Upvotes: 3
Reputation: 33509
You would have to loop over the Cursor
.
myCursor.moveToFirst();
/* Loop over all items */
while (myCursor.isAfterLast() == false) {
//get radio button
RadioButton myRadioButton = (RadioButton) findViewById(R.id.radiobutton);
/*
Edit or Check myRadioButton
*/
myCursor.moveToNext();
}
This is a quick example showing just one RadioButton
but you get the picture.
Upvotes: 2