新手 看了网上很多资料后 学习写的一个listview
来源:互联网 发布:比较好的vpn 知乎 编辑:程序博客网 时间:2024/05/17 06:57
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package
wx.android.test.listviewbutton;
import
java.util.ArrayList;
import
java.util.HashMap;
import
android.app.Activity;
import
android.app.AlertDialog;
import
android.content.Context;
import
android.content.DialogInterface;
import
android.os.Bundle;
import
android.util.Log;
import
android.view.LayoutInflater;
import
android.view.View;
import
android.view.ViewGroup;
import
android.view.View.OnClickListener;
import
android.widget.AdapterView;
import
android.widget.BaseAdapter;
import
android.widget.Button;
import
android.widget.ImageView;
import
android.widget.ListView;
import
android.widget.TextView;
import
android.widget.AdapterView.OnItemClickListener;
public
class
ListViewButton
extends
Activity {
/** Called when the activity is first created. */
ArrayList<HashMap<String, Object>> listitem ;
private
ListView hh;
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
hh=(ListView)findViewById(R.id.lv);
MyAdapter mAdapter =
new
MyAdapter(
this
);
hh.setAdapter(mAdapter);
hh.setOnItemClickListener(
new
OnItemClickListener(){
@Override
public
void
onItemClick(AdapterView<?> arg0, View arg1,
int
arg2,
long
arg3) {
// TODO Auto-generated method stub
//setTitle("你点击了第"+arg2+"行");
Log.v(
"MyListViewBase"
,
"你点击了ListView条目"
+ arg2);
}
});
}
private
ArrayList<HashMap<String,Object>> getDate(){
ArrayList<HashMap<String,Object>> listitem =
new
ArrayList<HashMap<String,Object>>();
for
(
int
i=
0
;i<
20
;i++){
HashMap<String,Object> map =
new
HashMap<String,Object>();
map.put(
"ItemTitle"
,
"第"
+i+
"行"
);
map.put(
"ItemText"
,
"这是第"
+i+
"行"
);
map.put(
"ItemImage"
, R.drawable.icon);
listitem.add(map);
}
return
listitem;
}
public
void
showinfo(){
new
AlertDialog.Builder(
this
)
.setTitle(
"Are you sure exit?"
).setCancelable(
false
).setPositiveButton(
"Yes"
,
new
DialogInterface.OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
// TODO Auto-generated method stub
ListViewButton.
this
.finish();
}
}).setNegativeButton(
"No"
,
new
DialogInterface.OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
// TODO Auto-generated method stub
dialog.cancel();
}
}).show();
}
private
class
MyAdapter
extends
BaseAdapter{
private
LayoutInflater mInflater;
public
MyAdapter(Context context){
this
.mInflater=LayoutInflater.from(context);
}
@Override
public
int
getCount() {
// TODO Auto-generated method stub
return
getDate().size();
}
@Override
public
Object getItem(
int
position) {
// TODO Auto-generated method stub
return
null
;
}
@Override
public
long
getItemId(
int
position) {
// TODO Auto-generated method stub
return
0
;
}
@Override
public
View getView(
final
int
position, View convertView, ViewGroup parent) {
ViewHolder hold;
// Log.v("MyListViewBase", "getView " + position + " " + convertView);
if
(convertView==
null
){
convertView =mInflater.inflate(R.layout.item,
null
);
hold=
new
ViewHolder();
hold.title=(TextView)convertView.findViewById(R.id.ItemTitle);
hold.text=(TextView)convertView.findViewById(R.id.ItemText);
hold.image=(ImageView)convertView.findViewById(R.id.ItemImage);
hold.bt=(Button)convertView.findViewById(R.id.ItemButton);
convertView.setTag(hold);
}
else
{
hold=(ViewHolder)convertView.getTag();
}
hold.title.setText(getDate().get(position).get(
"ItemTitle"
).toString());
hold.text.setText(getDate().get(position).get(
"ItemText"
).toString());
hold.image.setBackgroundResource((Integer)getDate().get(position).get(
"ItemImage"
));
hold.bt.setOnClickListener(
new
OnClickListener(){
@Override
public
void
onClick(View v) {
// TODO Auto-generated method stub
showinfo();
//Log.v("MyListBase","你点击了按钮"+position);
}
});
return
convertView;
// TODO Auto-generated method stub
}
}
public
final
class
ViewHolder{
public
ImageView image;
public
TextView title;
public
TextView text;
public
Button bt;
}
}
item.xml
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<RelativeLayout
xmlns:android=
"http://schemas.android.com/apk/res/android"
android:layout_width=
"fill_parent"
android:layout_height=
"fill_parent"
>
<Button
android:layout_alignParentRight=
"true"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:id=
"@+id/ItemButton"
android:text=
"点我"
android:layout_toLeftOf=
"@+id/ItemImage"
/>
<TextView
android:id=
"@+id/ItemTitle"
android:layout_height=
"wrap_content"
android:layout_width=
"fill_parent"
android:textSize=
"20sp"
/>
<TextView
android:id=
"@+id/ItemText"
android:layout_width=
"fill_parent"
android:layout_height=
"wrap_content"
android:layout_below=
"@+id/ItemTitle"
/>
<ImageView
android:id=
"@+id/ItemImage"
android:layout_width=
"fill_parent"
android:layout_height=
"wrap_content"
android:layout_marginLeft=
"138dip"
android:layout_marginRight=
"138dip"
/>
</RelativeLayout>
main.xml
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<LinearLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
android:layout_width=
"fill_parent"
android:layout_height=
"fill_parent"
>
<ListView
android:id=
"@+id/lv"
android:layout_width=
"fill_parent"
android:layout_height=
"fill_parent"
/>
</LinearLayout>
- 新手 看了网上很多资料后 学习写的一个listview
- 转载-个人看了网上很多String常量池的博客,这一篇写得最好
- 最近想写一个邮箱自动验证功能,在网上看了很多,写到自己上面出了很多问题,记录下来给后面的人一个参考
- IOS学习 关于证书的学习,看了后清晰了很多,留下来了
- 自学的过程中遇到的问题。看了很多资料后整理出来比较完整的
- 网上看的一个讲解工作流基本概念的资料
- 看了一个月资料,写一篇论文读书心得
- 看了一个月资料,写一篇论文读书心得
- 看了一个月资料,写一篇论文读书心得
- 看了一个月资料,写一篇论文读书心得
- 看了一个月资料,写一篇论文读书心得
- 看了一个月资料,写一篇论文读书心得
- 看了一个月资料,写一篇论文读书心得
- 看了一个月资料,写一篇论文读书心得
- 看了一个月资料,写一篇论文读书心得
- 看了一个月资料,写一篇论文读书心得
- 看了一个月资料,写一篇论文读书心得
- 看了一个月资料,写一篇论文读书心得
- PL/SQL 运算符
- QMIS项目文档管理系统发布
- 第二次作业参考答案
- A*算法入门
- oracle 总结
- 新手 看了网上很多资料后 学习写的一个listview
- 使uboot支持S3C6410的SD启动
- pl/sql 远程连接orcle服务器问题
- c++经典面试题
- java 中的流
- oralce 记录历史的两样东西 日志 回滚
- 第三次作业参考答案
- getopt(win32) -- 命令行参数解析函数
- PL/SQL复合类型