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 android.widget.gridview;
18 
19 import android.util.GridScenario;
20 import android.view.KeyEvent;
21 import android.view.View;
22 import android.view.ViewGroup;
23 import android.widget.BaseAdapter;
24 import android.widget.GridView;
25 import android.widget.ListAdapter;
26 
27 import java.util.ArrayList;
28 
29 /**
30  * A grid with vertical spacing between rows
31  */
32 public class GridDelete extends GridScenario {
33     @Override
init(Params params)34     protected void init(Params params) {
35         params.setStartingSelectionPosition(-1)
36                 .setMustFillScreen(false)
37                 .setNumItems(1001)
38                 .setNumColumns(4)
39                 .setItemScreenSizeFactor(0.20)
40                 .setVerticalSpacing(20);
41     }
42 
43 
44 
45     @Override
createAdapter()46     protected ListAdapter createAdapter() {
47         return new DeleteAdapter(getInitialNumItems());
48     }
49 
50 
51 
52 
53     @Override
onKeyDown(int keyCode, KeyEvent event)54     public boolean onKeyDown(int keyCode, KeyEvent event) {
55         if (keyCode == KeyEvent.KEYCODE_DEL) {
56             GridView g = getGridView();
57             ((DeleteAdapter)g.getAdapter()).deletePosition(g.getSelectedItemPosition());
58             return true;
59         } else {
60             return super.onKeyDown(keyCode, event);
61         }
62     }
63 
64 
65 
66 
67     private class DeleteAdapter extends BaseAdapter {
68 
69         private ArrayList<Integer> mData;
70 
DeleteAdapter(int initialNumItems)71         public DeleteAdapter(int initialNumItems) {
72             super();
73             mData = new ArrayList<Integer>(initialNumItems);
74 
75             int i;
76             for (i=0; i<initialNumItems; ++i) {
77                 mData.add(new Integer(10000 + i));
78             }
79 
80         }
81 
deletePosition(int selectedItemPosition)82         public void deletePosition(int selectedItemPosition) {
83             if (selectedItemPosition >=0 && selectedItemPosition < mData.size()) {
84                 mData.remove(selectedItemPosition);
85                 notifyDataSetChanged();
86             }
87 
88         }
89 
getCount()90         public int getCount() {
91             return mData.size();
92         }
93 
getItem(int position)94         public Object getItem(int position) {
95             return mData.get(position);
96         }
97 
getItemId(int position)98         public long getItemId(int position) {
99             return mData.get(position);
100         }
101 
getView(int position, View convertView, ViewGroup parent)102         public View getView(int position, View convertView, ViewGroup parent) {
103             int desiredHeight = getDesiredItemHeight();
104             return createView(mData.get(position), parent, desiredHeight);
105         }
106     }
107 }
108