Skip to content

Instantly share code, notes, and snippets.

@AntonioDiaz
Created December 24, 2016 16:31
Show Gist options
  • Save AntonioDiaz/a4a093c12ad6968d5ef2baebdaa5270b to your computer and use it in GitHub Desktop.
Save AntonioDiaz/a4a093c12ad6968d5ef2baebdaa5270b to your computer and use it in GitHub Desktop.
Android ContentProvider access example.
package android.example.com.dictionaryproviderexample;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.UserDictionary;
import android.provider.UserDictionary.Words;
import android.support.v7.app.ActionBarActivity;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView dictTextView = (TextView) findViewById(R.id.dictionary_text_view);
ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(UserDictionary.Words.CONTENT_URI, null, null, null, null);
try {
dictTextView.setText("The UserDictionary contains " + cursor.getCount() + " words.");
String columnsStr = "COLUMNS: " + Words._ID + " - " + Words.FREQUENCY + " - " + Words.WORD;
dictTextView.append(columnsStr);
int idColumn = cursor.getColumnIndex(Words._ID);
int frequencyColumn = cursor.getColumnIndex(Words.FREQUENCY);
int wordColumn = cursor.getColumnIndex(UserDictionary.Words.WORD);
while (cursor.moveToNext()) {
String word = cursor.getString(wordColumn);
Integer id = cursor.getInt(idColumn);
Integer frequency = cursor.getInt(frequencyColumn);
dictTextView.append(("\n" + id + " - " + frequency + " - " + word));
}
} finally {
cursor.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment