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
|
public class Content_Provider_Activity extends Activity {
private ListView listView;
private ListView listView2;
private Button button;
/*
* 1.靜態常量的定義
* 1.1 各個參數用於選擇的目標,若是在數組中沒有某個參數,則遊標不會定義(即找到那個參數)
* 1.2 從定義能夠看出,聯繫人的全部信息不是定義在一張表之中
* 1.3 爲了操做的方便和可行,分開定義查找的目標
*/
public static final String[] COLUMNS={Contacts._ID,
Contacts.DISPLAY_NAME};
private static final String[] COLUMNS2={Contacts.DISPLAY_NAME,Contacts._ID};
private static final String[] COLUMNS3={Phone.NUMBER};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_content__provider_);
listView=(ListView)findViewById(R.id.listview);
listView2=(ListView)findViewById(R.id.listview2);
button=(Button)findViewById(R.id.button);
button.setText("點擊獲取聯繫人姓名和手機號碼");
button.setOnClickListener(new MyButton());
@SuppressWarnings("deprecation")
<a title="Cursor" href="
http://www.android-study.com/jichuzhishi/547.html">Cursor</a> cursor=managedQuery(Contacts.CONTENT_URI, COLUMNS, null, null, null);
//獲取ID所對應的索引值---列索引
int idIndex=cursor.getColumnIndex(COLUMNS[0]);
//獲取NAME所對應的索引值---列索引
int displayNameIndex=cursor.getColumnIndex(COLUMNS[1]);
List<String> items=new ArrayList<String>();
for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
int id=cursor.getInt(idIndex);
String name=cursor.getString(displayNameIndex);
items.add("id="+id+"\t 姓名= "+name);
}
ArrayAdapter<String> adapter=new ArrayAdapter<String>(Content_Provider_Activity.this,
R.layout.list_item,items);
listView.setAdapter(adapter);
}
public class MyButton implements OnClickListener{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.button:
getNameAndPhone();
break;
default:
break;
}
}
}
public void getNameAndPhone(){
@SuppressWarnings("deprecation")
<a title="Cursor" href="
http://www.android-study.com/jichuzhishi/547.html">Cursor</a> cursor=managedQuery(Contacts.CONTENT_URI, COLUMNS2, null, null, null);
int displayNameIndex=cursor.getColumnIndex(COLUMNS2[0]);
List<String> items=new ArrayList<String>();
for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
String name=cursor.getString(displayNameIndex);
int id=cursor.getInt(cursor.getColumnIndex(COLUMNS2[1]));
@SuppressWarnings("deprecation")
<a title="Cursor" href="
http://www.android-study.com/jichuzhishi/547.html">Cursor</a> phone=managedQuery(Phone.CONTENT_URI, null, Phone.CONTACT_ID+" = "+id,
null, null);
while(phone.moveToNext()){
String phoneNumber=phone.getString(phone.getColumnIndex(COLUMNS3[0]));
items.add("姓名:"+name+"\t 手機:"+phoneNumber);
}
}
ArrayAdapter<String> adapter=new ArrayAdapter<String>(Content_Provider_Activity.this,
R.layout.list_item ,items);
listView2.setAdapter(adapter);
}
} html 轉載自:http://www.android-study.com/jichuzhishi/547.html android |