1

Я искал и изучал эту проблему по всему Интернету, но не получил четкого ответа. Я добавил слова в пользовательский словарь, используя контент-провайдер, так как android documentation утверждает, что слова добавляются, но после этого, когда я набираю на клавиатуру, я не вижу слова, появляющегося в предложениях в кандидатах, как другие слова, которые мы нажимаем там следующий раз. Я бы очень признателен за полный ответ на эту проблему, поскольку многие люди спрашивают ее в сети и не получают ответа. Я попытался этоКак добавить новые слова в пользовательский словарь андроида, чтобы они отображались при просмотре кандидатов при вводе

Uri mNewUri; 
    // Defines an object to contain the new values to insert 
    ContentValues mNewValues = new ContentValues(); 

    // Sets the values of each column and inserts the word. The arguments to the "put" 
    // method are "column name" and "value" 

    mNewValues.put(UserDictionary.Words.APP_ID, "example.user"); 
    mNewValues.put(UserDictionary.Words.LOCALE, "en_US"); 
    mNewValues.put(UserDictionary.Words.WORD, "Qasim"); 
    mNewValues.put(UserDictionary.Words.FREQUENCY, "100"); 

    mNewUri = getContentResolver().insert(
     UserDictionary.Words.CONTENT_URI, // the user dictionary content URI 
     mNewValues       // the values to insert 
    ); 


    Uri dic = UserDictionary.Words.CONTENT_URI; 
    ContentResolver resolver = getContentResolver(); 
    Cursor cursor = resolver.query(dic, null, null, null, null); 
//here i retrieve all the words stored into my dictionary 
    while (cursor.moveToNext()){ 
     String word = cursor.getString(cursor.getColumnIndex(UserDictionary.Words.WORD)); 
     int id = cursor.getInt(cursor.getColumnIndex(UserDictionary.Words._ID)); 
     String app = cursor.getString(cursor.getColumnIndex(UserDictionary.Words.APP_ID)); 
     int frequency = cursor.getInt(cursor.getColumnIndex(UserDictionary.Words.FREQUENCY)); 
     String locale = cursor.getString(cursor.getColumnIndex(UserDictionary.Words.LOCALE)); 
     Log.i("", "word: "+word+"\nId: "+id+"\nAppID: "+app+"\nfrequency: "+frequency+"\nLocale:"+locale); 
    } 

Был бы признателен, если кто-то помогает мне здесь

+0

Нашли решение ...? –

ответ

0

Если клавиатура используется читает словарь пользователя, вы должны иметь возможность добавлять слова в словарь, как сказано в документации, используя resolver.insert.

Я добавил несколько слов вручную, используя клавиатуру по умолчанию. С помощью resolver.query на выходе выводятся новые слова. Клавиатура добавляет новые слова в словарь.

Впоследствии я добавил слово в словарь (Quasimodo), используя resolver.insert. Слово было добавлено в словарь (оно появляется на выходе resolver.query), и оно также появляется как предложение на клавиатуре.

Если я переключусь на другую клавиатуру (например, Swiftkey), эти слова не используются для прогнозирования. Возможно, вы используете какую-то клавиатуру.

Мой полный код:

public class MainActivity extends ActionBarActivity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     // Get the TextView which will be populated with the Dictionary ContentProvider data. 
     TextView dictTextView = (TextView) findViewById(R.id.dictionary_text_view); 
     // Get the ContentResolver which will send a message to the ContentProvider 
     ContentResolver resolver = getContentResolver(); 
     // Put a new word on the dictionary 
     final Locale locale; 
     locale = Locale.getDefault(); 
     final int COLUMN_COUNT = 3; 
     ContentValues values = new ContentValues(COLUMN_COUNT); 
     values.put(Words.WORD, "Quasimodo"); 
     values.put(Words.FREQUENCY, 250); 
     values.put(Words.LOCALE, locale.toString()); 
     Uri result = resolver.insert(UserDictionary.Words.CONTENT_URI, values); 

     // Get a Cursor containing all of the rows in the Words table 
     // The word "Quasimodo" inserted will be shown 
     Cursor cursor = resolver.query(UserDictionary.Words.CONTENT_URI, null, null, null, null); 
     // Surround the cursor in a try statement so that the finally block will eventually execute 
     try { 
      dictTextView.setText("UserDictionary contains " + cursor.getCount() + " words\n"); 
      dictTextView.append("COLUMNS: " + Words._ID + " - " + Words.FREQUENCY + 
        " - " + Words.WORD); 
      int idColumn = cursor.getColumnIndex(UserDictionary.Words._ID); 
      int frequencyColumn = cursor.getColumnIndex(UserDictionary.Words.FREQUENCY); 
      int wordColumn = cursor.getColumnIndex(UserDictionary.Words.WORD); 
      while (cursor.moveToNext()) { 
       int id = cursor.getInt(idColumn); 
       int frequency = cursor.getInt(frequencyColumn); 
       String word = cursor.getString(wordColumn); 
       dictTextView.append(("\n" + id + " - " + frequency + " - " + word)); 
      } 
     } finally { 
      cursor.close(); 
     } 
    } 
} 

Моя клавиатура показать слово вставить как предложение впоследствии.

enter image description here

 Смежные вопросы

  • Нет связанных вопросов^_^