在自定義數組適配器中,我有一個「true」或「false」字符串值列表。迭代中的Android切換開關按鈕
public class CustomFriendList extends ArrayAdapter<String> {
private final Activity context;
private List<String> friendName = new ArrayList<String>();
private List<String> backchatable = new ArrayList<String>();
public CustomFriendList(Activity context, List<String> friendName, List<String> backchatable){
super(context, R.layout.fragment_friend_list_item, friendName);
this.context = context;
this.friendName = friendName;
this.backchatable = backchatable;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.fragment_friend_list_item, null, true);
TextView friend = (TextView) rowView.findViewById(R.id.friendName);
Switch switchBackChat = (Switch) rowView.findViewById(R.id.on_off_backchatable);
friend.setText(friendName.get(position));
for (String ele : backchatable) {
//System.out.println(ele.toString());
if(ele.equals("false")){
switchBackChat.setChecked(false);
}else if(ele.equals("true")){
switchBackChat.setChecked(true);
}
}
return rowView;
}
}
我期待根據值「true」或「false」設置開關的狀態。
但是,上面的代碼切換了自定義listView中的所有開關,而不是在listView中某個位置的相應開關。
您可以發佈您的整個getView方法嗎?另外,正如@Pavneet_Singh所建議的,您沒有正確地檢查字符串值(使用ele.equals(「false」),因爲這是在Java中檢查字符串值的正確方法)。 –
從==到.equals的檢查是正確的。但是,各個開關不會相應地改變。他們都一起改變。 – Jupiter