-2

I am having issues with views being recycled. I'm using 2 layouts but some of the rows change layouts when I scroll up and down. I've tried other tutorials with little success. Any suggestions?

public class CustomAdapter extends BaseAdapter {

    Date date = new Date();
    private OfferList[] offers;
    private String nameCompare = "";
    LayoutInflater inflater = getLayoutInflater();  


    CustomAdapter (OfferList[] offers, FetchItemsTask fetchItemsTask) { 
         this.offers = offers;
     }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();

            if (getItemViewType(position) == 0) {
                convertView = inflater.inflate(R.layout.list_offer_group, null);
            }
            else {
                nameCompare = offers[position].getName();
                convertView = inflater.inflate(R.layout.list_offer, null);
            }

            holder.name = (TextView) convertView.findViewById(R.id.name);
            holder.color = (TextView) convertView.findViewById(R.id.car_color_stock);
            holder.time = (TextView) convertView.findViewById(R.id.time);
            holder.offerStatus = (TextView) convertView.findViewById(R.id.offer_status);

            convertView.setTag(holder);

        } 
        else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.name.setText(offers[position].getCustomerName());
        holder.carStockColor.setText(offers[position].getVehicleYear() + " " + offers[position].getVehicleMake());
        java.util.Date time1 = new java.util.Date(Long.parseLong(offers[position].getModified()));
        holder.time.setText(time1.toString().substring(0, 11) + " | ");
        holder.offerStatus.setText(offers[position].getDealStatus());
        return convertView;

    }

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

    @Override
    public int getItemViewType(int position) {
        if (nameCompare.equalsIgnoreCase(offers[position].getCustomerName())) {
            return 0;
        }
        else {
            return 1;
        }
    }

    @Override
    public int getCount() {
        return offers.length;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public class ViewHolder {
        public int type;
        TextView name;
        TextView color;
        TextView time;
        TextView offerStatus;
    }
 }
Ahmad
  • 69,608
  • 17
  • 111
  • 137
ono
  • 2,984
  • 9
  • 43
  • 85

1 Answers1

2

You have to implement a different logic. The problem with your code is, that if the convertView is not null, meaning it has been recycled, but it previously inflated a different view, than that, that correlates to your data, it will display the layout of the old recycled view (the other layout and not the layout, that you need for this dataset).

So you just have to check, if the recycled view inflated the layout, that you need for this particular row or if it inflated the other view. If the former is the case, then you can just set your data, if not, then you have to inflate the right view.

Ahmad
  • 69,608
  • 17
  • 111
  • 137
  • yea i was thinking of doing it somehow by inflating only one layout – ono Oct 15 '13 at 02:28
  • How would I check if the recycled view inflated the layout or if it inflated the other view? – ono Oct 15 '13 at 15:10