码迷,mamicode.com
首页 > 移动开发 > 详细

按拼音、首字母搜索手机通讯录,自定义键盘搜索手机通讯录

时间:2015-07-22 19:01:40      阅读:299      评论:0      收藏:0      [点我收藏+]

标签:android   手机通讯录   通讯录搜索   

package contacters;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;


public class Test {
static Map<String, HashMap<String, Contact>> maps = new HashMap<String, HashMap<String, Contact>>();
static {
HashMap<String, Contact> cons = new HashMap<String, Contact>();
for (int i = 0; i < 1000; i++) {
String phone = getNumber(11);
if (!cons.containsKey(phone))
cons.put(phone, new Contact(getChar(6), getChar(10), phone,
getChar(4)));
}
maps.put("0", cons);
// 初始化,将所有联系人添加到lists
}


/**测试**/

public static void main(String[] args) {
search("3");
search("35");
search("356");
search("3567");
search("35678");
search("356781");
search("3567814");
search("35678147");
search("356781477");
search("3567814779");
search("35678147795");

for (int i = 0; i <= 10; i++) {
System.out.println(maps.get(i + "").size());
}
HashMap<String, Contact> a = maps.get("3");
for (Entry<String, Contact> entry : a.entrySet()) {
Contact con = entry.getValue();
System.out.println("------name:" + con.getName() + "---index:"
+ con.getIndex() + "-----pingyin:" + con.getPingyin());


List<String> contacts = getCombination("356");
for (String inputCon : contacts) {
if (con.getName().contains(inputCon)
|| con.getPingyin().contains(inputCon)
|| con.getIndex().contains(inputCon)) {
System.out.println(inputCon + ":"
+ con.getName().contains(inputCon) + "--"
+ con.getPingyin().contains(inputCon) + "--"
+ con.getIndex().contains(inputCon));
}
}
}
// List<String> contacts = getCombination("3567");


}


/**
* 搜索
* @param number
*/
public static void search(String number) {
HashMap<String, Contact> result = new HashMap<String, Contact>();
if(number.contains("0")||number.contains("1")){
maps.put(number.length()+"", result);
return;
}
List<String> contacts = getCombination(number);
HashMap<String, Contact> currentCantacts = maps
.get((number.length() - 1) + "");


for (String inputCon : contacts) {
for (Entry<String, Contact> entry : currentCantacts.entrySet()) {
Contact con = entry.getValue();
if (con.getName().contains(inputCon)
|| con.getPingyin().contains(inputCon)
|| con.getIndex().contains(inputCon)) {
result.put(con.getPhone(), con);
}
}
}
maps.put(number.length() + "", result);
}


/**
* 获取组合

* @param number
* @return
*/
public static List<String> getCombination(String number) {
List<String> oldList = new ArrayList<String>();
List<String> newList = new ArrayList<String>();
String strs[] = getNumbers(number.charAt(0) + "");
for (String str : strs) {
oldList.add(str);
}
if (number.length() == 1) {
return oldList;
}
for (int i = 1; i < number.length(); i++) {
strs = getNumbers(number.charAt(i) + "");
if (strs != null) {
newList.clear();
for (String old : oldList) {
for (String str : strs) {
newList.add(old + str);
}
}
oldList.clear();
oldList.addAll(newList);
}
}
return newList;
}


public static String[] getNumbers(String str) {
switch (str) {
case "2":
return new String[] { "a", "b", "c" };
case "3":
return new String[] { "d", "e", "f" };
case "4":
return new String[] { "g", "h", "i" };
case "5":
return new String[] { "j", "k", "l" };
case "6":
return new String[] { "m", "n", "o" };
case "7":
return new String[] { "p", "q", "r", "s" };
case "8":
return new String[] { "t", "u", "v" };
case "9":
return new String[] { "w", "x", "y", "z" };
default:
return null;
}


}


public static String getNumber(int num) {
Random random = new Random();
String temp = "";
for (int i = 0; i < num; i++) {
temp = temp + random.nextInt(9);
}
return temp;
}


public static String getChar(int num) {
String md = "abcdefghijkmnpqrstuvwxyzabc";
Random random = new Random();
String temp = "";
for (int i = 0; i < num; i++) {
temp = temp + md.charAt(random.nextInt(24));
}
return temp;
}


}


package contacters;


public class Contact {

private String name;

private String pingyin;

private String phone;

private String index;

public Contact(String name,String pingyin,String phone,String index){

this.name = name;

this.pingyin = pingyin;

this.phone = phone;

this.index = index;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getPingyin() {

return pingyin;

}

public void setPingyin(String pingyin) {

this.pingyin = pingyin;

}

public String getPhone() {

return phone;

}

public void setPhone(String phone) {

this.phone = phone;

}

public String getIndex() {

return index;

}

public void setIndex(String index) {

this.index = index;

}

}


版权声明:本文为博主原创文章,未经博主允许不得转载。

按拼音、首字母搜索手机通讯录,自定义键盘搜索手机通讯录

标签:android   手机通讯录   通讯录搜索   

原文地址:http://blog.csdn.net/torrytang/article/details/47004811

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!