DoneBar / src / com.example.android.donebar /

MainActivity.java

1
/*
2
* Copyright 2013 The Android Open Source Project
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
*     http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*/
16
 
17
package com.example.android.donebar;
18
 
19
import android.app.Activity;
20
import android.content.Intent;
21
import android.content.res.Resources;
22
import android.os.Bundle;
23
import android.view.View;
24
import android.view.ViewGroup;
25
import android.view.ViewTreeObserver;
26
import android.widget.AdapterView;
27
import android.widget.BaseAdapter;
28
import android.widget.GridView;
29
import android.widget.TextView;
30
 
31
/**
32
 * A simple launcher activity offering access to the individual samples in this project.
33
 */
34
public class MainActivity extends Activity implements AdapterView.OnItemClickListener {
35
    private Sample[] mSamples;
36
    private GridView mGridView;
37
 
38
    protected void onCreate(Bundle savedInstanceState) {
39
        super.onCreate(savedInstanceState);
40
        setContentView(R.layout.activity_main);
41
 
42
        // Prepare list of samples in this dashboard.
43
        mSamples = new Sample[]{
44
            new Sample(R.string.donebaractivity_title, R.string.donebaractivity_description,
45
                    DoneBarActivity.class),
46
            new Sample(R.string.donebuttonactivity_title, R.string.donebuttonactivity_description,
47
                    DoneButtonActivity.class),
48
        };
49
 
50
        // Prepare the GridView
51
        mGridView = (GridView) findViewById(android.R.id.list);
52
        mGridView.setAdapter(new SampleAdapter());
53
        mGridView.setOnItemClickListener(this);
54
    }
55
 
56
    @Override
57
    public void onItemClick(AdapterView<?> container, View view, int position, long id) {
58
        startActivity(mSamples[position].intent);
59
    }
60
 
61
    private class SampleAdapter extends BaseAdapter {
62
        @Override
63
        public int getCount() {
64
            return mSamples.length;
65
        }
66
 
67
        @Override
68
        public Object getItem(int position) {
69
            return mSamples[position];
70
        }
71
 
72
        @Override
73
        public long getItemId(int position) {
74
            return mSamples[position].hashCode();
75
        }
76
 
77
        @Override
78
        public View getView(int position, View convertView, ViewGroup container) {
79
            if (convertView == null) {
80
                convertView = getLayoutInflater().inflate(R.layout.sample_dashboard_item,
81
                        container, false);
82
            }
83
 
84
            ((TextView) convertView.findViewById(android.R.id.text1)).setText(
85
                    mSamples[position].titleResId);
86
            ((TextView) convertView.findViewById(android.R.id.text2)).setText(
87
                    mSamples[position].descriptionResId);
88
            return convertView;
89
        }
90
    }
91
 
92
    private class Sample {
93
        int titleResId;
94
        int descriptionResId;
95
        Intent intent;
96
 
97
        private Sample(int titleResId, int descriptionResId, Intent intent) {
98
            this.intent = intent;
99
            this.titleResId = titleResId;
100
            this.descriptionResId = descriptionResId;
101
        }
102
 
103
        private Sample(int titleResId, int descriptionResId,
104
                Class<? extends Activity> activityClass) {
105
            this(titleResId, descriptionResId,
106
                    new Intent(MainActivity.this, activityClass));
107
        }
108
    }
109
}