在listview中滚动选中的项目背景颜色消失了吗?

时间:2013-01-23 10:25:14

标签: android listview scroll

我正在实施一份清单。其中有多个项目,当我从列表中选择项目时,为列表项目设置颜色。当我滚动列表视图时,它会消失。

可能是什么原因? 适配器代码如下:

  public class CustomListViewAdapterProduct extends
        ArrayAdapter<HashMap<String, Object>> 
     {

    Context context;
    public CustomListViewAdapterProduct(Context context, int resourceId,
            ArrayList<HashMap<String, Object>> Strings) {
        super(context, resourceId, Strings);
        this.context = context;
    }

    /* private view holder class */

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {

      convertView = mInflater.inflate(R.layout.list_item_detail, null);
     holder = new ViewHolder();
     holder.txtProduct = (TextView) convertView.findViewById(R.id.product);
     holder.txtCode = (TextView) convertView.findViewById(R.id.code);
     convertView.setTag(holder);
     convertView.setBackgroundResource((position % 2) == 0 ? R.color.light
                            : R.color.dark);

        } else {
            if (position == selectedPosition) {
                convertView.setBackgroundColor(getResources().getColor(
                        R.color.selectedvalues));
            } else {
                convertView
                        .setBackgroundResource((position % 2) == 0 ? R.color.light
                                : R.color.dark);
            }

            holder = (ViewHolder) convertView.getTag();

        }

        holder.txtProduct.setText((this.getItem(position)
                .get("description").toString()));
        holder.txtCode.setText((this.getItem(position).get("quantity")
                .toString()));

        return convertView;
    }
}

1 个答案:

答案 0 :(得分:0)

过去当我显示ListView的问题时,通常是因为我忘了设置cachColorHint。有关详细信息,请参阅此post