简体   繁体   中英

When scroll in listview selected item background colr disappear?

I am implementing a list . There are number of items in it, when i select a item from list set a color for list item. when i scroll the list view it disappear.

what may be reason ? Code for adapter is given as below:

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

In the past when I have display issues with a ListView it's usually because I forgot to set the cachColorHint . See this post for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM