Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Complex example of ListView getItemViewType()

I have a ListView that is complex in that there are possible scenarios for 4 view types e.g. { book, movie, music cd, concert }

Currently, to decide on what object to inflate etc, I get the item that relates to the list row. I will then do a switch() on the type field of the object.

This has left me with a lot of code and heavy lifting in the getView().

Am I correct in saying that getItemViewType() is used to return the correct view type for the current item?

Where would I find an example of a ListAdapter that uses this approach? Note: this is in an attempt to smooth out my ListView that is quite sluggish now.

Thanks

EDIT: My Adapter

public class MyListAdapter extends BaseAdapter implements ListAdapter {

    private Context ctx;
    private int layoutResourceId;
    private ArrayList<ItemType> items;

    public MyListAdapter(Context context, ArrayList<ItemType> data) {

        this.layoutResourceId = R.layout.timeline_item_row;
        this.ctx = context;
        this.setitems(data);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View row = convertView;
        TimelineHolder holder = null;
        ItemType transaction = getitems().get(position);

        if (row == null) {

            LayoutInflater inflater = ((Activity) ctx).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new TimelineHolder();
            holder.imgActivityIcon = (ImageView)row.findViewById(R.id.imgActivityImageIcon);
            holder.imgActivityLabel = (ImageView)row.findViewById(R.id.imgActivityImageLabel);
            holder.lblMerchantName = (TextView)row.findViewById(R.id.lblMerchantName);
            holder.lblLocation = (TextView)row.findViewById(R.id.lblLocation);
            holder.lblActivityType = (TextView)row.findViewById(R.id.lblActivityType);
            holder.lblActivityLabelUpper = (TextView)row.findViewById(R.id.lblActivityTypeLabel);
            holder.lblTimeSinceActivity = (TextView)row.findViewById(R.id.lblTimeSinceActivity);
            holder.lblActivityAmount = (TextView)row.findViewById(R.id.lblActivityAmount);

            row.setTag(holder);

        }
        else {

            holder = (TimelineHolder) row.getTag();
        }

        // Get/Populate common values here

        String address = "";

        Date hostTime = Helper.parse8601UTC(transaction.getCurrentHostTime());
        Date transactionTime = Helper.parse8601UTC(transaction.getHostTransactionTime());
        String merchantName = transaction.getMerchant().getName();

        holder.lblMerchantName.setText(merchantName);
        holder.lblLocation.setText(address);
        holder.lblTimeSinceActivity.setText(Helper.timeSince(transactionTime, hostTime));
        holder.lblActivityAmount.setText(Helper.doubleToCurrency(transaction.getAmount(), transaction.getCurrency()));

        switch (transaction.getTransactionTypeId()) {

        case TYPE_ONE:

            holder.imgActivityIcon.setImageResource(R.drawable.redemmedicon_2x);
            holder.imgActivityLabel.setImageResource(R.drawable.earned_flag);
            holder.lblActivityType.setText(R.string.Loyalty_Earned);
            holder.lblActivityLabelUpper.setText(R.string.EARNED);
            holder.lblActivityAmount.setText(transaction.getResponsePoints() + " pts");

            break;

        case TYPE_TWO:

            holder.imgActivityIcon.setImageResource(R.drawable.prepay_icon_2x);
            holder.imgActivityLabel.setImageResource(R.drawable.earned_flag);
            holder.lblActivityType.setText(R.string.Prepay_Redeem_Refund);
            holder.lblActivityLabelUpper.setText(R.string.REDEEMED);

            break;

        case TYPE_THREE:

            holder.imgActivityIcon.setImageResource(R.drawable.prepay_icon_2x);
            holder.imgActivityLabel.setImageResource(R.drawable.earned_flag);
            holder.lblActivityLabelUpper.setText(R.string.REFUND);
            holder.lblActivityType.setText(R.string.Prepay_Purchase_Refund);

            break;
        case TYPE_FOUR:

            holder.imgActivityIcon.setImageResource(R.drawable.prepay_icon_2x);
            holder.imgActivityLabel.setImageResource(R.drawable.earned_flag);
            holder.lblActivityLabelUpper.setText(R.string.REFUND);
            holder.lblActivityType.setText(R.string.Prepay_Deposit_Refund);

            break;

        case TYPE_FIVE:

            holder.imgActivityIcon.setImageResource(R.drawable.reward_earned_icon_2x);
            holder.imgActivityLabel.setImageResource(R.drawable.earned_flag);
            holder.lblActivityType.setText(R.string.Reward_Redeemed);
            holder.lblActivityLabelUpper.setText(R.string.REDEEMED);
            holder.lblActivityAmount.setText("");

            break;

        case TYPE_SIX:

            holder.imgActivityIcon
                    .setImageResource(R.drawable.reward_earned_icon_2x);
            holder.imgActivityLabel.setImageResource(R.drawable.redeemed_flag);
            holder.lblActivityType.setText(R.string.Points_Redeemed);
            holder.lblActivityLabelUpper.setText(R.string.REDEEMED);
            holder.lblActivityAmount.setText(transaction.getResponsePoints()
                    + " pts");

            break;

        case TYPE_SEVEN:

            holder.imgActivityIcon.setImageResource(R.drawable.stamp_2x);
            holder.imgActivityLabel.setImageResource(R.drawable.redeemed_flag);
            holder.lblActivityType.setText(R.string.Checked_In);
            holder.lblActivityLabelUpper.setText(R.string.CHECK_IN);
            holder.lblActivityAmount.setText("");

            break;

        case TYPE_EIGHT:

            holder.imgActivityIcon.setImageResource(R.drawable.shared_icon_2x);
            holder.imgActivityLabel.setImageResource(R.drawable.redeemed_flag);
            holder.lblActivityType.setText(R.string.Shared_Coupon);
            holder.lblActivityLabelUpper.setText(R.string.SHARED);
            holder.lblActivityAmount.setText("");

            break;

        case TYPE_NINE:

            holder.imgActivityIcon.setImageResource(R.drawable.prepay_icon_2x);
            holder.imgActivityLabel.setImageResource(R.drawable.earned_flag);
            holder.lblActivityType.setText(R.string.Prepay_Deposit);
            holder.lblActivityLabelUpper.setText(R.string.DEPOSIT);

            break;

        case TYPE_TEN:

            holder.imgActivityIcon.setImageResource(R.drawable.prepay_icon_2x);
            holder.imgActivityLabel.setImageResource(R.drawable.redeemed_flag);
            holder.lblActivityType.setText(R.string.Prepay_Purchase);
            holder.lblActivityLabelUpper.setText(R.string.PURCHASE);

            break;

        case TYPE_ELEVEN:

            holder.imgActivityIcon.setImageResource(R.drawable.prepay_icon_2x);
            holder.imgActivityLabel.setImageResource(R.drawable.earned_flag);
            holder.lblActivityType.setText(R.string.Prepay_Redeemed);
            holder.lblActivityLabelUpper.setText(R.string.REDEEMED);

            break;

        case TYPE_TWELVE:

            holder.imgActivityIcon.setImageResource(R.drawable.prepay_icon_2x);
            holder.imgActivityLabel.setImageResource(R.drawable.earned_flag);
            holder.lblActivityType.setText(R.string.Refund);
            holder.lblActivityLabelUpper.setText(R.string.REFUND);
            holder.lblActivityAmount.setText("");

            break;

        case TYPE_THIRTEEN:

            holder.imgActivityIcon.setImageResource(R.drawable.prepay_icon_2x);
            holder.imgActivityLabel.setImageResource(R.drawable.earned_flag);
            holder.lblActivityType.setText(R.string.reversal);
            holder.lblActivityLabelUpper.setText(R.string.REVERSAL);
            holder.lblActivityAmount.setText("");

            break;

        case TYPE_FOURTEEN:

            holder.imgActivityIcon.setImageResource(R.drawable.unlocked_2x);
            holder.imgActivityLabel.setImageResource(R.drawable.earned_flag);
            holder.lblActivityType.setText(R.string.Check_In_Reward_Unlocked);
            holder.lblActivityLabelUpper.setText(R.string.UNLOCKED);
            holder.lblActivityAmount.setText("");

            break;

        case TYPE_FIFTEEN:

            holder.imgActivityIcon.setImageResource(R.drawable.reward_earned_icon_2x);
            holder.imgActivityLabel.setImageResource(R.drawable.earned_flag);
            holder.lblActivityType.setText(R.string.Coupon_Received);
            holder.lblActivityLabelUpper.setText(R.string.RECEIVED);
            holder.lblActivityAmount.setText("");

            break;

        default:

            holder.imgActivityIcon.setImageResource(R.drawable.stamp_2x);
            holder.imgActivityLabel.setImageResource(R.drawable.earned_flag);
            holder.lblActivityType.setText("");
            holder.lblActivityLabelUpper.setText("");
            holder.lblActivityAmount.setText("");

            break;
        }

        return row;
    }

