本文介绍了ArrayAdapter值不改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

以上是 ArrayAdapter getView() getCount将()实施减方法。现在,在我的活动我有这样的方法:

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

但是当我在调试器步,它就会在 getView()方法,并当我检查这些变量,他们并没有更改为新的那我设置他们太多,他们保持不变......是有什么我不理解关于 ArrayAdapter

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 内部集合,当你调用被修改添加()清()。在这种情况下,你正在更新,但是(从你的评论),它看起来像你还重写 getCount将() getView()获得从其他地方这些值(可能是单元会员吗?)。

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().

否则,删除单元字段干脆使用的getItem()来访问集合项目。

Otherwise, remove the units field altogether and use getItem() to access the collection items.

这篇关于ArrayAdapter值不改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 03:32
查看更多