这是我的数组适配器中将设置单选按钮状态的代码:
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;
}
我在代码中使用了 Log.d() 来显示字符串,所以我知道问题不是空字符串或坏字符串。因此,我认为要么我的 if 语句在某种程度上是错误的,要么我使用 RadioButton.setChecked() 错误。然后,RadioButtons 就不会按照应有的方式进行检查。 RadioButtons 位于 RadioGroup 中。
有什么想法吗?
感谢您的帮助!!
最佳答案
您无法在 Java 中使用 =
运算符来比较字符串
。请改用equals()
。虽然使用 =
编译得很好,但它只是比较内存地址,这当然不是您想要的。
例如:
people_array[position].attendance_status == "INVITED"
应该是:
people_array[position].attendance_status.equals("INVITED");
关于java - RadioButton.setChecked() 似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21583987/