2014-02-05 86 views
0

這是我arrayadapter的代碼,將設置單選按鈕狀態:RadioButton.setChecked()不出現工作

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

任何想法??

感謝您的幫助!

+0

張貼完成getView code – Triode

+0

@RajeshCP我有。 :) –

+0

可能重複[如何比較Java中的字符串?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Reimeus

回答

2

使用Java中的=運算符不能比較Strings。改爲使用equals()。雖然使用=編譯正常,但它只是比較內存地址,這當然不是你想要的。

如這樣的:

people_array[position].attendance_status == "INVITED" 

應該是:

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

這工作!謝謝您的幫助!!那麼「==」運算符只能用於整數和布爾值?是因爲字符串是對象,而不是原語?謝謝! :) –

+0

準確地說,這是因爲字符串不是原語。很高興我能幫上忙。 –

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

不錯的簡化!謝謝!:) –

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; 

}