I've got a ListView that contains EditText objects that correspond to 
inputs for a calculation.  In an effort to increase performance, I've 
implemented a ViewHolder class, for the ListView, since I've read that this 
is the better way to handle the inflation of custom views in the ListView.  
I've got an array of inputs that correspond to the EditTexts that are in 
the list.  In the getView method, for the adapter used in the list, I'm 
setting the TextView as a label for the EditText, with the name of the 
input, and putting the value of the input into the text box.  I determine 
the index into my array of inputs by using the position argument from the 
getView.  Is this not the correct method?

The first time through, it will set the text to the desired value, but then 
it keeps hitting the afterTextChanged event for the first EditText 
repeatedly.  If I set a break point, it will hit it over, and over, until I 
get tired of hitting the play button.  If I disable the breakpoint, though, 
the view will pop up, as expected, but it will have changed the value of 
the first input to 0.0.  I don't know if I've got some kind of recursive 
error situation, with my event handlers, or what.  This is some code out of 
my InputAdapter class.  Will someone let me know if I'm doing something 
fundamentally wrong, here?

public class InputAdapter extends ArrayAdapter<ModeInput> {
    private final Activity context;
    private final ModeInput [] values;
    
    static class ViewHolder {
        public TextView label;
        public EditText textBox;
        public Spinner enumSpinner;
    }
    
    public InputAdapter(Activity context, ModeInput [] values) {
        super(context, R.layout.inputlayout, values);
        this.context = context;
        this.values = values;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup 
parent) {
        View rowView = convertView;
        ModeInput mi = values[position];
        ViewHolder viewHolder;
        
       if (rowView == null)
        {
            LayoutInflater inflater = context.getLayoutInflater();
            rowView = inflater.inflate(R.layout.inputlayout, null);
            viewHolder = new ViewHolder();
            viewHolder.label = 
(TextView)rowView.findViewById(R.id.editLabelTV);
            viewHolder.textBox = 
(EditText)rowView.findViewById(R.id.inputEdit);
            viewHolder.enumSpinner = 
(Spinner)rowView.findViewById(R.id.inputSpinner);
            
            viewHolder.label.setText(mi.GetName().trim() + ":");
            
            viewHolder.textBox.addTextChangedListener(new TextWatcher() {
                public void onTextChanged(CharSequence s, int start, int 
before, int count){}
                public void afterTextChanged(Editable s) 
                {
                    double newValue;
                    String boxContents = s.toString();
                    if (!boxContents.isEmpty())
                    {
                        try {
                            newValue = Double.parseDouble(boxContents);
                            values[position].SetValue(newValue);
                        }
                        catch(Exception exc)
                        {
                            values[position].ChangeValue(0.0);
                        }
                        finally {}
                    }
                    else
                    {
                        values[position].ChangeValue(0.0);
                    }
                }
                public void beforeTextChanged(CharSequence s, int start, 
int count, int after){}
            });
                            
            rowView.setTag(viewHolder);
        }
        else
        {
            viewHolder = (ViewHolder) rowView.getTag();
        }

        viewHolder.label.setText(mi.GetName().trim() + ":");

        viewHolder.enumSpinner.setVisibility(View.GONE);
        viewHolder.textBox.setVisibility(View.VISIBLE);
            
        viewHolder.textBox.setText(Double.toString(mi.GetValue()));

        return rowView;
    }

I think that's all the pertinent code.  I appreciate the help, this thing 
is driving me crazy.

-Mark 

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to