java - RadioButton.setChecked() 似乎不起作用

标签 java android string if-statement radio-button

这是我的数组适配器中将设置单选按钮状态的代码:

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/

相关文章:

安卓形状线

java - 当传递给 Intent 构造函数时,从事件处理程序传递 this 与 ClassName.this 有什么区别?

python - numpy 数组和返回上的 str() 方法

java - Canoo WebFunctionalTest/Selenium,功能比较

android - Eclipse 不识别任何导入

python - 拆分和连接字符串

java - 从存储在 HashMap 中的字符串数组列表生成字符串组合

java - FileCleaner 是为了避免在 Web 应用程序中进行多次部署吗?

java - finally block 是否总是在 Java 中执行?

java - 字符串相等与位置相等