2016-03-10 19 views
5

Seguendo This Retrieving a List of Contacts Tutorial nel sito degli sviluppatori Android, sono riuscito a implementare la funzionalità di ricerca dei contatti. Ecco il mio codice finoraRicerca intelligente contatti in Android

private void retrieveContactRecord(String phoneNo) { 
     try { 
      Log.e("Info", "Input: " + phoneNo); 
      Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, 
        Uri.encode(phoneNo)); 
      String[] projection = new String[]{ContactsContract.PhoneLookup._ID, ContactsContract.PhoneLookup.DISPLAY_NAME}; 


      String sortOrder = ContactsContract.PhoneLookup.DISPLAY_NAME + " COLLATE LOCALIZED ASC"; 
      ContentResolver cr = getContentResolver(); 
      if (cr != null) { 
       Cursor resultCur = cr.query(uri, projection, null, null, sortOrder); 
       if (resultCur != null) { 
        while (resultCur.moveToNext()) { 
         String contactId = resultCur.getString(resultCur.getColumnIndex(ContactsContract.PhoneLookup._ID)); 
         String contactName = resultCur.getString(resultCur.getColumnIndexOrThrow(ContactsContract.PhoneLookup.DISPLAY_NAME)); 
         Log.e("Info", "Contact Id : " + contactId); 
         Log.e("Info", "Contact Display Name : " + contactName); 
         break; 
        } 
        resultCur.close(); 
       } 
      } 
     } catch (Exception sfg) { 
      Log.e("Error", "Error in loadContactRecord : " + sfg.toString()); 
     } 
    } 

Qui è il fermo, questo codice funziona abbastanza grande, ma ho bisogno di implementare una ricerca intelligente qui. Voglio 26268 per abbinare Amanu così come 094 526 2684. Credo che si chiami dizionario T9.

Ho provato a cercare altri progetti per indizio, ma non sono riuscito a trovare nulla. Qualsiasi suggerimento sarebbe apprezzato!

risposta

0

Il ContentProvider per i contatti non lo supporta. Quindi quello che ho fatto è stato di scaricare tutti i contatti in un List quindi utilizzare un RegEx corrispondente per il nome.

public static String[] values = new String[]{" 0", "1", "ABC2", "DEF3", "GHI4", "JKL5", "MNO6", "PQRS7", "TUV8", "WXYZ9"}; 

/** 
* Get the possible pattern 
* You'll get something like ["2ABC","4GHI"] for input "14" 
*/ 
public static List<String> possibleValues(String in) { 

    if (in.length() >= 1) { 
     List<String> p = possibleValues(in.substring(1)); 
     String s = "" + in.charAt(0); 
     if (s.matches("[0-9]")) { 
      int n = Integer.parseInt(s); 

      p.add(0, values[n]); 
     } else { 
      // It is a character, use it as it is 
      p.add(s); 
     } 

     return p; 
    } 
    return new ArrayList<>(); 
} 

.... Quindi compilare il modello. Ho usato (?i) per renderlo case insensitive

List<String> values = Utils.possibleValues(query); 
StringBuilder sb = new StringBuilder(); 
for (String value : values) { 
    sb.append("["); 
    sb.append(value); 
    sb.append("]"); 
    if (values.get(values.size() - 1) != value) { 
    sb.append("\\s*"); 
    } 
} 

Log.e("Utils", "Pattern = " + sb.toString()); 

Pattern queryPattern = Pattern.compile("(?i)(" + sb.toString() + ")"); 

Saprete cosa fare dopo questo.

1

Dump tutti i contatti a una HashSet

Set<String> contacts = new HashSet<String>(); 

quindi cercare:

List<List<String>> results = new ArrayList<List<String>>(); 
// start the search, pass empty stack to represent words found so far 
search(input, dictionary, new Stack<String>(), results); 

Metodo di ricerca (from @WhiteFang34)

public static void search(String input, Set<String> contacts, 
    Stack<String> words, List<List<String>> results) { 

    for (int i = 0; i < input.length(); i++) { 
     // take the first i characters of the input and see if it is a word 
     String substring = input.substring(0, i + 1); 

     if (contacts.contains(substring)) { 
      // the beginning of the input matches a word, store on stack 
      words.push(substring); 

      if (i == input.length() - 1) { 
       // there's no input left, copy the words stack to results 
       results.add(new ArrayList<String>(words)); 
      } else { 
       // there's more input left, search the remaining part 
       search(input.substring(i + 1), contacts, words, results); 
      } 

      // pop the matched word back off so we can move onto the next i 
      words.pop(); 
     } 
    } 
} 
+0

Dov'è il T9? –

3

ricerca T9 può essere implementato utilizzando trie data structure. Puoi vedere un esempio qui: Trie dict. Dopo aver implementato qualcosa di simile, sarai in grado di convertire il tuo input di ricerca nella sua possibile variante decodificata T9 e confrontarlo se corrisponde al nome.

+0

Hai implementato questo? , Non sono in grado di capire come usare nel mio progetto, per favore aiuto –

Problemi correlati