public class UnitListAdapter extends ArrayAdapter<Unit> {
private Context context;
private ArrayList<Unit> units;
private MeasurementType type;
private static HashMap<String, Double> unitValues;
public void setMeasurementType(MeasurementType measurementType) {
type = measurementType;
}
public void setUnitValues(HashMap<String, Double> newValues) {
unitValues.clear();
unitValues = newValues;
}
public void setUnits(ArrayList<Unit> newUnits) {
units = newUnits;
}
}
Above is the implementation of ArrayAdapter
minus the getView()
and getCount()
methods. Now, in my activity I have this method:
public void updateUnitAdapter(ArrayList<Unit> units, final MeasurementType measurementType) {
//change the type
unitAdapter.setMeasurementType(measurementType);
//set the hashmap unit values
unitValues = new HashMap<String, Double>() {{
put(measurementType.getType(), DEFAULT_VALUE);
}};
//clear the current units for the previous measurement type
unitAdapter.clear();
//add the new units for the new measurement type
for(Unit u : units) {
unitAdapter.add(u);
}
//update the list view
unitAdapter.notifyDataSetChanged();
}
but when I step through in the debugger, it gets to the getView()
method and when I check these variables, they are haven't changed to the new ones that I am setting them too, they stay the same...is there something I am not understanding about ArrayAdapter
?
ArrayAdapter
has its own internal collection, which is modified when you call add()
or clear()
. In this case you are updating it, but (from your comments) it looks like you have also overriden getCount()
and getView()
to get those values from somewhere else (possibly the units
member?).
If that's the case, you should update units
intead of calling clear()
/add()
.
Otherwise, remove the units
field altogether and use getItem()
to access the collection items.