我是初学着,希望大家帮帮忙。
代码如下:
 //Activity01Activity.java
package com.android.demo;import android.app.Activity;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;public class Activity01Activity extends Activity { LinearLayout m_LinearLayout;
ListView m_ListView; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
m_LinearLayout = new LinearLayout(this);
/*
 * 设置布局LinearLayout的属性
 */
m_LinearLayout.setOrientation(LinearLayout.VERTICAL);
m_LinearLayout.setBackgroundColor(android.graphics.Color.BLACK); /*
 * 创建ListVIew
 */
m_ListView = new ListView(this);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
m_ListView.setBackgroundColor(Color.BLACK);
m_LinearLayout.addView(m_ListView, param);
setContentView(m_LinearLayout);
Cursor cursor = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
ListAdapter listAdapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2, cursor, new String[] {
PhoneLookup.DISPLAY_NAME, PhoneLookup.NUMBER },
new int[] {android.R.id.text1, android.R.id.text2 });
m_ListView.setAdapter(listAdapter); m_ListView
.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
DisplayToast("滚动到第"
+ Long.toString(arg0.getSelectedItemId()) + "项"); } @Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub }
});
m_ListView
.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
DisplayToast("选中了第" + Integer.toString(arg2 + 1) + "项"); } });
} private void DisplayToast(String string) {
Toast.makeText(this, string, Toast.LENGTH_SHORT).show(); }
}//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"
    android:orientation="vertical" >    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

</LinearLayout>//AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.demo"
    android:versionCode="1"
    android:versionName="1.0" >    <uses-sdk android:minSdkVersion="14" />    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".Activity01Activity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
</manifest>为什么这里为什么编译过,但是一运行就出错,出错的位置在于
ListAdapter listAdapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2, cursor, new String[] {
PhoneLookup.DISPLAY_NAME, PhoneLookup.NUMBER },
new int[] {android.R.id.text1, android.R.id.text2 });
请谁能说说这个适配器的用法,最后一个参数代表什么,这样写需要改动main.xml的内容么??求好心人解答一下,谢谢!!!

解决方案 »

  1.   

    第一个是当前类。
    第二个是要显示的布局
    第三个是查询后返回的Cursor
    第四个是要显示的字段
    最后一个是数据要放到的地方。android.R.id.text1, android.R.id.text2 是android.R.layout.simple_list_item_2里的两个TextView.和main.xml无关
      

  2.   

    11-18 05:19:59.570: E/AndroidRuntime(2003): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.demo/com.android.demo.Activity01Activity}: java.lang.IllegalArgumentException: column 'number' does not exist
    出现这样的错。。
      

  3.   

    没munber这一列,你再检查下你的sql语句
      

  4.   

    我的sdk版本有点低。没有那个android.provider.ContactsContract.PhoneLookup
    最后我试了android.provider.MediaStore;(放音乐数据的包)。结果是可以的。
    以下是我的代码:
    package com.qhl.csdn;import android.app.Activity;
    import android.database.Cursor;
    import android.graphics.Color;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.LinearLayout;
    import android.widget.ListAdapter;
    import android.widget.ListView;
    import android.widget.SimpleCursorAdapter;
    import android.widget.Toast;
    import android.provider.MediaStore;public class csdn extends Activity {LinearLayout m_LinearLayout;
    ListView m_ListView;@Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    m_LinearLayout = new LinearLayout(this);
    /*
    * 设置布局LinearLayout的属性
    */
    m_LinearLayout.setOrientation(LinearLayout.VERTICAL);
    m_LinearLayout.setBackgroundColor(android.graphics.Color.BLACK);/*
    * 创建ListVIew
    */
    m_ListView = new ListView(this);
    LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.FILL_PARENT,
    LinearLayout.LayoutParams.WRAP_CONTENT);
    m_ListView.setBackgroundColor(Color.BLACK);
    m_LinearLayout.addView(m_ListView, param);
    setContentView(m_LinearLayout);
    Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
    ListAdapter listAdapter = new SimpleCursorAdapter(this,
    android.R.layout.simple_list_item_2, cursor, new String[] {
    MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ARTIST },
    new int[] {android.R.id.text1, android.R.id.text2 });
    m_ListView.setAdapter(listAdapter);m_ListView
    .setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1,
    int arg2, long arg3) {
    DisplayToast("滚动到第"
    + Long.toString(arg0.getSelectedItemId()) + "项");}@Override
    public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub}
    });
    m_ListView
    .setOnItemClickListener(new AdapterView.OnItemClickListener() {@Override
    public void onItemClick(AdapterView<?> arg0, View arg1,
    int arg2, long arg3) {
    DisplayToast("选中了第" + Integer.toString(arg2 + 1) + "项");}});
    }private void DisplayToast(String string) {
    Toast.makeText(this, string, Toast.LENGTH_SHORT).show();
    }}
      

  5.   

    红色部分是我修改的地方
    ps:你先保证你的虚拟机里有歌曲。我的是在sd卡中添加的