第一篇文章。我是 android 和一般编程的新手。我想要的是弹出一个自动完成文本框,其中包含联系人列表中的自动完成名称。IE,如果他们输入“john”,它将显示“John Smith”或他们联系人中的任何 john。代码是基本的,我从一些教程中提取了它。
private void autoCompleteBox() {
ContentResolver cr = getContentResolver();
Uri contacts = Uri.parse("content://contacts/people");
Cursor managedCursor1 = cr.query(contacts, null, null, null, null);
if (managedCursor1.moveToFirst()) {
String contactname;
String cphoneNumber;
int nameColumn = managedCursor1.getColumnIndex("name");
int phoneColumn = managedCursor1.getColumnIndex("number");
Log.d("int Name", Integer.toString(nameColumn));
Log.d("int Number", Integer.toString(phoneColumn));
do {
// Get the field values
contactname = managedCursor1.getString(nameColumn);
cphoneNumber = managedCursor1.getString(phoneColumn);
if ((contactname != " " || contactname != null)
&& (cphoneNumber != " " || cphoneNumber != null)) {
c_Name.add(contactname);
c_Number.add(cphoneNumber);
Toast.makeText(this, contactname, Toast.LENGTH_SHORT)
.show();
}
} while (managedCursor1.moveToNext());
}
name_Val = (String[]) c_Name.toArray(new String[c_Name.size()]);
phone_Val = (String[]) c_Number.toArray(new String[c_Name.size()]);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, name_Val);
personName.setAdapter(adapter);
}
personName 是我的自动完成文本框。因此,当我在模拟器(4.2)中使用它并通过人员应用程序手动输入联系人时,它实际上可以工作,但是当我在我的设备上使用它时,它不会弹出任何名称。我确信这很荒谬,但我试图找到答案,但我一无所获。不问就学不会。
这是日志
12-11 18:35:09.229: I/ActivityManager(578): 显示 12-11 18:35:11.031: I/ActivityManager(578): 开始 u0 {cmp=com.fthatnoise.borrow.me/.AddItem} 从PID 4153
12-11 18:35:11.161: D/dalvikvm(4153): GC_CONCURRENT freed <1K, 2% free 9648K/9784K, paused 5ms+2ms, total 22ms 12-11 18:35:11.181: D/dalvikvm(4153) :GC_FOR_ALLOC 已释放 <1K,2% 可用 9648K/9784K,暂停 21ms,总共 21ms 12-11 18:35:11.181:I/dalvikvm-heap(4153):将堆(碎片情况)增加到 10.359MB,分配 960016 字节12-11 18:35:11.201:D/dalvikvm(4153):GC_FOR_ALLOC 释放 0K,2% 释放 10585K/10724K,暂停 18ms,总共 18ms 12-11 18:35:11.221:D/dalvikvm(4153):GC_CONCURRENT 释放<1K,1%免费10637K/10744K,暂停3ms+1ms,共15ms
12-11 18:35:11.551: I/ActivityManager(578): 显示 com.fthatnoise.borrow.me/.AddItem: +514ms 12-11 18:35:11.571: D/overlay(162): FROM_STATE = OV_BYPASS_3_LAYER TO_STATE = OV_CLOSED 12-11 18:35:11.911: D/overlay(162): FROM_STATE = OV_CLOSED TO_STATE = OV_BYPASS_3_LAYER 12-11 18:35:13.073: D/overlay(162): FROM_STATE = OV_BYPASS_3_LAYER TO_STATE = OV_CLOSED