gpt4 book ai didi

android - 列出数组中的联系人

转载 作者:太空宇宙 更新时间:2023-11-03 10:49:19 25 4
gpt4 key购买 nike

我有一个包含一些电话号码的数组,我想选择并列出也在我的数组中的设备联系人。

现在我只能显示数组中的第一个电话号码。

这是我的代码:

public class Contacts extends ListActivity {
SharedPreferences settings ;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.contacts);
setTitle("Choose a phone");
// Query: contacts with phone shorted by name
settings = this.getSharedPreferences(SETTINGS, Context.MODE_PRIVATE);
String message = settings.getString("contacts", "0,0");
String [] contacts = message.split(",");

String query = " IN (";
for (int i = 1; i < contacts.length; i++) {
query += contacts[i];
if (i < contacts.length - 1)
query += ",";
}
query += ")";


Cursor mCursor = getContentResolver().query(
Data.CONTENT_URI,
new String[] { Data._ID, Data.DISPLAY_NAME, Phone.NUMBER, Phone.TYPE }, Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "' AND " + Phone.NUMBER + " IS NOT NULL AND " + Phone.NUMBER + query, null, Data.DISPLAY_NAME + " ASC");
startManagingCursor(mCursor);

// Setup the list
ListAdapter adapter = new SimpleCursorAdapter(this, // context
android.R.layout.simple_list_item_2, // Layout for the rows
mCursor, // cursor
new String[] { Data.DISPLAY_NAME, Phone.NUMBER }, // cursor
// fields
new int[] { android.R.id.text1, android.R.id.text2 } // view
// fields
);
setListAdapter(adapter);
}

最佳答案

我不确定这是否是唯一的问题。你没有在 IN statemet 中使用撇号

你可以尝试使用

String query = " IN (";
for (int i = 1; i < contacts.length; i++) {
query+= "'";
query += contacts[i];
query+= "'";
if (i < contacts.length - 1)
query += ",";
}
query += ")";

关于android - 列出数组中的联系人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16750083/

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