gpt4 book ai didi

android - 联系人 API 编辑群组

转载 作者:行者123 更新时间:2023-11-30 00:08:30 27 4
gpt4 key购买 nike

我正在尝试编辑组标题和注释,

编辑标题适用于系统组用户创建的组

虽然注释列只有在系统组(例如“联系人”、“ friend ”、“家庭”、“同事”)时才会保留,我假设它不会为用户创建的组保存注释,或者某些如何被注释列中的标题列内容覆盖。 p>

如何在联系人组中使用备注栏?还有其他方法可以与组一起存储附加信息吗?这是我的代码 fragment :

     ArrayList<ContentProviderOperation> ops =new ArrayList<ContentProviderOperation>();

ContentProviderOperation.Builder op =
ContentProviderOperation.newUpdate(ContactsContract.Groups.CONTENT_URI)
.withSelection(ContactsContract.Groups._ID + "="+group.getId(), null)
.withValue(ContactsContract.Groups.TITLE, group.getTitle());

HashMap<String, String> notes = group.getNotes();
if(notes!=null && notes.size()>0){
op = op.withValue(ContactsContract.Groups.NOTES, new Gson().toJson(group.getNotes()));
}

ops.add(op.build());
try {
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (RemoteException e) {
e.printStackTrace();
} catch (OperationApplicationException e) {
e.printStackTrace();
}

最佳答案

希望这个解决方案对你有用,似乎对我也有用。

我用过 ContentResolver.update()方法。

I assume that it either doesn't save notes for user created groups or some how gets overwritten with title column content in notes column.

我能够使用以下代码更新用户创建的组的注释。

How can I use notes column in contact groups?

您可以使用注释列来保存任何 TEXT 值,因为它在 sqlite 中定义为 TEXT 类型。参见 ContactsContract.Groups.NOTES .

(在 Google Pixel XL、Oreo 8.0.0 上测试)

    public class MainActivity extends AppCompatActivity {

private Cursor mCursor;
private SimpleCursorAdapter adapter;
private ListView lvGroups;

@Override
protected void onDestroy() {
if (mCursor != null)
mCursor.close();
super.onDestroy();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

lvGroups = findViewById(R.id.lvGroups);
checkAndRequestPermission();

}

private void checkAndRequestPermission() {
if (PermissionChecker.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) == PermissionChecker.PERMISSION_GRANTED) {
loadGroups();
} else {
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS}, 123);
}
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
boolean isGranted = true;
for (int i = 0; i < grantResults.length; i++) {
if (grantResults[i] == PermissionChecker.PERMISSION_DENIED)
isGranted = false;
}
if (isGranted)
loadGroups();
else
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS},123);
}

private void loadGroups() {
mCursor = getContentResolver().query(ContactsContract.Groups.CONTENT_URI,null,ContactsContract.Groups.GROUP_IS_READ_ONLY + " = 0",null, ContactsContract.Groups.TITLE);

adapter = new SimpleCursorAdapter(this, R.layout.contact_group_item, mCursor, new String[] {
ContactsContract.Groups.TITLE,
ContactsContract.Groups._ID,
ContactsContract.Groups.ACCOUNT_NAME,
ContactsContract.Groups.ACCOUNT_TYPE,
ContactsContract.Groups.AUTO_ADD,
ContactsContract.Groups.GROUP_IS_READ_ONLY,
ContactsContract.Groups.GROUP_VISIBLE,
ContactsContract.Groups.SOURCE_ID,
ContactsContract.Groups.NOTES},
new int[] {
R.id.text1,
R.id.text2,R.id.text3, R.id.text4, R.id.text5, R.id.text6, R.id.text7, R.id.text8, R.id.text9}, SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

lvGroups.setAdapter(adapter);

lvGroups.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor cursor = adapter.getCursor();
cursor.moveToPosition(position);

int groupId = cursor.getInt(cursor.getColumnIndex(ContactsContract.Groups._ID));
ContentValues contentValues = new ContentValues();
contentValues.put(ContactsContract.Groups.NOTES,"My test Notes");
getContentResolver().update(ContactsContract.Groups.CONTENT_URI, contentValues, ContactsContract.Groups._ID + " = " + groupId, null);
adapter.notifyDataSetChanged();
}
});

}
}

关于android - 联系人 API 编辑群组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48560321/

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