您的位置:首页 > 移动开发 > Android开发

关于《Android应用开发揭秘》中早期版本中联系人例子错误的解决办法

2011-08-21 13:59 423 查看
转自:/article/4362647.html

针对于android应用开发揭秘书中的4-5节代码不能工作的补充

首先感谢该篇文章的作者,因为网上到处转载,就不知道原作者了,在这里感谢你对本书的关于与支持。

问题原因:android从2.0之后联系人部分的api发生了较大的变化。

下面是“ 针对于android应用开发揭秘书中的4-5节代码不能工作的补充”中的解决方案。

今天在练习android应用开发揭密书中的例子时遇到了一个问题。例子4-5代码不能工作,并且总是提示不存number这一列。

Caused
by: java.lang.IllegalArgumentException: column 'number' does not exist

由于刚刚开始研究android,很多东西都没搞清楚,所以先上网搜了一下,没想到还有人也是遇到了一样的问题,有人问但是没有人回答,所以觉得得好好研究一下。既然系统报的是找不到number这个列那么,就应该是在query时没有搜出这一列数据。那么首先检查query的参数:

Cursor cur = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);


根据android的开发文档,我们得知这种方式是全列查讯,所以对这个问题来说有两种可能:

1.
目标数据源中没有number这列

2.
在其他代码中出现了问题

再看下面的代码

ListAdapter adapter = new SimpleCursorAdapter(this,android.R.layout.expandable_list_content,  cur,new String[]{PhoneLookup.DISPLAY_NAME,PhoneLookup.NUMBER},new int[]{android.R.id.text1,android.R.id.text2});


这段代码没有什么问题,因为只有有number列那就应该不会出错,所以问题很可能是第一种情况。那么,就用调试器查看一下我们查询出来的这个cursor中列集。

果然,在列集中真的没有number这一列。那么为什么在作者的例子中使用了这样的code呢?我个人认为问题是这样的,主要问题是number与contact的对应关系上。事实上android在2.0之后有出来2.0.1版,这两个版本对contact下的内容进行了一些改进,把number这个属性移动到了另一地方(Uri:
ContactsContract.CommonDataKinds.Phone.CONTENT_URI)。所以,书里的代码不能正常工作,这个结论是我个人的猜测,还没有仔细去比较版本的差别。

下面给出一个我改进过的,能在2.0.1SDK上工作的代码:

1 package example.moonzwu.code;

2

3 import java.util.ArrayList;

4 import java.util.HashMap;

5 import java.util.Map;

6

7 import android.app.Activity;

8 import android.database.Cursor;

9 import android.graphics.Color;

10 import android.os.Bundle;

11 import android.provider.ContactsContract;

12 import android.view.View;

13 import android.widget.AdapterView;

14 import android.widget.LinearLayout;

15 import android.widget.ListAdapter;

16 import android.widget.ListView;

17 import android.widget.SimpleAdapter;

18 import android.widget.Toast;

19 import android.widget.AdapterView.OnItemClickListener;

20 import android.widget.AdapterView.OnItemSelectedListener;

21

22 public class accessContact2 extends Activity {

23 LinearLayout mLinLayout;

24 ListView mLstViw;

25 ArrayList<Map<String, String>> listData;

26

27 static final String NAME = "name";

28 static final String NUMBER = "number";

29

30 /** Called when the activity is first created. */

31 @Override

32 public void onCreate(Bundle savedInstanceState) {

33 super.onCreate(savedInstanceState);

34 mLinLayout = new LinearLayout(this);

35 mLinLayout.setOrientation(LinearLayout.VERTICAL);

36 mLinLayout.setBackgroundColor(Color.BLACK);

37

38 mLstViw = new ListView(this);

39 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(

40 LinearLayout.LayoutParams.FILL_PARENT,

41 LinearLayout.LayoutParams.WRAP_CONTENT);

42 mLstViw.setBackgroundColor(Color.BLACK);

43

44 // add the list view to layout

45 mLinLayout.addView(mLstViw, params);

46 setContentView(mLinLayout);

47

48 listData = new ArrayList<Map<String, String>>();

49

50

51 // read contacts

52 Cursor cur = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,

53 null, null, null, null);

54 startManagingCursor(cur);

55

56 while (cur.moveToNext()) {

57 Map<String, String> mp = new HashMap<String, String>();

58

59 long id = cur.getLong(cur.getColumnIndex("_id"));

60 Cursor pcur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,

61 null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + Long.toString(id),

62 null, null);

63

64 // 处理多个号码的情况

65 String phoneNumbers = "";

66 while (pcur.moveToNext()) {

67 String strPhoneNumber = pcur.getString(pcur.getColumnIndex(

68 ContactsContract.CommonDataKinds.Phone.NUMBER));

69 phoneNumbers += strPhoneNumber + ":";

70 }

71 phoneNumbers += "\n";

72 pcur.close();

73

74 String name = cur.getString(cur.getColumnIndex("display_name"));

75 mp.put(NAME, name);

76 mp.put(NUMBER, phoneNumbers);

77 listData.add(mp);

78 }

79 cur.close();

80

81 // 建立一个适配器去查询数据

82 ListAdapter adapter = new SimpleAdapter(this, listData, android.R.layout.simple_list_item_2,

83 new String[]{NAME, NUMBER},

84 new int[] {android.R.id.text1, android.R.id.text2});

85 mLstViw.setAdapter(adapter);

86

87

88 mLstViw.setOnItemClickListener(new OnItemClickListener() {

89

90 @Override

91 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,

92 long arg3) {

93 // TODO Auto-generated method stub

94 DisplayToast("选中第 " + Integer.toString(arg2+1) + "个");

95 }

96 });

97

98

99 mLstViw.setOnItemSelectedListener(new OnItemSelectedListener() {

100

101 @Override

102 public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,

103 long arg3) {

104 // TODO Auto-generated method stub

105 DisplayToast("滚动到" + Long.toString(arg0.getSelectedItemId()) + "行");

106 }

107

108 @Override

109 public void onNothingSelected(AdapterView<?> arg0) {

110 // TODO Auto-generated method stub

111

112 }

113

114 });

115

116 }

117

118 public void DisplayToast(String str) {

119 Toast.makeText(this, str, Toast.LENGTH_SHORT).show();

120 }

121 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