Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
421 views
in Technique[技术] by (71.8m points)

java - android获取自定义联系人类型标签名称(android getting custom contact type label name)

I'm having trouble finding out how to query a custom phone number type.

(我在查找如何查询自定义电话号码类型时遇到麻烦。)

Like if you go into contacts-edit contact and change one of the phone numbers to a custom label.

(就像您进入联系人-编辑联系人并将电话号码之一更改为自定义标签一样。)

Is there a way to get the name entered into the custom type label?

(有没有办法将名称输入自定义类型标签?)
I have tried

(我试过了)

"android.content.res.Resources.getSystem() .getStringArray( android.R.array.phoneTypes)"

(“ android.content.res.Resources.getSystem().getStringArray(android.R.array.phoneTypes)”)

but it just seems to crash the app and I think its for an old version of android.

(但是它似乎使应用程序崩溃了,我认为它适用于旧版本的android。)
And I have also tried

(而且我也尝试过)

curser.getString(curser.getColumnIndex(ContactsContract.CommonDataKinds.Phone .LABEL)".

(curser.getString(curser.getColumnIndex(ContactsContract.CommonDataKinds.Phone .LABEL)”。)

If anyone has an idea it would be greatly appreciated, or maybe point me to a duplicate if one exist I couldn't find one though.

(如果任何人有一个主意,将不胜感激,或者如果我有一个主意,也许我会指出一个副本,但是我找不到。)

  ask by Bob translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

right now I've been doing the same thing, after ensuring your queries are correct (i'm querying ContactsContract.CommonDataKinds.Phone.CONTENT_URI) (make sure your projection's are right, etc.) you can do something like below, i guess the difficulty you're facing is picking up a preset label vs. a custom label.

(现在,我一直在做同样的事情,在确保您的查询正确之后(我正在查询ContactsContract.CommonDataKinds.Phone.CONTENT_URI)(确保您的投影正确,等等),我想可以做以下类似的事情您面临的困难是选择预设标签还是自定义标签。)

preset labels are represented as integers in the TYPE column whereas if the TYPE == TYPE_CUSTOM, the LABEL field will have the data you're looking for.

(预设标签在TYPE列中表示为整数,而如果TYPE == TYPE_CUSTOM,则LABEL字段将包含您要查找的数据。)

moving from numbers in TYPE to a string is with a method provided.

(从TYPE中的数字移动到字符串是提供的一种方法。)

i'm not sure about the localization though i think that's handled.

(我不确定本地化,尽管我认为已经解决了。)

Cursor curse = mContext.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[]{ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.LABEL}, 
                        ContactsContract.CommonDataKinds.Phone.NUMBER + " = ?", new String[]{numnum}, null);
                int colIndex = curse.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
                int lblIndex = curse.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL);
                ArrayList<String> numbers = new ArrayList<String>();
                String cur = "";
                while(curse.moveToNext())
                {
                    int labelType = curse.getInt(colIndex);
                    if(labelType == ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM)
                    {
                        cur = curse.getString(lblIndex);
                    }
                    else
                    {
                        CharSequence seq = ContactsContract.CommonDataKinds.Phone.getTypeLabel(mContext.getResources(), labelType, "Mobile");
                        cur = seq.toString();
                    }
                    numbers.add(cur);
                }
                curse.close();

at the end of this snippet you'll end up with an arraylist of strings filled with the labels used for this phone number.

(在此代码段的结尾,您将得到一个字符串数组列表,其中填充有用于此电话号码的标签。)

note that the phone number needs to be a pretty precise match, so 444-4444 will not match up with 4444444 and vice versa.

(请注意,电话号码必须非常精确地匹配,因此444-4444不会与4444444匹配,反之亦然。)

personally, i haven't had time to experiment with what the difference is between putting "Mobile" or "" on the last variable in getTypeLabel though it didn't appear to make a difference just yet.

(就个人而言,我还没有时间尝试将“ Mobile”或“”放在getTypeLabel的最后一个变量之间有什么区别,尽管它似乎似乎并没有什么不同。)

hope this answer wasn't too late.

(希望这个答案不会太晚。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...