Reputation: 399
This is the code in my arrayadapter that will set the RadioButtons status:
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
attendance_holder holder = null;
People_Attendance people_array[] = people_attendance.toArray(new People_Attendance[people_attendance.size()]);
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new attendance_holder();
holder.txtName = (TextView)row.findViewById(R.id.name_txt);
holder.txtNumber = (TextView)row.findViewById(R.id.number_txt);
holder.attendance_group = row.findViewById(R.id.Attendance_Group);
holder.not_attending = (RadioButton) holder.attendance_group.findViewById(R.id.not_attending_radio);
holder.attending = (RadioButton) holder.attendance_group.findViewById(R.id.attending_radio);
holder.invited = (RadioButton) holder.attendance_group.findViewById(R.id.invited_radio);
row.setTag(holder);
}
else
{
holder = (attendance_holder)row.getTag();
}
holder.txtName.setText(people_array[position].name);
holder.txtNumber.setText(people_array[position].number);
if (people_array[position].attendance_status == "INVITED"){
holder.invited.setChecked(true);
}else if(people_array[position].attendance_status == "ATTENDING"){
holder.attending.setChecked(true);
}else if(people_array[position].attendance_status == "NOT ATTENDING"){
holder.not_attending.setChecked(true);
}else{
Log.d("ATTENDANCE", people_array[position].attendance_status);
}
return row;
}
I have used the Log.d() in my code to show the string, so I know it's not an empty string or bad string that's the problem. I would therefore think that either my if statements are wrong in someway, or that I am using RadioButton.setChecked() wrong. Then RadioButtons then do not get checked in the way they should do. The RadioButtons are in a RadioGroup.
Any thoughts??
Thanks for the help!!
Upvotes: 0
Views: 206
Reputation: 86
This is the code you should be using. You cannot compare strings like that. You have to use the .equals() function.
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
attendance_holder holder = null;
People_Attendance people_array[] = people_attendance.toArray(new People_Attendance[people_attendance.size()]);
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new attendance_holder();
holder.txtName = (TextView)row.findViewById(R.id.name_txt);
holder.txtNumber = (TextView)row.findViewById(R.id.number_txt);
holder.attendance_group = row.findViewById(R.id.Attendance_Group);
holder.not_attending = (RadioButton) holder.attendance_group.findViewById(R.id.not_attending_radio);
holder.attending = (RadioButton) holder.attendance_group.findViewById(R.id.attending_radio);
holder.invited = (RadioButton) holder.attendance_group.findViewById(R.id.invited_radio);
row.setTag(holder);
}
else
{
holder = (attendance_holder)row.getTag();
}
holder.txtName.setText(people_array[position].name);
holder.txtNumber.setText(people_array[position].number);
if (people_array[position].attendance_status.equals("INVITED")){
holder.invited.setChecked(true);
}else if(people_array[position].attendance_status.equals("ATTENDING")){
holder.attending.setChecked(true);
}else if(people_array[position].attendance_status.equals("NOT ATTENDING")){
holder.not_attending.setChecked(true);
}else{
Log.d("ATTENDANCE", people_array[position].attendance_status);
}
return row;
}
Upvotes: 1
Reputation: 11357
if(people_array[position].attendance_status.equals("INVITED")
use this instead of
people_array[position].attendance_status == "INVITED"
You can simplify the code as below.
holder.invited.setChecked(people_array[position].attendance_status.equals("INVITED"));
holder.attending.setChecked(people_array[position].attendance_status.equals("ATTENDING"));
holder.not_attending.setChecked(people_array[position].attendance_status.equals("NOT ATTENDING"));
Upvotes: 2
Reputation: 12919
You cannot compare Strings
using the =
operator in Java. Use equals()
instead. While using =
compiles fine, it just compares the memory addresses, which certainly is not what you want.
Eg this:
people_array[position].attendance_status == "INVITED"
Should be:
people_array[position].attendance_status.equals("INVITED");
Upvotes: 2