aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/AttributesGridAdapter.java
blob: fd813399df2d7a4c2c6b40ad1d855bf9747b6d69 (plain)
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
package cgeo.geocaching;

import cgeo.geocaching.enumerations.CacheAttribute;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;

import java.util.List;

public class AttributesGridAdapter extends BaseAdapter {
    private final Context context;
    private final Resources resources;
    private final List<String> attributes;
    private final LayoutInflater inflater;

    public AttributesGridAdapter(final Context context, final Geocache cache) {
        this.context = context;
        resources = context.getResources();
        attributes = cache.getAttributes();
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return attributes.size();
    }

    @Override
    public Object getItem(final int position) {
        return attributes.get(position);
    }

    @Override
    public long getItemId(final int position) {
        return 0;
    }

    @Override
    public View getView(final int position, final View convertView, final ViewGroup parent) {
        final FrameLayout attributeLayout;
        if (convertView == null) {
            attributeLayout = (FrameLayout) inflater.inflate(R.layout.attribute_image, parent, false);
        } else {
            attributeLayout = (FrameLayout) convertView;
        }

        drawAttribute(attributeLayout, attributes.get(position));
        return attributeLayout;
    }

    private void drawAttribute(final FrameLayout attributeLayout, final String attributeName) {
        final ImageView imageView = (ImageView) attributeLayout.getChildAt(0);

        final boolean strikeThrough = !CacheAttribute.isEnabled(attributeName);
        final CacheAttribute attrib = CacheAttribute.getByRawName(CacheAttribute.trimAttributeName(attributeName));
        if (attrib != null) {
            Drawable drawable = resources.getDrawable(attrib.drawableId);
            imageView.setImageDrawable(drawable);
            if (strikeThrough) {
                // generate strike through image with same properties as attribute image
                final ImageView strikeThroughImage = new ImageView(context);
                strikeThroughImage.setLayoutParams(imageView.getLayoutParams());
                drawable = resources.getDrawable(R.drawable.attribute__strikethru);
                strikeThroughImage.setImageDrawable(drawable);
                attributeLayout.addView(strikeThroughImage);
            }
        } else {
            imageView.setImageDrawable(resources.getDrawable(R.drawable.attribute_unknown));
        }
    }

}