1/*
2 * Copyright (C) 2008 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
17package android.widget.listview;
18
19import com.android.frameworks.coretests.R;
20
21import android.app.ListActivity;
22import android.content.Context;
23import android.os.Bundle;
24import android.view.Menu;
25import android.view.MenuItem;
26import android.view.View;
27import android.widget.ArrayAdapter;
28
29
30/**
31 * Tests using an empty view with a list */
32public class ListWithEmptyView extends ListActivity {
33
34    private class CarefulAdapter<T> extends ArrayAdapter<T> {
35
36        public CarefulAdapter(Context context, int textViewResourceId) {
37            super(context, textViewResourceId);
38            // TODO Auto-generated constructor stub
39        }
40
41        @Override
42        public long getItemId(int position) {
43            if (position <  0 || position >= this.getCount()) {
44                throw new ArrayIndexOutOfBoundsException();
45            }
46            return super.getItemId(position);
47        }
48
49
50    }
51
52    public static final int MENU_ADD = Menu.FIRST + 1;
53    public static final int MENU_REMOVE = Menu.FIRST + 2;
54
55    private CarefulAdapter<String> mAdapter;
56
57    private int mNextItem = 0;
58
59    private View mEmptyView;
60
61    @Override
62    public void onCreate(Bundle savedInstanceState) {
63        super.onCreate(savedInstanceState);
64        mAdapter = new CarefulAdapter<String>(this,
65                android.R.layout.simple_list_item_1);
66        setContentView(R.layout.list_with_empty_view);
67        setListAdapter(mAdapter);
68
69        mEmptyView = findViewById(R.id.empty);
70        getListView().setEmptyView(mEmptyView);
71    }
72
73    @Override
74    public boolean onCreateOptionsMenu(Menu menu) {
75        super.onCreateOptionsMenu(menu);
76        menu.add(0, MENU_ADD, 0, R.string.menu_add)
77                .setIcon(android.R.drawable.ic_menu_add);
78        menu.add(0, MENU_REMOVE, 0, R.string.menu_remove)
79                .setIcon(android.R.drawable.ic_menu_delete);
80        return true;
81    }
82
83    @Override
84    public boolean onOptionsItemSelected(MenuItem item) {
85        switch (item.getItemId()) {
86            case MENU_ADD:
87                String str = "Item + " + mNextItem++;
88                mAdapter.add(str);
89                return true;
90            case MENU_REMOVE:
91                if (mAdapter.getCount() > 0) {
92                    mAdapter.remove(mAdapter.getItem(0));
93                }
94                return true;
95        }
96
97        return super.onOptionsItemSelected(item);
98    }
99
100    public View getEmptyView() {
101        return mEmptyView;
102    }
103
104}
105