RadioButton.setChecked()似乎不起作用

时间:2014-02-05 17:17:20

标签: java android string if-statement radio-button

这是我的arrayadapter中的代码,它将设置RadioButtons状态:

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中。

有什么想法吗?

感谢您的帮助!!

3 个答案:

答案 0 :(得分:2)

您无法在Java中使用Strings运算符比较=。请改用equals()。虽然使用=编译很好,但它只是比较内存地址,这当然不是你想要的。

例如:

people_array[position].attendance_status == "INVITED"

应该是:

people_array[position].attendance_status.equals("INVITED");

答案 1 :(得分:2)

if(people_array[position].attendance_status.equals("INVITED") 

使用此代替

people_array[position].attendance_status == "INVITED"

您可以简化代码,如下所示。

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"));

答案 2 :(得分:1)

这是您应该使用的代码。你无法比较那样的字符串。你必须使用.equals()函数。

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;

}