Well, I have a seekBar into a custom listview, and I'm using the setTag/getTag method on my onChangeClickListener, just as suggested here in this question and works fine. But when I add a new element to the listView, the seekbar of the new element takes the progress of the element that previously was on its position.
Note that my SeekBar sbAnalog
updates my EditText etAnalogPosition
and vice-versa
Here is my getView()
code:
public View getView(final int position, View convertView, ViewGroup parent) {
view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.my_custom_list_layout, null);
holder = new ViewHolder();
holder.sbAnalog = (SeekBar) view.findViewById(R.id.sbAnalog);
holder.etAnalogPosition = (EditText) view.findViewById(R.id.tvAnalogPosition);
view.setTag(holder);
}else{
holder = (ViewHolder) view.getTag();
}
holder.sbAnalog.setTag(holder.etAnalogPosition);
holder.etAnalogPosition.setTag(holder.sbAnalog);
holder.sbAnalog.setMax(list.get(position).getMax());
holder.sbAnalog.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
EditText et = (EditText) seekBar.getTag();
et.setText(String.valueOf(i));
list.get(position).setPosition(i);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
holder.etAnalogPosition.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
if (i == EditorInfo.IME_ACTION_DONE) {
int value = 0;
try {
value = Integer.parseInt(textView.getText().toString());
} catch (NumberFormatException e) {
SeekBar sb = (SeekBar )textView.getTag();
value = sb.getProgress();
}
//if (value > list.get(position).getMax())
//value = list.get(position).getMax();
SeekBar sb = (SeekBar) textView.getTag();
sb.setProgress(value);
textView.clearFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
textView.setText(String.valueOf(sb.getProgress()));
}
return false;
}
});}
Need help, thank you.
I just realized that the best way to solve is adding this two lines to the code:
holder.sbAnalog.setProgress(list.get(position).getPosition());
holder.etAnalogPosition.setText(String.valueOf(list.get(position).getPosition()));
Note that every element of my list is a custom Object that I created, which one has a getPosition()
method that basically returns the seekBar's position of the element, setted by a setPosition()
method.
So if you are having the same problem, I recommend you to make it this way, or simply create an arrayAdapter that stores every element's position, so you'll be able to set
and get
just as I did im my getView()
code.
Hope it helps!