自定义ArrayAdapter的setBackground在getView自定义、ArrayAdapter、getView、setBackground

2023-09-06 13:50:54 作者:Existence °空

我工作的一个ListActivity这将显示一串数字(权值)的。我想更改特定行的背景在ListView。要做到这一点,我已经创建了一个自定义实现一个ArrayAdapter类,并覆盖了getView方法。适配器接受号的列表,并将行的背景用数字20到黄色(为简单起见)。

I'm working on a ListActivity which will display a bunch of numbers (weights). I would like to change the background of a specific row in the ListView. To do this I have created a custom implementation of the ArrayAdapter class and have overridden the getView method. The adapter accepts a list of numbers and sets the background of the row with the number 20 to yellow (for simplicity reasons).

    public class WeightListAdapter extends ArrayAdapter<Integer> {

    private List<Integer> mWeights;

    public WeightListAdapter(Context context, List<Integer> objects) {
        super(context, android.R.layout.simple_list_item_1, objects);

        mWeights = objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = super.getView(position, convertView, parent);

        int itemWeight = mWeights.get(position);
        if (itemWeight == 20) {
            v.setBackgroundColor(Color.YELLOW);
        }
        return v;
    }

}

问题是,不仅与数20行得到黄色背景,但也与数字0(第一个是行)的行,我不知道为什么会这样。

The problem is that not only the row with the number 20 gets the yellow background but also the row with the number 0 (the first row that is) and I'm not sure why this is so.

我做得不对的getView方法(如调用父类方法)?我推理的实现是:所有返回的意见应该是相同的(这就是为什么我打电话的超法)只有视图装修的标准是否应该改变

Am I doing something wrong in the getView method (like calling the super method)? My reasoning for the implementation is: All the returned views should be the same (that's why I'm calling the super method) only the view fitting the if criteria should be changed.

感谢您的帮助!

推荐答案

我做了一些研究,以找出应如何正确完成。我写的这个人打倒了同样的问题,我想这是正确的方式该怎么办呢。请让我知道,如果我错了,或者如果这个解决方案有我没有看到任何瑕疵。

I did a bit of research to find out how this should be done properly. I'm writing this down for the others with the same problem, as I guess this is the proper way how to do it. Please, let me know, if I am mistaken or if this solution has any flaws I'm not seeing.

public class WeightListAdapter extends ArrayAdapter<Integer> {

private static final int TYPE_COUNT = 2;
private static final int TYPE_ITEM_COLORED = 1;
private static final int TYPE_ITEM_NORMAL = 0;

public WeightListAdapter(Context context, List<Integer> objects) {
    super(context, android.R.layout.simple_list_item_1, objects);
}

@Override
public int getViewTypeCount() {
    return TYPE_COUNT;
}

@Override
public int getItemViewType(int position) {

    int item = getItem(position);

    return (item == 30) ? TYPE_ITEM_COLORED : TYPE_ITEM_NORMAL;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = super.getView(position, convertView, parent);
    switch (getItemViewType(position)) {
    case TYPE_ITEM_COLORED:
        v.setBackgroundColor(Color.YELLOW);
        break;
    case TYPE_ITEM_NORMAL:
        break;
    }

    return v;

}

}

显然的基类已经实现确保正确convertView的逻辑被传递给getView方法(基于getViewItemType和getViewTypeCount方法)。

Apparently the base class already implements the logic ensuring the correct convertView is passed to the getView method (based on the getViewItemType and getViewTypeCount methods).