2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package android.widget.listview;
19 import com.android.frameworks.coretests.R;
21 import android.app.ListActivity;
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.TextView;
32 import java.util.Random;
35 * Exercises change notification in a list
37 public class ListThrasher extends ListActivity implements AdapterView.OnItemSelectedListener {
38 Handler mHandler = new Handler();
39 ThrashListAdapter mAdapter;
40 Random mRandomizer = new Random();
43 Runnable mThrash = new Runnable() {
45 mAdapter.bumpVersion();
46 mHandler.postDelayed(mThrash, 500);
50 private class ThrashListAdapter extends BaseAdapter {
51 private LayoutInflater mInflater;
56 private String[] mTitles = new String[100];
61 private int[] mVersion = new int[100];
63 public ThrashListAdapter(Context context) {
64 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
65 mTitles = new String[100];
66 mVersion = new int[100];
69 for (i = 0; i < 100; i++) {
70 mTitles[i] = "[" + i + "]";
75 public int getCount() {
76 return mTitles.length;
79 public Object getItem(int position) {
83 public long getItemId(int position) {
87 public View getView(int position, View convertView, ViewGroup parent) {
90 if (convertView == null) {
91 view = (TextView) mInflater.inflate(android.R.layout.simple_list_item_1, null);
93 view = (TextView) convertView;
95 view.setText(mTitles[position] + " " + mVersion[position]);
100 public void bumpVersion() {
101 int position = mRandomizer.nextInt(getCount());
102 mVersion[position]++;
103 notifyDataSetChanged();
110 public void onCreate(Bundle icicle) {
111 super.onCreate(icicle);
113 setContentView(R.layout.list_thrasher);
115 mText = (TextView) findViewById(R.id.text);
116 mAdapter = new ThrashListAdapter(this);
117 setListAdapter(mAdapter);
119 mHandler.postDelayed(mThrash, 5000);
121 getListView().setOnItemSelectedListener(this);
124 public void onItemSelected(AdapterView parent, View v, int position, long id) {
125 mText.setText("Position " + position);
128 public void onNothingSelected(AdapterView parent) {
129 mText.setText("Nothing");