2013-12-21 116 views
0

我正在与EDITTEXT和ListView一个Android应用程序。在那我想用java集合来存储物品,然后在那个列表集合中搜索物品。 我的搜索方法是: 首先,我们将项目添加到收藏。 后来,当我们键入EDITTEXT字的启动信,该列表以获得以该字母开头,会为setSelection该指数的列表视图字的索引。 例如:我输入一个,然后它会选择苹果,并..... .....Java集合和地图

我想知道什么样的收集和地图,可以帮助我这个? 在此先感谢。

回答

0

如果你只看第一键入的字符,而不是单词的后续字符,然后一个简单的解决方案可能是字母表中的阵列要HANDL E,并将其持有的字符串的一个的ArrayList。这是非常简单,重量轻,并具有良好的性能,同时,见下图:

// Create an array with the alphabet from 'a' to 'z' (i.e. English alphabet) 
    List<String>[] firstCharLookup = new List[26]; 

    // Initialize the array to avoid NullPointerException's 
    for (int i = 0; i < firstCharLookup.length; i++) { 
     firstCharLookup[i] = new ArrayList<String>(); 
    } 

    // Fill your collections with the strings in the appropriate collections. 
    firstCharLookup[0].add("apple"); 
    firstCharLookup[0].add("active"); 
    firstCharLookup[0].add("addict"); 

    firstCharLookup[1].add("bee"); 
    firstCharLookup[1].add("busy"); 
    firstCharLookup[1].add("bus"); 
    // ... 
    // Continue with filling dictionary 


    // If user types 'a' or 'A' then lowercase the character, then: 
    char firstLookup = 'a'; 
    System.out.println(firstCharLookup[firstLookup - 'a']); 
    // We subtract by 'a' so that we get a '0' based index 

    // If user types 'b', then: 
    char secondLookup = 'b'; 
    System.out.println(firstCharLookup[secondLookup - 'a']);