1 /*
2  * Copyright (C) 2007 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.apis.view;
18 
19 import com.example.android.apis.R;
20 
21 import android.app.ListActivity;
22 import android.content.Context;
23 import android.os.Bundle;
24 import android.view.View;
25 import android.view.ViewGroup;
26 import android.view.ViewGroup.LayoutParams;
27 import android.widget.BaseAdapter;
28 import android.widget.Button;
29 import android.widget.ImageView;
30 import android.widget.AbsListView;
31 
32 import java.util.ArrayList;
33 
34 
35 /**
36  * A list view that demonstrates the use of setEmptyView. This example alos uses
37  * a custom layout file that adds some extra buttons to the screen.
38  */
39 public class List8 extends ListActivity {
40 
41     PhotoAdapter mAdapter;
42 
43     @Override
onCreate(Bundle savedInstanceState)44     public void onCreate(Bundle savedInstanceState) {
45         super.onCreate(savedInstanceState);
46 
47         // Use a custom layout file
48         setContentView(R.layout.list_8);
49 
50         // Tell the list view which view to display when the list is empty
51         getListView().setEmptyView(findViewById(R.id.empty));
52 
53         // Set up our adapter
54         mAdapter = new PhotoAdapter(this);
55         setListAdapter(mAdapter);
56 
57         // Wire up the clear button to remove all photos
58         Button clear = (Button) findViewById(R.id.clear);
59         clear.setOnClickListener(new View.OnClickListener() {
60 
61             public void onClick(View v) {
62                 mAdapter.clearPhotos();
63             } });
64 
65         // Wire up the add button to add a new photo
66         Button add = (Button) findViewById(R.id.add);
67         add.setOnClickListener(new View.OnClickListener() {
68 
69             public void onClick(View v) {
70                 mAdapter.addPhotos();
71             } });
72     }
73 
74     /**
75      * A simple adapter which maintains an ArrayList of photo resource Ids.
76      * Each photo is displayed as an image. This adapter supports clearing the
77      * list of photos and adding a new photo.
78      *
79      */
80     public class PhotoAdapter extends BaseAdapter {
81 
82         private Integer[] mPhotoPool = {
83                 R.drawable.sample_thumb_0, R.drawable.sample_thumb_1, R.drawable.sample_thumb_2,
84                 R.drawable.sample_thumb_3, R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
85                 R.drawable.sample_thumb_6, R.drawable.sample_thumb_7};
86 
87         private ArrayList<Integer> mPhotos = new ArrayList<Integer>();
88 
PhotoAdapter(Context c)89         public PhotoAdapter(Context c) {
90             mContext = c;
91         }
92 
getCount()93         public int getCount() {
94             return mPhotos.size();
95         }
96 
getItem(int position)97         public Object getItem(int position) {
98             return position;
99         }
100 
getItemId(int position)101         public long getItemId(int position) {
102             return position;
103         }
104 
getView(int position, View convertView, ViewGroup parent)105         public View getView(int position, View convertView, ViewGroup parent) {
106             // Make an ImageView to show a photo
107             ImageView i = new ImageView(mContext);
108 
109             i.setImageResource(mPhotos.get(position));
110             i.setAdjustViewBounds(true);
111             i.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.WRAP_CONTENT,
112                     LayoutParams.WRAP_CONTENT));
113             // Give it a nice background
114             i.setBackgroundResource(R.drawable.picture_frame);
115             return i;
116         }
117 
118         private Context mContext;
119 
clearPhotos()120         public void clearPhotos() {
121             mPhotos.clear();
122             notifyDataSetChanged();
123         }
124 
addPhotos()125         public void addPhotos() {
126             int whichPhoto = (int)Math.round(Math.random() * (mPhotoPool.length - 1));
127             int newPhoto = mPhotoPool[whichPhoto];
128             mPhotos.add(newPhoto);
129             notifyDataSetChanged();
130         }
131 
132     }
133 }
134