Reputation: 665
In a custom arrayadapter I have a list of string values being either "true" or "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;
}
}
I am looking to set the state of the switches according to the values "true" or "false".
However the code above switches all the switches in the custom listView and not the respective ones at some position in the listView.
Upvotes: 1
Views: 35
Reputation: 148
Use ele.equals("false") and ele.equals("true") in the if statements or just use booleans instead of string values.
And why are you setting the checked status while iterating through the backchatable array? By this only the last string of the array matters and if the last string is "false" the switch will not be checked and if the last string is "true" the switch will be checked.
Upvotes: 2