【问题标题】:use autocomplete textview with contacts phone number使用带有联系人电话号码的自动完成文本视图
【发布时间】:2012-02-20 04:51:26
【问题描述】:

我正在让我的编辑文本字段自动完成联系人电话号码。我知道如何从数据库中获取联系人并将它们显示在文本字段中,但我需要它们自动完成以防万一用户想在文本字段中输入名称。我了解如何让数组自动完成以及其背后的整个理论。但是如何从电话联系人中提取是困难的。我看过很多教程以及关于堆栈溢出的各种问题,但仍然有点难过。请提供代码片段。

public class MyContacts extends Activity {

    AutoCompleteTextView txtPhoneNo;

    public ArrayList<String> c_Name = new ArrayList<String>();
    public ArrayList<String> c_Number = new ArrayList<String>();
    String[] name_Val = null;
    String[] phone_Val = null;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        txtPhoneNo = (AutoCompleteTextView) findViewById(R.id.txtPhoneNo);

    }

    Uri contacts = Uri.parse("content://icc/adn");
    ContentResolver cr = getContentResolver();

    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);
                }

            } 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);
        txtPhoneNo.setAdapter(adapter);
    }
}

我的代码...没有编译错误但仍然无法工作

【问题讨论】:

  • 你能解释一下你想做什么吗?
  • 当你发短信时,与任何 Android 手机标准的自动完成文本视图非常相似
  • 您的问题解决了吗?如果解决了,请发布您的答案并接受它...

标签: android autocomplete sms android-contacts phone-number


【解决方案1】:

最好是获取联系人并将其存储在ArrayList 中。然后您可以将 ArrayList 与AutoComplete TextView 绑定。然后,当用户在AutoComplete TextView 中键入时,其余部分就很容易过滤 ArrayList。

更新

您可以使用contact_namecontact_number 的getter-setter 创建一个POJO 类。然后创建List&lt;POJO&gt; list = new ArrayList&lt;POJO&gt;();。然后只需使用POJO 类将contact_name 和contact_number 添加到list

POJO pojo_obj = new POJO();
pojo_obj.setcontact_name(contactname);
pojo_obj.setcontact_number(cphoneNumber);
list.add(pojo_obj);

最后将此list 设置为Adapter

然后在 Adapter 类中,您可以使用

将其设置为 TextView
list.get(position).getcontact_name();
list.get(position).getcontact_number();

【讨论】:

【解决方案2】:

我认为您正在 SDK 中进行测试。 SDK 没有 SIM 卡,因此无法工作。将 .APK 放入手机中,然后重试。它应该像一个魅力。

【讨论】:

  • 我一直在我的HTC Inspire上测试它。它还没有工作?当/如果你尝试过它对你有用吗?如果它对你有用,那会有所帮助。
  • 是的,这在我的三星 Galaxy 3 Android 2.1 中对我有用。顺便说一句,你可以试试这个网页,它有更好的方法来提取联系人.. this link does a god job
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-11
  • 2014-04-22
  • 1970-01-01
  • 1970-01-01
  • 2012-09-06
  • 2012-08-09
  • 1970-01-01
相关资源
最近更新 更多