zoukankan      html  css  js  c++  java
  • Android读取电话薄中的*

    此文为针对于android应用开发揭秘书中的示例3-2 和4-4代码不能工作的补充。 今天在练习android应用开发揭密书中的例子时遇到了一个问题。例子3-2 和4-4代码不能工作,并且总是提示不存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上工作的代码,我是在2.2版本测试的:
    package com.yarin.android.Examples_04_04;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Map;
    
    import android.app.Activity;
    import android.database.Cursor;
    import android.graphics.Color;
    import android.os.Bundle;
    import android.provider.ContactsContract;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.LinearLayout;
    import android.widget.ListAdapter;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;
    import android.widget.Toast;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.AdapterView.OnItemSelectedListener;
    
    public class Activity01 extends Activity {
    	LinearLayout mLinLayout;
    	ListView mLstViw;
    	ArrayList<Map<String, String>> listData;
    
    	static final String NAME = "name";
    	static final String NUMBER = "number";
    
    	/** Called when the activity is first created. */
    	@Override
    	public void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		mLinLayout = new LinearLayout(this);
    		mLinLayout.setOrientation(LinearLayout.VERTICAL);
    		mLinLayout.setBackgroundColor(Color.BLACK);
    
    		mLstViw = new ListView(this);
    		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
    				LinearLayout.LayoutParams.FILL_PARENT,
    				LinearLayout.LayoutParams.WRAP_CONTENT);
    		mLstViw.setBackgroundColor(Color.BLACK);
    
    		// add the list view to layout
    		mLinLayout.addView(mLstViw, params);
    		setContentView(mLinLayout);
    
    		listData = new ArrayList<Map<String, String>>();
    
    		// read contacts
    		Cursor cur = getContentResolver().query(
    				ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    		startManagingCursor(cur);
    
    		while (cur.moveToNext()) {
    			Map<String, String> mp = new HashMap<String, String>();
    
    			long id = cur.getLong(cur.getColumnIndex("_id"));
    			Cursor pcur = getContentResolver().query(
    					ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
    					null,
    					ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "="
    							+ Long.toString(id), null, null);
    
    			// 处理多个号码的情况
    			String phoneNumbers = "";
    			while (pcur.moveToNext()) {
    				String strPhoneNumber = pcur
    						.getString(pcur
    								.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
    				phoneNumbers += strPhoneNumber + ":";
    			}
    			phoneNumbers += "\n";
    			pcur.close();
    
    			String name = cur.getString(cur.getColumnIndex("display_name"));
    			mp.put(NAME, name);
    			mp.put(NUMBER, phoneNumbers);
    			listData.add(mp);
    		}
    		cur.close();
    
    		// 建立一个适配器去查询数据
    		ListAdapter adapter = new SimpleAdapter(this, listData,
    				android.R.layout.simple_list_item_2, new String[] { NAME,
    						NUMBER }, new int[] { android.R.id.text1,
    						android.R.id.text2 });
    		mLstViw.setAdapter(adapter);
    
    		mLstViw.setOnItemClickListener(new OnItemClickListener() {
    
    			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
    					long arg3) {
    				// TODO Auto-generated method stub
    				DisplayToast("选中第  " + Integer.toString(arg2 + 1) + "个");
    			}
    		});
    
    		mLstViw.setOnItemSelectedListener(new OnItemSelectedListener() {
    
    			public void onItemSelected(AdapterView<?> arg0, View arg1,
    					int arg2, long arg3) {
    				// TODO Auto-generated method stub
    				DisplayToast("滚动到" + Long.toString(arg0.getSelectedItemId())
    						+ "行");
    			}
    
    			public void onNothingSelected(AdapterView<?> arg0) {
    				// TODO Auto-generated method stub
    
    			}
    
    		});
    
    	}
    
    	public void DisplayToast(String str) {
    		Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
    	}
    }
    
    效果如下图:
  • 相关阅读:
    SQL游标应用
    八月随笔
    LINQ TO DATATABLE/DATASET基本操作之-简单查询
    Linux8.7 Linux系统日志
    Linux6.2 压缩扩展
    [转]Linux系统误删数据找回
    Linux9.2 MySQL安装
    Linux9.1 介绍
    来自朋友最近面试的阿里、腾讯、美团等P7岗位面试题
    拿到京东Java社招offer,过来分享面经
  • 原文地址:https://www.cnblogs.com/wanyakun/p/3403229.html
Copyright © 2011-2022 走看看