    @Override
    public int getCount() {

        return getitems() == null ? 0 : getitems().size();
    }

    @Override
    public Object getItem(int position) {

        return getitems() == null ? null : getitems().get(
                position);
    }

    @Override
    public long getItemId(int position) {

        return layoutResourceId;
    }

    public void addItem(ItemType item, boolean addFirst) {

        if (addFirst) {

            getitems().add(0, item);
        } else {

            getitems().add(item);
        }

        notifyDataSetChanged();
    }

    public ArrayList<ItemType> getitems() {
        return items;
    }

    public void setitems(ArrayList<ItemType> items) {
        this.items = items;
    }

    public int getFirstTransactionId() {

        int result = 0;

        if (items != null && !items.isEmpty()) {

            ItemType txn = items.get(0);
            result = txn.getId();
        }

        return result;
    }

    public int getLastTransactionId() {

        int result = 0;

        if (items != null && !items.isEmpty()) {

            ItemType txn = items.get(items.size() - 1);
            result = txn.getId();
        }

        return result;
    }

    @Override
    public int getViewTypeCount() {

        return 1;
    }

    @Override
    public int getItemViewType(int position) {

        return 0;
    }
}
like image 574
jim Avatar asked Mar 07 '13 16:03

jim


1 Answers

*** Updated hints ****
You aren't implementing getViewTypeCount() and getItemViewType() correctly.

