0

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;
}
}

2 Answers2

0
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 

as per link

This is not an issue, there is absolutely no guarantee on the order in which getView() will be called nor how many times. In your particular case you are doing the worst thing possible with a ListView by giving it a height=wrap_content. This forces ListView to measure a few children out of the adapter at layout time, to know how big it should be. This is what provides ListView with the convertViews you see passed to getView() even before you scroll.

Focusable EditText inside ListView

Community
  • 1
  • 1
Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36
0
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 

Very first time List View creates Views(row) only for which device can occupy , if you scroll the List View it will check whether View Type is changed if so it will create new View or else it will re-use your View just by replacing your Data..

Working of getView()

Also if you give List View height as wrap_content only first three Views(rows)will be shown rest will be ignored..

Problem in EditText may be due to your if else block.. Check it properly.. Your are check

if(data=null){
  vholder.fieldName.setText(data.name);
}else{
   showing Edit Text // Check it properly
}
Venky
  • 11,049
  • 5
  • 49
  • 66