2011-07-23 85 views
1

中的代碼我一般新來的android和編程(即一個Linux系統管理員/腳本人員)。我一直試圖讓一個簡單的android應用程序去感受編程,但我遇到了一些問題。無法從聯繫人列表中獲取電話號碼...

我能夠串起一些想法和查詢來獲得所有聯繫人的列表視圖,但現在id喜歡能夠抓住所選聯繫人的電話號碼,但我沒有任何運氣......我知道可能有更好的方法來做我正在做的事情,但我只是想了解這一點。

代碼第一:

package com.SomeApp; 

import android.app.AlertDialog; 
import android.app.ListActivity; 
import android.database.Cursor; 
//import android.net.Uri; 
import android.os.Bundle; 
import android.provider.ContactsContract; 
import android.view.View; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.SimpleCursorAdapter; 

public class CallAppActivity extends ListActivity 
{ /** Called when the activity is first created. */ 
    private ListAdapter mAdapter; 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    //Get All information from ContactsContract 
    Cursor managedCursor = getContentResolver().query(
     ContactsContract.Contacts.CONTENT_URI, 
     null, 
     null, 
     null, 
     null 
    ); 

    //Activity will manage the cursor lifecycle. 
    startManagingCursor(managedCursor); 
    String[] columns = new String[] {ContactsContract.Contacts.DISPLAY_NAME}; 
    int[] names = new int[] {R.id.contact_entry}; 
    mAdapter = new SimpleCursorAdapter(
     this, 
     R.layout.main, 
     managedCursor, 
     columns, 
     names 
    ); 
    setListAdapter(mAdapter); 
    } 

    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id) 
    { super.onListItemClick(l, v, position, id); 

    Cursor lookup = (Cursor) mAdapter.getItem(position); 
    String contactId = lookup.getString(
     lookup.getColumnIndex(
     ContactsContract.Contacts._ID 
    ) 
    ); 

    Cursor phone_num = getContentResolver().query(
     ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
     null, 
     ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, 
     null, 
     null 
    ); 

    String phoneId = phone_num.getString(
     phone_num.getColumnIndex(
     ContactsContract.CommonDataKinds.Phone.NUMBER 
    ) 
    ); 

    //We are going to print shit to a message box.... 
    AlertDialog.Builder alertbox = new AlertDialog.Builder(this); 
    alertbox.setMessage(phoneId); 
    alertbox.show(); 
    } 
} 

而且現在的logcat ....

07-23 11:35:34.469: WARN/dalvikvm(781): threadid=1: thread exiting with uncaught exception (group=0x40015560) 
07-23 11:35:34.479: ERROR/AndroidRuntime(781): FATAL EXCEPTION: main 
07-23 11:35:34.479: ERROR/AndroidRuntime(781): android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1 

無論我選擇什麼樣的聯繫,我得到了同樣的異常,所以我明明做錯了什麼..我只是不能很弄清楚我自己:(

任何幫助表示讚賞。

回答

2

迪d您是否嘗試將光標指針指向其數據的第一行,如cursor.moveToFirst(),然後再訪問其中的數據?其實,對於Cursor phone_numCursor phone_id

+0

我當然沒有,但我只是加了它,低,看到它的作品,現在我欣賞快速反應尼古拉。有人能給我一個快速解釋爲什麼這是必要的嗎?當我已經有一個應該只返回該值的查詢時,爲什麼需要這樣做對我來說並不合適? – trey85stang

+0

將遊標指針放到第一條記錄是非常重要的,所以您可以使用.moveToNext()遍歷記錄,並檢查該指針是否位於最後一條記錄之後.isAfterLast()。查詢不只返回該值(更好的說一個值),它返回的數據分別與查詢條件中的數據一樣多。您可以使用遊標進行迭代並查看查詢的結果。我希望我對你的問題的建議能夠完成這項工作,這個快速解釋讓你更清楚。乾杯。 –

+1

尼古拉,我很欣賞你的答覆對你的解釋很有幫助。 – trey85stang

相關問題