1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
package com.turvy.organicreaction.adapters;
import android.content.Context;
import android.graphics.Color;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.turvy.organicreaction.R;
import com.turvy.organicreaction.activities.MainActivity;
import java.util.ArrayList;
import java.util.HashMap;
/**
* Adapter qui permet d'afficher le menu principal
*
* @author Florent
*/
public class MainListAdapter extends BaseAdapter {
static final String TITLE = "title";
static final String ICON = "icon";
private ArrayList<HashMap<String, String>> listItem;
private final Context context;
public MainListAdapter(Context context,
ArrayList<HashMap<String, String>> listItem) {
super();
this.context = context;
this.listItem = listItem;
}
@Override
public int getCount() {
return listItem.size();
}
@Override
public HashMap<String, String> getItem(int position) {
return listItem.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public static class ViewHolder {
@SuppressWarnings("unchecked")
public static <T extends View> T get(View view, int id) {
SparseArray<View> viewHolder = (SparseArray<View>) view.getTag();
if (viewHolder == null) {
viewHolder = new SparseArray<View>();
view.setTag(viewHolder);
}
View childView = viewHolder.get(id);
if (childView == null) {
childView = view.findViewById(id);
viewHolder.put(id, childView);
}
return (T) childView;
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(
R.layout.main_item, parent, false);
}
TextView title = ViewHolder.get(convertView, R.id.title);
ImageView icon = ViewHolder.get(convertView, R.id.icon);
HashMap<String, String> item = getItem(position);
title.setText(item.get(TITLE));
int iconID = convertView
.getContext()
.getApplicationContext()
.getResources()
.getIdentifier(
item.get(ICON),
"drawable",
convertView.getContext().getApplicationContext()
.getPackageName());
icon.setImageResource(iconID);
if (!MainActivity.isPremium) {
if (position == 5 || position == 8) {
icon.setBackgroundColor(Color.GRAY);
}
}
return convertView;
}
} |
Partager