2013-12-21 41 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']);