0

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

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

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

1 回答 1

0

过去,当我遇到显示问题时,ListView通常是因为我忘记设置cachColorHint. 有关更多信息,请参阅此帖子

于 2013-01-23T11:18:34.317 回答