OSDN Git Service

0a6c709889c99358ee8491302c6025b280257677
[android-x86/packages-apps-Eleven.git] / src / com / andrew / apollo / adapters / GenreAdapter.java
1 /*
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.
10  */
11
12 package com.andrew.apollo.adapters;
13
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;
20
21 import com.andrew.apollo.R;
22 import com.andrew.apollo.model.Genre;
23 import com.andrew.apollo.ui.MusicHolder;
24 import com.andrew.apollo.ui.MusicHolder.DataHolder;
25 import com.andrew.apollo.ui.fragments.GenreFragment;
26
27 /**
28  * This {@link ArrayAdapter} is used to display all of the genres on a user's
29  * device for {@link GenreFragment} .
30  * 
31  * @author Andrew Neal (andrewdneal@gmail.com)
32  */
33 public class GenreAdapter extends ArrayAdapter<Genre> {
34
35     /**
36      * Number of views (TextView)
37      */
38     private static final int VIEW_TYPE_COUNT = 1;
39
40     /**
41      * The resource Id of the layout to inflate
42      */
43     private final int mLayoutId;
44
45     /**
46      * Used to cache the genre info
47      */
48     private DataHolder[] mData;
49
50     /**
51      * Constructor of <code>GenreAdapter</code>
52      * 
53      * @param context The {@link Context} to use.
54      * @param layoutId The resource Id of the view to inflate.
55      */
56     public GenreAdapter(final Context context, final int layoutId) {
57         super(context, 0);
58         // Get the layout Id
59         mLayoutId = layoutId;
60     }
61
62     /**
63      * {@inheritDoc}
64      */
65     @Override
66     public View getView(final int position, View convertView, final ViewGroup parent) {
67         // Recycle ViewHolder's items
68         MusicHolder holder;
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);
79         } else {
80             holder = (MusicHolder)convertView.getTag();
81         }
82
83         // Retrieve the data holder
84         final DataHolder dataHolder = mData[position];
85
86         // Set each genre name (line one)
87         holder.mLineOne.get().setText(dataHolder.mLineOne);
88         return convertView;
89     }
90
91     /**
92      * {@inheritDoc}
93      */
94     @Override
95     public boolean hasStableIds() {
96         return true;
97     }
98
99     /**
100      * {@inheritDoc}
101      */
102     @Override
103     public int getViewTypeCount() {
104         return VIEW_TYPE_COUNT;
105     }
106
107     /**
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
110      * called.
111      */
112     public void buildCache() {
113         mData = new DataHolder[getCount()];
114         for (int i = 0; i < getCount(); i++) {
115             // Build the artist
116             final Genre genre = getItem(i);
117
118             // Build the data holder
119             mData[i] = new DataHolder();
120             // Genre Id
121             mData[i].mItemId = genre.mGenreId;
122             // Genre names (line one)
123             mData[i].mLineOne = genre.mGenreName;
124         }
125     }
126
127     /**
128      * Method that unloads and clears the items in the adapter
129      */
130     public void unload() {
131         clear();
132         mData = null;
133     }
134
135 }