I have an adapter that takes in an array list of String, full of a bunch of different names. As of right now, it displays all the names in the listview properly, but I can't get the checkbox to work properly. To test it, I made a Toast that should displays the person's name when the checkbox is cicked. However, it always displays the name of the last person on the list, regardless of which checkbox I press. The checkbox is going to eventually change a setting for the specific person. I know I'm misunderstanding how adapters work, but I'm not sure where.
public class SummonerAdapter extends ArrayAdapter<String> {
TextView summonerNameText;
String summonerName;
CheckBox checkBox;
public SummonerAdapter(Context context, ArrayList<String> summoners) {
super(context, 0, summoners);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getContext()); //inflates layout
View theView = inflater.inflate(R.layout.list_layout, parent, false);
summonerName = getItem(position);
summonerNameText = (TextView) theView.findViewById(R.id.summonerName);
summonerNameText.setText(summonerName);
checkBox = (CheckBox) theView.findViewById(R.id.checkBox);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(getContext(), summonerNameText.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
return theView;
}
}
I understand your question perfectly i had the same problem i couldn't find a better answer. Try to fill your answers on a List
public class SummonerAdapter extends ArrayAdapter<String> {
final TextView summonerNameText;
final String summonerName;
final CheckBox checkBox;
public SummonerAdapter(Context context, ArrayList<String> summoners) {
super(context, 0, summoners);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getContext()); //inflates layout
final View theView = inflater.inflate(R.layout.list_layout, parent, false);
summonerName = getItem(position);
summonerNameText = (TextView) theView.findViewById(R.id.summonerName);
summonerNameText.setText(summonerName);
checkBox = (CheckBox) theView.findViewById(R.id.checkBox);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(getContext(), summonerNameText.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
return theView;
}