2 * Copyright (C) 2012 Andrew Neal Licensed under the Apache License, Version 2.0
3 * (the "License"); you may not use this file except in compliance with the
4 * License. You may obtain a copy of the License at
5 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
6 * or agreed to in writing, software distributed under the License is
7 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
8 * KIND, either express or implied. See the License for the specific language
9 * governing permissions and limitations under the License.
12 package com.cyngn.eleven.adapters;
14 import android.content.Context;
15 import android.util.TypedValue;
16 import android.view.LayoutInflater;
17 import android.view.View;
18 import android.view.ViewGroup;
19 import android.widget.ArrayAdapter;
21 import com.cyngn.eleven.R;
22 import com.cyngn.eleven.model.Playlist;
23 import com.cyngn.eleven.ui.MusicHolder;
24 import com.cyngn.eleven.ui.MusicHolder.DataHolder;
25 import com.cyngn.eleven.ui.fragments.PlaylistFragment;
28 * This {@link ArrayAdapter} is used to display all of the playlists on a user's
29 * device for {@link PlaylistFragment}.
31 * @author Andrew Neal (andrewdneal@gmail.com)
33 public class PlaylistAdapter extends ArrayAdapter<Playlist> {
36 * Number of views (TextView)
38 private static final int VIEW_TYPE_COUNT = 1;
41 * The resource Id of the layout to inflate
43 private final int mLayoutId;
46 * Used to cache the playlist info
48 private DataHolder[] mData;
51 * Constructor of <code>PlaylistAdapter</code>
53 * @param context The {@link Context} to use.
54 * @param layoutId The resource Id of the view to inflate.
56 public PlaylistAdapter(final Context context, final int layoutId) {
66 public View getView(final int position, View convertView, final ViewGroup parent) {
67 // Recycle ViewHolder's items
69 if (convertView == null) {
70 convertView = LayoutInflater.from(getContext()).inflate(mLayoutId, parent, false);
71 holder = new MusicHolder(convertView);
72 // Hide the second and third lines of text
73 holder.mLineTwo.get().setVisibility(View.GONE);
74 holder.mLineThree.get().setVisibility(View.GONE);
75 // Make line one slightly larger
76 holder.mLineOne.get().setTextSize(TypedValue.COMPLEX_UNIT_PX,
77 getContext().getResources().getDimension(R.dimen.text_size_large));
78 convertView.setTag(holder);
80 holder = (MusicHolder)convertView.getTag();
83 // Retrieve the data holder
84 final DataHolder dataHolder = mData[position];
86 // Set each playlist name (line one)
87 holder.mLineOne.get().setText(dataHolder.mLineOne);
95 public boolean hasStableIds() {
103 public int getViewTypeCount() {
104 return VIEW_TYPE_COUNT;
108 * Method used to cache the data used to populate the list or grid. The idea
109 * is to cache everything before {@code #getView(int, View, ViewGroup)} is
112 public void buildCache() {
113 mData = new DataHolder[getCount()];
114 for (int i = 0; i < getCount(); i++) {
116 final Playlist playlist = getItem(i);
118 // Build the data holder
119 mData[i] = new DataHolder();
121 mData[i].mItemId = playlist.mPlaylistId;
122 // Playlist names (line one)
123 mData[i].mLineOne = playlist.mPlaylistName;
128 * Method that unloads and clears the items in the adapter
130 public void unload() {