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
|
public
class
MainActivity
extends
ListActivity
{
@Override
protected
void
onCreate(Bundle savedInstanceState)
{
super
.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
ContentResolver cr = getContentResolver();
//得到contentprovider对象
final
Cursor c = cr.query(Phones.CONTENT_URI,
null
,
null
,
null
,
null
);
//得到表的游标对象
startManagingCursor(c);
setListAdapter(
new
CursorAdapter(
this
,c)
{
@Override
//找到行布局
public
View newView(Context context, Cursor cursor, ViewGroup viewGroup)
{
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(android.R.layout.simple_list_item_2,
null
);
return
layout;
}
@Override
//获取和设置行布局控件
public
void
bindView(View view, Context context, Cursor cursor)
{
TextView tvName = (TextView) view.findViewById(android.R.id.text1);
TextView tvPhone = (TextView) view.findViewById(android.R.id.text2);
String name = c.getString(c.getColumnIndex(
"name"
));
String phone = c.getString(c.getColumnIndex(
"number"
));
tvName.setText(name);
tvPhone.setText(phone);
}
});
}
@Override
public
boolean
onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.activity_main, menu);
return
true
;
}
}
|
本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1216878,如需转载请自行联系原作者