gpt4 book ai didi

android - 我无法在我的代码中获取 EMAIL_ID 列,而是在 emailid 列中获取联系电话

转载 作者:行者123 更新时间:2023-11-29 00:16:45 26 4
gpt4 key购买 nike

我在这里发布我的代码。请帮助我获取联系人的电子邮件地址。我也得到了一个随机列表;请帮我也对数据进行排序。

package com.example.webwerks.retrievingcontactsdemo;

import android.app.Activity;
import android.app.ListActivity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;

import java.util.ArrayList;


public class MyActivity extends Activity {
private String name;
private String Number;
private String alternateNumber;
private String email_id;
RelativeLayout relativeLayout;
ListView listView;
ArrayList <String> arrayList = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
listView = (ListView)findViewById(android.R.id.list);
getInformation(this.getContentResolver());
// relativeLayout = (RelativeLayout)findViewById(R.id.layoutRelative);


}
public void getInformation(ContentResolver contentResolver){
Cursor phones = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null);
while (phones.moveToNext()){
name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
Number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

email_id = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
arrayList.add(name);
arrayList.add(Number);
arrayList.add(email_id);
}
phones.close();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrayList);
listView.setAdapter(adapter);


}


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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

这是我的代码。它给了我联系人姓名和联系电话(主要)和电子邮件 ID 列。此外,它只显示联系电话。并且数据不是根据我的电话联系人列表排序的。请帮忙。提前致谢:)

最佳答案

您需要使用 id 执行另一个查询以获取 tat 联系人的 Email 详细信息。所以检查这段代码

最好使用 DataModel 类:

public class ContactData
{
public String name;
public String email;
}


ArrayList<ContactData> data = new ArrayList<>();
public void getInformation(ContentResolver contentResolver){
Cursor phones = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null);


while (phones.moveToNext()){
String id = phones.getString(phones.getColumnIndex(ContactsContract.Contacts._ID));
Cursor cur1 = contentResolver.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
while (cur1.moveToNext()) {
//to get the contact names
String name=cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
Log.e("Name :", name);
String email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
Log.e("Email", email);


if(email!=null){
Toast.makeText(this,
"Name : "+ name+" EMail : "+email, 1000)
.show();
}
}
dataList.add(contactData);
cur1.close();


}
phones.close();




}

关于android - 我无法在我的代码中获取 EMAIL_ID 列,而是在 emailid 列中获取联系电话,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26113317/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com