aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/cyanogenmod/explorer/adapters/CheckableListAdapter.java
blob: cc432407a33f94baebb8ef7547bba74b40b2f3b6 (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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
 * Copyright (C) 2012 The CyanogenMod Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cyanogenmod.explorer.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cyanogenmod.explorer.R;

import java.util.List;

/**
 * An implementation of {@link ArrayAdapter} for display a list with an optional check.
 */
public class CheckableListAdapter extends ArrayAdapter<CheckableListAdapter.CheckableItem> {

    /**
     * A class that wrap an item with checkable options.
     */
    public static class CheckableItem {
        /**
         * Constructor of <code>CheckableItem</code>.
         *
         * @param label The text of the item
         * @param checkable If the item has a check
         * @param checked If the item is checked
         */
        public CheckableItem(String label, boolean checkable, boolean checked) {
            super();
            this.mLabel = label;
            this.mCheckable = checkable;
            this.mChecked = checked;
        }
        String mLabel;
        boolean mCheckable;
        boolean mChecked;
    }

    /**
     * A class that conforms with the ViewHolder pattern to performance.
     * the list view rendering
     */
    private static class ViewHolder {
        /**
         * @hide
         */
        public ViewHolder() {
            super();
        }
        ImageView mDwCheck;
        TextView mTvTitle;
    }

    //The resource of the item check
    private static final int RESOURCE_ITEM_CHECK = R.id.option_list_item_check;
    //The resource of the item name
    private static final int RESOURCE_ITEM_NAME = R.id.option_list_item_text;

    /**
     * Constructor of <code>CheckableListAdapter</code>.
     *
     * @param context The current context
     * @param items An array of items to add to the current list
     */
    public CheckableListAdapter(
            Context context, List<CheckableListAdapter.CheckableItem> items) {
        super(context, RESOURCE_ITEM_NAME, items);
    }

    /**
     * Method that dispose the elements of the adapter.
     */
    public void dispose() {
        clear();
    }

    /**
     * Method that returns the identifier of the setting.
     *
     * @param position The position of the item
     * @return int The identifier of the setting
     */
    @SuppressWarnings("static-method")
    public int getId(int position) {
        return position;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //Check to reuse view
        View v = convertView;
        if (v == null) {
            //Create the view holder
            LayoutInflater li =
                    (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = li.inflate(R.layout.option_list_item, parent, false);
            ViewHolder viewHolder = new CheckableListAdapter.ViewHolder();
            viewHolder.mTvTitle = (TextView)v.findViewById(RESOURCE_ITEM_NAME);
            viewHolder.mDwCheck = (ImageView)v.findViewById(RESOURCE_ITEM_CHECK);
            v.setTag(viewHolder);
        }

        //Retrieve the item
        CheckableListAdapter.CheckableItem item = getItem(position);

        //Retrieve the view holder and fill the views
        ViewHolder viewHolder = (ViewHolder)v.getTag();
        viewHolder.mTvTitle.setText(item.mLabel);
        viewHolder.mDwCheck.setVisibility(item.mCheckable ? View.VISIBLE : View.GONE);
        if (item.mCheckable) {
            viewHolder.mDwCheck.setSelected(item.mChecked);
        }

        return v;
    }

}