I've got a ListView containing my own CookPoint objects. A CookPoint has a time and a temp, both of which can be edited. No mater how I try editing a Temp value in the first row changes ALL temps for all rows. Similarly Edit the Time in the first row and all Times are changed whilst editing any other row does nothing. It will change the displayed values but the data structures are not updated and saved.
I've got a custom ArrayAdapter which contains two EditText Views and sets up a TextChangedListener on the getView method. I'm creating a new Listener Object for each EditText View so can't understand why I'm getting Text Changed events for either All rows or None.
private class CookPointArrayAdapter extends ArrayAdapter<CookPoint> {
private ArrayList<CookPoint> mCookPoints;
public CookPointArrayAdapter(ArrayList<CookPoint> cookPoints) {
super(getActivity(), 0, cookPoints);
mCookPoints = cookPoints;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//
if(convertView == null) {
convertView = getActivity().getLayoutInflater().inflate(R.layout.cook_point_list_item, null);
}
CookPoint cp = getItem(position);
Log.d(TAG, "getView CookPoint " + position + " Time:" + cp.getTime() + " Temp:" + cp.getTemp());
EditText time = (EditText)convertView.findViewById(R.id.cook_point_time);
time.setText(Integer.toString(cp.getTime()));
time.addTextChangedListener(new CookPointTextWatcher(cp, time));
EditText temp = (EditText)convertView.findViewById(R.id.cook_point_temp);
temp.setText(Integer.toString(cp.getTemp()));
temp.addTextChangedListener(new CookPointTextWatcher(cp, temp));
return convertView;
}
private class CookPointTextWatcher implements TextWatcher {
private CookPoint mCookPoint;
private View mView;
private CookPointTextWatcher(CookPoint cp, View view) {
this.mCookPoint = cp;
this.mView = view;
}
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
@Override
public void afterTextChanged(Editable editable) {
String text = editable.toString();
Log.d(TAG, "After Text Changed CookPoint:" + Integer.toString(mCookPoint.getIndex()) + " New Value " + text);
int i;
try {
i = Integer.parseInt(text);
} catch (Exception e) {
Log.e(TAG, "Failed to parseInt()");
return;
}
switch(mView.getId()){
case R.id.cook_point_time:
Log.d(TAG, "SetTime" + text);
mCookPoint.setTime(i);
break;
case R.id.cook_point_temp:
Log.d(TAG, "SetTemp");
mCookPoint.setTemp(i);
break;
}
}
} // private class CookPointWatcher
} // private class CookPointArrayAdapter
Can anybody advise why I'm getting this behaviour?
0 comments:
Post a Comment