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 com.android.frameworks.coretests.R;
20 
21 import android.app.Activity;
22 import android.content.Context;
23 import android.os.Bundle;
24 import android.os.Handler;
25 import android.view.View;
26 import android.view.ViewGroup;
27 import android.view.LayoutInflater;
28 import android.widget.AdapterView;
29 import android.widget.BaseAdapter;
30 import android.widget.GridView;
31 import android.widget.TextView;
32 
33 import java.util.Random;
34 
35 /**
36  * Exercises change notification in a list
37  */
38 public class GridThrasher extends Activity implements AdapterView.OnItemSelectedListener
39 {
40     Handler mHandler = new Handler();
41     ThrashListAdapter mAdapter;
42     Random mRandomizer = new Random();
43     TextView mText;
44 
45     Runnable mThrash = new Runnable() {
46         public void run() {
47             mAdapter.bumpVersion();
48             mHandler.postDelayed(mThrash, 500);
49         }
50     };
51 
52     private class ThrashListAdapter extends BaseAdapter {
53         private LayoutInflater mInflater;
54 
55         /**
56          * Our data, part 1.
57          */
58         private String[] mTitles = new String[100];
59 
60         /**
61          * Our data, part 2.
62          */
63         private int[] mVersion = new int[100];
64 
ThrashListAdapter(Context context)65         public ThrashListAdapter(Context context) {
66             mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
67             mTitles = new String[100];
68             mVersion = new int[100];
69 
70             int i;
71             for (i=0; i<100; i++) {
72                 mTitles[i] = "[" + i + "]";
73                 mVersion[i] = 0;
74             }
75         }
76 
getCount()77         public int getCount() {
78             return mTitles.length;
79         }
80 
getItem(int position)81         public Object getItem(int position) {
82             return position;
83         }
84 
getItemId(int position)85         public long getItemId(int position) {
86             return position;
87         }
88 
getView(int position, View convertView, ViewGroup parent)89         public View getView(int position, View convertView, ViewGroup parent) {
90             TextView view;
91 
92             if (convertView == null) {
93                 view = (TextView) mInflater.inflate(android.R.layout.simple_list_item_1, null);
94             } else {
95                 view = (TextView) convertView;
96             }
97             view.setText(mTitles[position] + " " + mVersion[position]);
98             return view;
99         }
100 
101 
bumpVersion()102         public void bumpVersion() {
103             int position = mRandomizer.nextInt(getCount());
104             mVersion[position]++;
105             notifyDataSetChanged();
106         }
107 
108 
109     }
110 
111     @Override
onCreate(Bundle icicle)112     public void onCreate(Bundle icicle)
113     {
114         super.onCreate(icicle);
115 
116         setContentView(R.layout.grid_thrasher);
117 
118         mText = findViewById(R.id.text);
119         mAdapter = new ThrashListAdapter(this);
120         GridView g = findViewById(R.id.grid);
121         g.setAdapter(mAdapter);
122 
123         mHandler.postDelayed(mThrash, 5000);
124 
125         g.setOnItemSelectedListener(this);
126     }
127 
onItemSelected(AdapterView parent, View v, int position, long id)128     public void onItemSelected(AdapterView parent, View v, int position, long id) {
129         mText.setText("Position " + position);
130     }
131 
onNothingSelected(AdapterView parent)132     public void onNothingSelected(AdapterView parent) {
133         mText.setText("Nothing");
134     }
135 }
136