2014-02-10 108 views
0

我想訪問存儲在手機(數量和名稱)存儲在我現在已經成功完成的聯繫人現在我想顯示他們在一個列表中,使他們的聯繫人姓名和電話號碼應顯示爲我有以下代碼不運行任何人告訴一個列表項什麼問題顯示在手機屏幕上保存的聯繫人列表

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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/listView1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" > 
</ListView> 

</RelativeLayout> 

mainActivity.java

package com.tayyab.contactnum; 
import java.util.ArrayList; 
import java.util.List; 

import android.os.Bundle; 
import android.provider.ContactsContract; 
import android.app.Activity; 
import android.app.ListActivity; 
import android.content.ContentResolver; 
import android.database.Cursor; 
import android.util.Log; 
import android.view.Menu; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.TextView; 

public class MainActivity extends ListActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    ListView listview; 
    List<String> newValues = new ArrayList<String>(); 

    ContentResolver cr = getContentResolver(); 
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, 
      null, null, null); 
    if (cur.getCount() > 0) { 
     while (cur.moveToNext()) { 
      String id = cur.getString(cur 
      .getColumnIndex(ContactsContract.Contacts._ID)); 
String name=cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
        newValues.add(name); 
     if (Integer.parseInt(cur.getString(cur 
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { 
    Cursor phones = getContentResolver().query(
    ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
    null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = " + id, 

      null, null); 
    while (phones.moveToNext()) { 
     String phoneNumber = phones.getString(phones 
    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 

       } 

       phones.close(); 
      } 
     } 
    } 
    setListAdapter(new ArrayAdapter<String>(this, 
      android.R.layout.simple_list_item_1, newValues)); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 
    } 
+0

你得到的錯誤是什麼? – Uttam

回答

0

在使用代替,

setListAdapter(new ArrayAdapter<String>(this, 
     android.R.layout.simple_list_item_1, newValues)); 

必須使用:

final ArrayAdapter<String> aa = new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_1, newValues); 
    listview.setAdapter(aa); 

,只是後newValues.add(名稱),你必須使用

aa.notifyDataSetChanged(); 
0

首先,做一個類來保存信息您需要使用而不是使用ArrayList<String>,請使用ArrayList<Person>並將Person類定義爲:

class Person { 

    public final String name, phone; 

    public Person(String name, String phone) { 
     this.name = name; 
     this.phone = phone; 
    } 

} 

現在,當您瀏覽光標時,創建Person的實例並將它們添加到ArrayList<Person>

查看https://stackoverflow.com/a/2265712/772095,瞭解如何使用新的自定義類型Person來擴展ArrayAdapter,以及如何以這種方式爲列表項目創建視圖。

public class PersonAdapter extends ArrayAdapter<Person> 
相關問題