I created a list with a TextView and an EditText in each item. In order to fill the correct value, I defined several "dataType". For example, if we need to type a date, I show a date picker and I fill the return value in the EditText, and for a NumericID, I define the numPad as soft keyboard.
I've a data ArrayList, for each item, I've such information : a String for textView, a dataType and a String for EditText.
I've 2 problems, and I think they're associated.
First one, my data list's size is only 10, so I've 10 items in my list. But getView() is called more than 20 times when the list's created and each time the soft keyboard shows/hides, this function's recalled for all "positions". It seems wired, isn't it ?
Second one is when I generate my list with an arrayAdapter, my textViews seem good, but the EditTexts seem so random : in my data list, I've "ContactName"-"UserName"-"Jane" and "CreateDate"-"Date"-"12/02/12" etc. When the list is created, all is good. But when I scroll a bit my list, or I show then hide the keyboard, my EditTexts mismatch with my TextView, ContactName's editText could be Date type...
Here's my code: setListAdapter(new ArrayAdapter(this, R.layout.itemview, datalist) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view= convertView;
DataHolder data = getItem(position);
final ViewHolder vholder ;
if (null == view) {
LayoutInflater mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = mInflater.inflate(R.layout.itemview, null);
vholder = new ViewHolder();
vholder.fieldName = (TextView) view.findViewById(R.id.fieldName);
vholder.fieldValue = (EditText) view.findViewById(R.id.fieldValue);
vholder.fieldValue.setHint(data.detail.getFieldType().getFieldTypeName());
// here I define some properties of the EditText, or I show a date picker then I put the
// result in the EditText
vholder.fieldValue = (EditText) data.detail.getFieldType().getValue(vholder.fieldValue);
view.setTag(vholder);
Log.d("convertView is null?", "null "+position);
}
else{
vholder = (ViewHolder) view.getTag();
Log.d("convertView is null?", "not null "+position);
}
if(data != null){
vholder.fieldName.setText(data.name);
if(vholder.fieldValue == null){
Log.i(vholder.fieldName.getText()+" null", "data type is: "+data.detail.getFieldType().getFieldTypeName().toString());
}else{
String fieldValueContent = data.value;
if(!fieldValueContent.isEmpty())
vholder.fieldValue.setText(fieldValueContent);
// update data if focus changes
final String fieldValueText = vholder.fieldValue.getText().toString();
vholder.fieldValue.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if((fieldValueText!=null)&&(!fieldValueText.isEmpty())){
// some update data instructions
}
}
});
}
}
return view;
}
});
public class DataHolder{
public FieldDetails detail;
public String value;
public String name;
}
public class ViewHolder{
public TextView fieldName;
public EditText fieldValue;
}
public class UserName implements FieldType{
@Override
public View getValue(View input) {
((EditText) input).setInputType(InputType.TYPE_TEXT_VARIATION_PERSON_NAME);
return input;
}
}