在自定义arrayadapter中,我有一个字符串值列表是" true"或" false"。
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;
}
}
我希望根据值&#34; true&#34;设置开关的状态。或&#34; false&#34;。
但是上面的代码会切换自定义listView中的所有开关,而不是listView中某个位置的相应开关。
答案 0 :(得分:2)
在if语句中使用ele.equals(“false”)和ele.equals(“true”),或者只使用布尔值而不是字符串值。
为什么在迭代可回溯数组时设置检查状态?这样,只有数组的最后一个字符串很重要,如果最后一个字符串为“false”,则不会检查该开关,如果最后一个字符串是“true”,则将检查该开关。