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 17package android.widget.focus; 18 19import android.app.ListActivity; 20import android.content.Context; 21import android.os.Bundle; 22import android.view.LayoutInflater; 23import android.view.View; 24import android.view.ViewGroup; 25import android.widget.AbsListView; 26import android.widget.BaseAdapter; 27import android.widget.Button; 28import android.widget.TextView; 29 30import com.google.android.collect.Lists; 31import com.android.frameworks.coretests.R; 32 33import java.util.List; 34 35public class ListWithFooterViewAndNewLabels extends ListActivity { 36 37 private MyAdapter mMyAdapter; 38 39 @Override 40 protected void onCreate(Bundle icicle) { 41 super.onCreate(icicle); 42 43 setContentView(R.layout.list_with_button_above); 44 45 Button footerButton = new Button(this); 46 footerButton.setText("hi"); 47 footerButton.setLayoutParams( 48 new AbsListView.LayoutParams( 49 ViewGroup.LayoutParams.WRAP_CONTENT, 50 ViewGroup.LayoutParams.WRAP_CONTENT)); 51 getListView().addFooterView(footerButton); 52 53 mMyAdapter = new MyAdapter(this); 54 setListAdapter(mMyAdapter); 55 56 // not in list 57 Button topButton = (Button) findViewById(R.id.button); 58 topButton.setText("click to add new item"); 59 topButton.setOnClickListener(new View.OnClickListener() { 60 61 public void onClick(View v) { 62 mMyAdapter.addLabel("yo"); 63 } 64 }); 65 66 mMyAdapter.addLabel("first"); 67 } 68 69 /** 70 * An adapter that can take new string labels. 71 */ 72 static class MyAdapter extends BaseAdapter { 73 74 private final Context mContext; 75 private List<String> mLabels = Lists.newArrayList(); 76 77 public MyAdapter(Context context) { 78 mContext = context; 79 } 80 81 public int getCount() { 82 return mLabels.size(); 83 } 84 85 public Object getItem(int position) { 86 return mLabels.get(position); 87 } 88 89 public long getItemId(int position) { 90 return position; 91 } 92 93 public View getView(int position, View convertView, ViewGroup parent) { 94 String label = mLabels.get(position); 95 96 LayoutInflater inflater = (LayoutInflater) 97 mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 98 99 TextView tv = (TextView) inflater.inflate( 100 android.R.layout.simple_list_item_1, 101 null); 102 tv.setText(label); 103 return tv; 104 } 105 106 public void addLabel(String s) { 107 mLabels.add(s + mLabels.size()); 108 notifyDataSetChanged(); 109 } 110 } 111} 112