I have a listView whit many checkBox, that when are checked at true start a TextToSpeach. Now the problem is that when i scroll in the listView all the CheckBox that i have checked are reseted at unchecked. So have found any tutorials for a solution. But whit solution that i have used, the state of the checkbox is saved in a variable, and when the checkBox is again visible is rechecked. So the TextToSpeach start again. Here is my code (I know that isn't a good code):
public class MyAdapter extends ArrayAdapter<Model> {
private TreeSet mSeparatorsSet = new TreeSet();
private final List<Model> list;
private final Activity context;
public String textToSay;
public MyAdapter(Activity context, List<Model> list) {
super(context, R.layout.row, list);
this.context = context;
this.list = list;
}
@Override
public int getItemViewType(int position) {
if (list.get(position).getVersion() == 0) {
return 0;
}
return 1;
}
@Override
public int getViewTypeCount() {
return 2;
}
static class ViewHolder {
public TextView text;
public TextView text2;
public CheckBox checkbox;
public TextView textSeparator;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (this.getItemViewType(position)== 0){
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
viewHolder = new ViewHolder();
convertView = inflator.inflate(R.layout.title_row, null);
viewHolder.textSeparator = (TextView) convertView.findViewById(R.id.textView);
convertView.setTag(viewHolder);
convertView.setTag(R.id.textView, viewHolder.textSeparator);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.textSeparator.setText(list.get(position).getName());
return convertView;
}
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
convertView = inflator.inflate(R.layout.row, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView.findViewById(R.id.label);
viewHolder.text2 = (TextView)convertView.findViewById(R.id.label2);
viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.checkBox);
convertView.setTag(viewHolder);
convertView.setTag(R.id.label, viewHolder.text);
convertView.setTag(R.id.checkBox, viewHolder.checkbox);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.checkbox.setTag(position); // This line is important.
viewHolder.text.setText(list.get(position).getName());
viewHolder.text2.setText(list.get(position).getName2());
viewHolder.checkbox.setChecked(list.get(position).isSelected());
final CheckBox chk = (CheckBox)convertView.findViewById(R.id.checkBox);
final View finalConvertView = convertView;
chk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
if (isChecked) {
String textPart1 = " ";
String textPart2 = " ";
String textToSay = " ";
TextView label2 = (TextView) finalConvertView.findViewById(R.id.label2);
TextView label1 = (TextView) finalConvertView.findViewById(R.id.label);
textPart1 = label2.getText().toString();
if ((getPosition + 1) < list.size()) {
if(list.get(getPosition+1).getVersion() == 0){
textPart2 = list.get(getPosition + 2).getName();
} else {
textPart2 = list.get(getPosition + 1).getName();
}
}
textToSay = textPart1 +
"\n" + "." +
"\n" + "." +
"\n" + "." +
"\n" + "." +
"\n" + "." +
"\n" + "." +
"\n" + "." +
"\n" + "." +
"\n" + "." +
"\n" + "." +
"\n" + "." +
textPart2;
OneFragment.startSpeak(textToSay);
}
}
});
return convertView;
}
}
So anyone have an idea how i can solv my problem?
Aucun commentaire:
Enregistrer un commentaire