CustomChoiceList / src / com.example.android.customchoicelist /

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.customchoicelist;
18
 
19
import android.app.ListActivity;
20
import android.os.Bundle;
21
import android.view.View;
22
import android.view.ViewGroup;
23
import android.widget.BaseAdapter;
24
import android.widget.TextView;
25
 
26
/**
27
 * This sample demonstrates how to create custom single- or multi-choice
28
 * {@link android.widget.ListView} UIs. The most interesting bits are in
29
 * the <code>res/layout/</code> directory of this sample.
30
 */
31
public class MainActivity extends ListActivity {
32
    protected void onCreate(Bundle savedInstanceState) {
33
        super.onCreate(savedInstanceState);
34
        setContentView(R.layout.sample_main);
35
        setListAdapter(new MyAdapter());
36
    }
37
 
38
    /**
39
     * A simple array adapter that creates a list of cheeses.
40
     */
41
    private class MyAdapter extends BaseAdapter {
42
        @Override
43
        public int getCount() {
44
            return Cheeses.CHEESES.length;
45
        }
46
 
47
        @Override
48
        public String getItem(int position) {
49
            return Cheeses.CHEESES[position];
50
        }
51
 
52
        @Override
53
        public long getItemId(int position) {
54
            return Cheeses.CHEESES[position].hashCode();
55
        }
56
 
57
        @Override
58
        public View getView(int position, View convertView, ViewGroup container) {
59
            if (convertView == null) {
60
                convertView = getLayoutInflater().inflate(R.layout.list_item, container, false);
61
            }
62
 
63
            ((TextView) convertView.findViewById(android.R.id.text1))
64
                    .setText(getItem(position));
65
            return convertView;
66
        }
67
    }
68
}