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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
public class MorceauFilsAdapter extends ArrayAdapter<Fils> {
private int mode;
private SparseArray<String> editedValues;
public MorceauFilsAdapter(Context context, List<Fils> fils, int mode) {
super(context, R.layout.activity_morceau_fils, R.id.TV_mFils_Nom,fils);
this.mode = mode;
this.editedValues = new SparseArray<String>();
}
private class ViewHolder {
public TextView tvNom;
public EditText etVal;
public Switch swBool;
public TextWatcher watcher;
}
public View getView(int position, View convertView, ViewGroup parent){
View ret = super.getView(position,convertView,parent);
ViewHolder holder = (ViewHolder) ret.getTag();
if (holder == null) {
holder = new ViewHolder();
holder.tvNom = (TextView)ret.findViewById(R.id.TV_mFils_Nom);
holder.etVal = (EditText)ret.findViewById(R.id.ET_mFils_Val);
holder.swBool = (Switch)ret.findViewById(R.id.switch1);
ret.setTag(holder);
} else {
holder.etVal.removeTextChangedListener(holder.watcher);
}
Fils fils = getItem(position);
int type = fils.getTypeFils();
String val = fils.getValeurFils();
String comment = fils.getComFils();
holder.tvNom.setText(fils.getNomFils());
String value = this.editedValues.get(position);
if (value == null)
value = val;
holder.etVal.setText(value);
if (type < MaDatabase.TYPE_BOOL)
{
holder.etVal.setVisibility(View.VISIBLE);
holder.swBool.setVisibility(View.GONE);
}
if (type == MaDatabase.TYPE_TEXT) {
holder.etVal.setInputType(InputType.TYPE_CLASS_TEXT);
}
if (type == MaDatabase.TYPE_DATE) {
setDate fromDate = new setDate(holder.etVal,fils.getNomFils()); // Résoudre le bug avec ces #$ù*µ de dates
}
if (type == MaDatabase.TYPE_INT)
holder.etVal.setInputType(InputType.TYPE_CLASS_NUMBER);
if (type == MaDatabase.TYPE_BOOL) {
holder.etVal.setVisibility(View.GONE);
holder.swBool.setVisibility(View.VISIBLE);
if (getContext().getString(R.string.yes).equals(value))
{
holder.swBool.setChecked(true);
holder.swBool.setText(getContext().getString(R.string.yes));
holder.etVal.setText(getContext().getString(R.string.yes));
}
else
{
holder.swBool.setChecked(false);
holder.swBool.setText(getContext().getString(R.string.no));
holder.etVal.setText(getContext().getString(R.string.no));
}
final ViewHolder finalHolder = holder;
holder.swBool.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (finalHolder.swBool.isChecked()) {
finalHolder.swBool.setText(getContext().getString(R.string.yes));
finalHolder.etVal.setText(getContext().getString(R.string.yes));
} else {
finalHolder.swBool.setText(getContext().getString(R.string.no));
finalHolder.etVal.setText(getContext().getString(R.string.no));
}
}
});
}
TextWatcher watcher = new ListTextWatcher(position);
holder.etVal.addTextChangedListener(watcher);
holder.watcher = watcher;
//On verrouille les commande si on est en mode SELECT
if (mode == ActivityFils.MODE_SELECT) {
holder.etVal.setEnabled(false);
holder.swBool.setEnabled(false);
}
return ret;
}
public boolean isModified(int position){
return this.editedValues.get(position) != null;
}
public String getModifiedValue(int position){
return this.editedValues.get(position);
}
private class ListTextWatcher implements TextWatcher {
private int position;
public ListTextWatcher(int p) { this.position = p;}
@Override
public void afterTextChanged(Editable s) {
MorceauFilsAdapter.this.editedValues.put(this.position, s.toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
}
private class setDate implements View.OnFocusChangeListener, DatePickerDialog.OnDateSetListener {
private EditText editText;
private Calendar myCalendar;
String nom;
public setDate(EditText editText, String nom) {
this.editText = editText;
this.editText.setOnFocusChangeListener(this);
myCalendar = Calendar.getInstance();
this.nom = nom;
}
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// this.editText.setText();
String myFormat = "yyyyMMdd";
SimpleDateFormat sdformat = new SimpleDateFormat(myFormat, Locale.FRANCE);
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
editText.setText(sdformat.format(myCalendar.getTime()));
}
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if (hasFocus)
new DatePickerDialog(getContext(), this, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
}
} |
Partager