Мне удалось объединить некоторые идеи и запросы, чтобы получить список всех контактов, но теперь мне хотелось бы иметь возможность получить номер телефона выбранного контакта, но мне не повезло... Я знаю, что, вероятно, есть лучшие способы сделать то, что я делаю, но я просто пытаюсь понять это.
Код сначала:
Код: Выделить всё
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();
}
}
Код: Выделить всё
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
Подробнее здесь: https://stackoverflow.com/questions/680 ... ode-inside
Мобильная версия