0

我正在尝试让 ListView 工作,但我在屏幕上收到一条消息,需要关闭应用程序!我正在寻找错误,但我找不到任何错误!也许它在布局内部?我不确定我是否应该在里面有一个 ListView 或者这是创建 cdynamic 的?我究竟做错了什么?我也将使用 onClick 方法,但我想在这种情况下这是一个矿工问题!?一些帮助!谢谢!

public class Activity_3 extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    String[] projection = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone._ID};

    // Get a cursor with all people
    Cursor cursor = managedQuery(ContactsContract.Contacts.CONTENT_URI,
            projection, null, null, null);

    startManagingCursor(cursor);

    ListAdapter adapter = new SimpleCursorAdapter(this, R.layout.activity_3, cursor, new String[] {ContactsContract.Contacts.DISPLAY_NAME}, new int[] {R.id.contactItem });

    setListAdapter(adapter);
}
}

布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
    android:id="@+id/contactList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

<TextView android:id="@+id/contactItem" >
</TextView>

</LinearLayout>
4

2 回答 2

1

您是否已添加此权限

  <uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>

在 manifest.xml

好的,现在这应该是您的活动

public class mainact extends Activity {

    ListView l;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.abc);
        l=(ListView)findViewById(R.id.contactList);

        String[] projection = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone._ID};

        // Get a cursor with all people

        Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection,null,null, null);

        while (phones.moveToNext())
        {
             String Name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
             String Number=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

        }
        startManagingCursor(phones);

        ListAdapter adapter = new SimpleCursorAdapter(this, R.layout.abc, phones, new String[] {ContactsContract.Contacts.DISPLAY_NAME}, new int[] {R.id.contactItem });

        l.setAdapter(adapter);
    }
    }

和 xml 应该像

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
    android:id="@+id/contactList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
 android:id="@+id/contactItem" >
</TextView>

</LinearLayout>

或者只是适合您的需要,但这是工作代码

于 2013-02-22T12:53:17.803 回答
0

据我了解,您正在尝试使用适配器来创建每个视图包含另一个 ListView 的视图?

删除

<ListView
    android:id="@+id/contactList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

通常你不能将 ListView 放在另一个 ListView 中。

于 2013-02-22T12:51:37.923 回答