- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个联系人选择器列表,其中包含具有电话号码的联系人的复选框。现在,我的问题是似乎无法获取选中的联系人的姓名和电话号码。
这是我的代码:
import android.app.ListActivity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class Create_Group extends ListActivity implements OnClickListener{
// List variables
public String[] Contacts = {};
public int[] to = {};
public ListView myListView;
Button save_button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_group);
// Initializing the buttons according to their ID
save_button = (Button)findViewById(R.id.save_group_button);
// Defines listeners for the buttons
save_button.setOnClickListener(this);
Cursor mCursor = getContacts();
startManagingCursor(mCursor);
ListAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_multiple_choice, mCursor,
Contacts = new String[] {ContactsContract.Contacts.DISPLAY_NAME },
to = new int[] { android.R.id.text1 });
setListAdapter(adapter);
myListView = getListView();
myListView.setItemsCanFocus(false);
myListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
private Cursor getContacts() {
// Run query
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] { ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME};
String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + " = '"
+ ("1") + "'";
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC";
return managedQuery(uri, projection, selection, selectionArgs,
sortOrder);
}
public void onClick(View src) {
Intent i;
switch (src.getId())
{
case R.id.save_group_button:
int checked_Names_Counter = 0;
// Goes over the list of contacts and checks which were checked
for (int j = 0; j < myListView.getCount(); j++)
{
if (myListView.isItemChecked(j) == true)
{
Cursor cur = getContacts();
ContentResolver contect_resolver = getContentResolver();
cur.moveToFirst();
/**
* Here I tried to compare the IDs but each list has different IDs so it didn't really help me...
// Converts the current checked name ID into a String
String Checked_ID = String.valueOf(myListView.getCheckedItemIds()[checked_Names_Counter]);
// Checks if the current checked ID matches the cursors ID, if not move the cursor to the next name
while (Checked_ID != cur.getString(cur.getColumnIndexOrThrow(ContactsContract.Contacts._ID)))
{
cur.moveToNext();
}
*/
/**
* Here I tried to compare the names, even though it's not a good pratice, and it didn't work either...
String Checked_Name = myListView.getAdapter().getItem(checked_Names_Counter).toString();
// Checks if the current checked ID matches the cursors ID, if not move the cursor to the next name
while (Checked_Name != cur.getString(cur.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)))
{
cur.moveToNext();
}
*/
String id = cur.getString(cur.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
String name = "";
String no = "";
Cursor phoneCur = contect_resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
name = phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
no = phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
id = null;
name = null;
no = null;
phoneCur = null;
checked_Names_Counter++;
}
}
// Goes back to the Manage Groups screen
i = new Intent(this, Manage_Groups.class);
startActivity(i);
break;
}
}
}
有什么想法吗?
谢谢!!
最佳答案
看起来你很接近,我使用 ListView.getCheckedItemIds() 返回所选联系人的唯一 ID:
public void onClick(View view) {
long[] ids = myListView.getCheckedItemIds();
for(long id : ids) {
Cursor contact = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id + "" }, null);
// Do whatever you want with the data
}
}
添加
我有一个关于这段代码的快速问题:
// Goes back to the Manage Groups screen
i = new Intent(this, Manage_Groups.class);
startActivity(i);
这会让用户回到上一个 Activity 吗?如果是这样,您应该改用 finish();
。 finish() 结束当前 Activity,将其从堆栈中取出并释放所有内存(更少的内存浪费意味着更快的应用程序。)它还允许之前的 Activity 在它离开时恢复保存的状态(填充 EditTexts,之前的 Spinner 选择、切换按钮和复选标记等) Activity 从用户离开的地方恢复。
关于android - 多个联系人选择器列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11283831/
对于我的应用程序的一部分,我需要在选择该选项时显示所有联系人的列表(带有电话号码)。 这是按下按钮时调用的 Activity : package com.example.prototype01; im
我正在尝试使用 Google Contact API (C#) 将“生日”值添加到 Google Contact。谁能帮我解决这个问题。 我正在使用 Google 数据 API 设置 (1.4.0.2
如何使用 Google Contact API (c#) 在 Google Contact 中创建新的“自定义字段”? 我用过: ExtendedProperty obj_ExtendedProper
我正在 iPhone 应用程序中使用地址簿框架,并且我想获取项目公司名称。我在 AddressBookUI_Framework.pdf 中找不到此信息,有人可以解释一下吗? 问候 AddressBoo
我正在开发一个数据库来管理一家小公司的客户数据。客户是公司和机构(学校等),当然还有人/联系人。会有更多的范围及时添加,但现在我正在寻找关于核心设计本身的任何输入,如果有任何我在这里遗漏的东西可能会导
我正在使用 Swift Contacts 并尝试确定是否可以将 contacts.phoneNumbers 转换为 NSDictionary?即可以通过以下方式在 contacts.phoneNumb
我想要我的联系人的 ListView。我使用谷歌示例代码。问题是我一遍又一遍地获得相同的联系人: 吉姆 吉姆 吉姆 吉姆 吉姆 安娜 安娜 安娜 安娜 ... 如何获得我的联系人的 DISTINCT
对于我的应用程序,我需要导入 Gmail 地址簿,我可以按照“Gmail Contact API”进行操作。 最近 Gmail 添加了一些不属于 xml 的新字段(即生日、网站等)。 gmail ap
在 NetSuite 中编辑记录时,我有一个按钮需要能够获取所有联系人的名字、姓氏、电子邮件和可能的 Angular 色,以便我可以将其附加到我已经编写的其他代码中。我似乎无法弄清楚如何提取与记录关联
我们需要一种使用 Delphi/Pascal 代码读取(并且可以选择写入).vcf 文件的方法。带有源代码的免费库将是完美的。 最佳答案 AceVCard是delphi 2009的免费开放组件,兼容V
我正在模拟一个电话簿,其中有 ArrayList 。如何覆盖toString()函数为了拥有像这样的东西,我们正在做 System.out.println(phonebook) ? Name: nam
我正在尝试构建一个搜索 XML 表达式以与 Java 中的 Exchange Web 服务一起使用。我试图实现的是我可以通过电子邮件地址搜索所有联系人。我已经浏览了他们的文档,但未能使其正常工作。这是
我有一个项目正在进行,我想自动发送 SMS,并有时间和日期将 SMS 推送给特定的联系人。那么如何以简洁高效的方式从手机中提取联系人呢?基本上我希望用户按下一个按钮,然后应用程序转到联系人列表,然后用
我有一个表格 View ,显示所有联系人都使用 RealmSwift。如何使用谓词按电话号码过滤 Realm 联系人? class Contact: Object { @obj
我正在尝试使用 CNContactVCardSerialization 将联系人保存为 vcf,效果相对较好。我确实发现苹果不包含注释或图像作为 VCF 的一部分。我确实使用了 stackoverfl
我正在读取手机中的所有联系人,如下所示: Cursor cursor = MainApp.get().getContentResolver().query( C
我有具有 list 权限的应用程序 我的 Activity 尝试加载联系人: eDeviceRecordsLoader contactsLoader = new eDeviceRec
我正在尝试编辑组标题和注释, 编辑标题适用于系统组 和用户创建的组, 虽然注释列只有在系统组(例如“联系人”、“ friend ”、“家庭”、“同事”)时才会保留,我假设它不会为用户创建的组保存注释,
在我的应用中,我想将联系人与其他数据相关联。对联系人的引用必须尽可能持久,否则关联的数据将变成垃圾。 首先,我应该使用 ContactsContract.Contact.LOOKUP_KEY 访问聚合
我正在做一个应用程序,因为我显示从设备到 ListView 的所有联系人(姓名、号码、图像)。我想使用工具栏搜索从 ListView 中搜索联系人。我添加了工具栏搜索,但我不知道如何从列表中过滤联系人
我是一名优秀的程序员,十分优秀!