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.view.LayoutInflater;
16 import android.view.View;
17 import android.view.ViewGroup;
18 import android.widget.ArrayAdapter;
20 import com.cyngn.eleven.model.Song;
21 import com.cyngn.eleven.ui.MusicHolder;
22 import com.cyngn.eleven.ui.MusicHolder.DataHolder;
23 import com.cyngn.eleven.ui.fragments.QueueFragment;
24 import com.cyngn.eleven.ui.fragments.SongFragment;
25 import com.cyngn.eleven.utils.MusicUtils;
28 * This {@link ArrayAdapter} is used to display all of the songs on a user's
29 * device for {@link SongFragment}. It is also used to show the queue in
30 * {@link QueueFragment}.
32 * @author Andrew Neal (andrewdneal@gmail.com)
34 public class SongAdapter extends ArrayAdapter<Song> {
37 * Number of views (TextView)
39 private static final int VIEW_TYPE_COUNT = 1;
42 * The resource Id of the layout to inflate
44 private final int mLayoutId;
47 * Used to cache the song info
49 private DataHolder[] mData;
52 * Constructor of <code>SongAdapter</code>
54 * @param context The {@link Context} to use.
55 * @param layoutId The resource Id of the view to inflate.
57 public SongAdapter(final Context context, final int layoutId) {
67 public View getView(final int position, View convertView, final ViewGroup parent) {
68 // Recycle ViewHolder's items
70 if (convertView == null) {
71 convertView = LayoutInflater.from(getContext()).inflate(mLayoutId, parent, false);
72 holder = new MusicHolder(convertView);
73 // Hide the third line of text
74 holder.mLineThree.get().setVisibility(View.GONE);
75 convertView.setTag(holder);
77 holder = (MusicHolder)convertView.getTag();
80 // Retrieve the data holder
81 final DataHolder dataHolder = mData[position];
83 // Set each song name (line one)
84 holder.mLineOne.get().setText(dataHolder.mLineOne);
85 // Set the song duration (line one, right)
86 holder.mLineOneRight.get().setText(dataHolder.mLineOneRight);
87 // Set the album name (line two)
88 holder.mLineTwo.get().setText(dataHolder.mLineTwo);
96 public boolean hasStableIds() {
104 public int getViewTypeCount() {
105 return VIEW_TYPE_COUNT;
109 * Method used to cache the data used to populate the list or grid. The idea
110 * is to cache everything before {@code #getView(int, View, ViewGroup)} is
113 public void buildCache() {
114 mData = new DataHolder[getCount()];
115 for (int i = 0; i < getCount(); i++) {
117 final Song song = getItem(i);
119 // Build the data holder
120 mData[i] = new DataHolder();
122 mData[i].mItemId = song.mSongId;
123 // Song names (line one)
124 mData[i].mLineOne = song.mSongName;
125 // Song duration (line one, right)
126 mData[i].mLineOneRight = MusicUtils.makeTimeString(getContext(), song.mDuration);
127 // Album names (line two)
128 mData[i].mLineTwo = song.mAlbumName;
133 * Method that unloads and clears the items in the adapter
135 public void unload() {