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