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