我是android开发的新手。我的应用程序需要根据名称的联系号码,我必须从微调器中选择我使用的是Android 1.6。
public void onItemSelected(AdapterView<?> parent, View arg1,
int pos, long arg3)
{
String name,phoneNumber;
name=parent.getItemAtPosition(pos).toString();
String[] projection1 = new String[]{
People._ID,People.NAME,People.NUMBER
};
Cursor cur=getContentResolver().query(People.CONTENT_URI, projection1,People.NAME+"="+name , null, null);
phoneNumber=cur.getString(cur.getColumnIndex(People.NUMBER));
Toast.makeText(parent.getContext(), "number="+phoneNumber,Toast.LENGTH_LONG).show();
/* cur.moveToFirst();
if(cur.moveToFirst())
{
do
{
}while(cur.moveToNext());
}*/
}
提前致谢 问候: Arpit
答案 0 :(得分:0)
这是完整的代码试试这个
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));
if(name.equals(sentTo)){
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
for(int i=0;i<pCur.getColumnCount();i++)
number = pCur.getString(i);
}
pCur.close();
pCur = null;
}
}
}
}
答案 1 :(得分:0)
在清单中给予许可
<uses-permission android:name="android.permission.READ_CONTACTS"/>
创建模态类
public class ContactSave {
String name;
String number;
public ContactSave(String name, String number) {
this.name = name;
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
@Override
public String toString() {
return
"name='" + name + '\'' +
", number='" + number + '\'';
}
}
在MainActivity中编写代码
import android.content.ContentResolver;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
Spinner spinner;
ArrayList<ContactSave> arrayList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner) findViewById(R.id.spinner);
readAllContact();
}
private void readAllContact() {
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));
if (Integer.parseInt(cur.getString(
cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor cursor = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id}, null);
while (cursor.moveToNext()) {
String phoneNo = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
arrayList.add(new ContactSave(name, phoneNo));
Toast.makeText(MainActivity.this, "Name: " + name + ", Phone No: " + phoneNo, Toast.LENGTH_SHORT).show();
}
ArrayAdapter<ContactSave> dataAdapter = new ArrayAdapter<ContactSave>(this, android.R.layout.simple_spinner_item, arrayList);
spinner.setAdapter(dataAdapter);
cursor.close();
}
}
}
}
}
XML文件代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>