1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
class MyAdapter extends ArrayAdapter<MyObject>
{
private SparseArray<String> editedValues;
public MyAdapter(Context ctxt, MyObject[] objs)
{
super(ctxt,R.layout.my_edit_layout.xml,objs);
this.editedValues = new SparseArray<String>();
}
@Override
public View getView(int position, View convert, ViewGroup parent)
{
View ret = super.getView(position,convert,parent);
// on récupère le "EditText"
EditText edt = (EditText) ret.findViewById(R.id.my_text_edit);
// il faut lui mettre les bonnes données !
String edited = this.editedValues.get(position);
if (edited == null) {
MyObject obj = getItem(position);
// on lui colle le texte par défaut de l'objet
edt.setText(obj.getText());
} else {
// la valeur a été modifiée, on lui colle la valeur stockée !
edt.setText(edited);
}
// on lui colle un text-watcher qu'on va conserver sous le coude (Tag) pour pouvoir le supprimer si la vue est recyclée.
TextWatcher watcher = (TextWatcher) edt.getTag();
if (watcher != null) edt.removeTextChangedListener(watcher);
watcher = new ListTextWatcher(position);
edt.addTextChangedListener(watcher);
edt.setTag(watcher);
return ret;
}
// Une inner class pour le watcher:
private class ListTextWatcher implements TextWatcher {
int position;
public TextWatcher(int p) { this.position = p; }
public void afterTextChanged(Editable s) {
{
MyAdapter.this.editedValues.put(this.position,s.toString());
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
}
} |
Partager