Read the tutorial I provided in links below. I don't want to re-explain them as I'll do a worse job of it. But read below first, because you don't really need that as you only have 1 view type.


Option 1: Create 16-different rows with the correct resource image. Then call getItemViewType(position) inside the getView() method. Do a switch on it, and inflate the correct row based on the view type it returns. (Tutorial will explain this). That way you don't have to keep calling the "setImageResource" call, which is expensive.

Option 2: Based on your view holder, it appears that you only have 1 view and you are simply changing 3 text fields and 2 bitmaps. But each bitmap call is heavy, you need to create a HashMap to cache those icons. With each call, reference the hash map, if the item doesn't exist, create it, add it, and proceed. This is important because each call to setImageResource() is doing a bitmap decode and will take time. That is where you real error is See pseudo code below.

private static HashMap<Integer, Bitmap> cache;
static class BitmapCache {
    public static Bitmap get(Integer id) {
        if (cache.containsKey(id)) {
            return cache.get(id);
        } else {
            Bitmap t = BitmapFactory.decodeResource(getContext(), id);
            cache.put(key, t);
            return t;
        }
    }
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder item = null;
    Object o = getItem(position);

    View mRow = convertView;
    if (mRow == null) {
        LayoutInflater li = LayoutInflater.from(getContext());
        switch (getItemViewType(position)) {
        case VIEW_ONLINE:
            mRow = li.inflate(R.layout.row_online, parent, false);
            break;
        default:
            // default to sortable
            mRow = li.inflate(R.layout.row_offline, parent, false);
            break;
        }

        item = new ViewHolder(mRow);
        mRow.setTag(item);
    } else {
        item = (ViewHolder) mRow.getTag();
    }


    item.icon.setImageBitmap(BitmapCache.get(object.key));

    return mRow;
}

* Old answer with tutorials *
There are several posts on StackOverflow already that I just went through yesterday related to this. (See links below).

  1. Yes, use getItemViewType() to retrieve the correct view type. As I understand it, the real benefit here is in the view optimization during the getView() call. Implementing getItemViewType() allows the getView() type to return the correct cached view.
  2. Use the static view holder pattern for text and image views inside your inflated view. You shouldn't be inflating the view every time, you should be reusing the convertView provided by the getView() call.
  3. Be sure to implement getViewTypeCount()

Here is a tutorial that explains in depth how to perform custom list views including optimizations. http://www.vogella.com/articles/AndroidListView/article.html

Here are two stackoverflow posts which also discuss what you are trying to do:
1. Android Programming tutorials - Tutorial 5
2. Restaurant Program Tutorial

like image 93
Cookster Avatar answered Sep 25 '22 00:09

Cookster