As I mentioned in the title, the listview duplicates edittexts values for the children after i=10 in the for loop in my code.
public class ExerciseAdapter extends ArrayAdapter<ExerciseSet> {
private ArrayList<ExerciseSet> mExercise;
private LayoutInflater inflater;
private List<String> texts = new ArrayList<String>();
public ExerciseAdapter(Context context, int textViewResourceId, ArrayList<ExerciseSet> objects) {
super(context, textViewResourceId, objects);
mExercise = objects;
inflater = (LayoutInflater) getContext().
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getItemViewType(int position) {
if(mExercise.get(position).isSet() == false) return 0;
else return 1;
}
@Override
public int getViewTypeCount() {
return 2; // two types of rows, exercise headers and bodies (sets)
}
public View getView(final int position, View convertView, ViewGroup parent){
View view = convertView;
final Holder holder;
for(int i = 0; i < mExercise.size(); i++) {
texts.add(String.valueOf(i));
}
if (getItemViewType(position) == 0) {
if (view == null) {
view = inflater.inflate(R.layout.item_exercise_header, null);
holder = new Holder();
holder.exerciseTitleView = (TextView) view.findViewById(R.id.tv_exercice_title);
view.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
ExerciseSet exercise = mExercise.get(position);
if (exercise != null)
holder.exerciseTitleView.setText(exercise.getExerciseTitle());
} else {
if (view == null) {
view = inflater.inflate(R.layout.item_set, null);
holder = new Holder();
holder.setCounterView = (TextView) view.findViewById(R.id.tv_set_counter);
holder.cbSet = (CheckBox) view.findViewById(R.id.cb_setdone);
holder.repsCapt = (EditText) view.findViewById(R.id.et_reps_number);
holder.repsCapt.setTag(position);
holder.repsCapt.setText(texts.get(position));
//holder.weightCapt = (EditText) view.findViewById(R.id.et_weight_number);
//db = new DatabaseHandler(this.getContext());
//holder.weightCapt.setTag(position);
view.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
ExerciseSet exercise = mExercise.get(position);
/*
* edit texts
*/
int tag_position=(Integer) holder.repsCapt.getTag();
holder.repsCapt.setId(tag_position);
holder.repsCapt.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
final EditText repsText = (EditText) holder.repsCapt;
if(repsText.getText().toString().length()>0){
texts.add(repsText.getText().toString());
}else{
Toast.makeText(getContext(), "Please enter some value", Toast.LENGTH_SHORT).show();
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
}
});
if (exercise != null) {
holder.setCounterView.setText(String.valueOf(exercise.getSetId()));
}
/*
* check boxes
*/
holder.cbSet.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton button, boolean isChecked) {
if(isChecked)
mExercise.get(position).setCbChecked(true);
else
mExercise.get(position).setCbChecked(false);
}
});
holder.cbSet.setChecked(exercise.isCbChecked());
}
return view;
}
static class Holder {
public EditText weightCapt;
public EditText repsCapt;
// header row items
TextView exerciseTitleView;
// body row item
CheckBox cbSet;
TextView setCounterView;
}
}
I dont know why, please help..
Edit_1: I tried saving the values into an arraylist and retrieve them while the views are reused but it still gives me duplicated values while I scroll down.
Edit_2: If my question is unclear please let me know!
Edit_3: I placed holder.repsCapt.setText(texts.get(position)) inside the if (exercise != null) { condition and the values are not duplicated anymore. But still if I scroll down and up the values get messed up.