As a learning project for myself, I read the contents of a text file
into an SQL database, and would like to make its contents accessible
via a SimpleCursorAdapter.
I would like to associate the cursor to the content provider CONTENT_URI,
so that I can link its adapter to the AutoCompleteTextView. Can I do it?
My code to read the file and store it in the DB is:
Using java Syntax Highlighting
- public class DictionarySQLAutoComplete extends Activity
- {
- public static final String AUTHORITY = "com.xxx.android.examples";
- public static final Uri CONTENT_URI = Uri.parse("content://" +
- AUTHORITY + "/dictionarysqlautocomplete");
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- DictionaryDbAdapter aDbHelper = new DictionaryDbAdapter( this );
- aDbHelper.open();
- // Fill the DB with the file contents
- AssetManager assetManager = getResources().getAssets();
- InputStream inStream =assetManager.open("filename.txt");
- DataInputStream dataInStream = new DataInputStream(inStream);
- BufferedReader bufReader = new BufferedReader(new InputStreamReader
- (dataInStream));
- String aFileLine = null;
- while( ( aFileLine = bufReader.readLine() ) != null )
- {
- // Copy read line to next item to the dictionary
- long result = aDbHelper.createDictionaryEntry(aFileLine, "empty for now");
- }
- bufReader.close();
- // Get the cursor for the full DB contents query
- Cursor aCursor = mDbHelper.fetchAllDictionaryEntries();
- //Create the auto-complete text widget
- AutoCompleteTextView textView = (AutoCompleteTextView) findViewById
- (R.id.edit);
- // Create the adapter from the database cursor
- mFrom = new String[] { DictionaryDbAdapter.KEY_ROWID};
- mTo = new int[] { android.R.id.text1 };
- SimpleCursorAdapter adapter = new SimpleCursorAdapter( this,
- android.R.layout.simple_dropdown_item_1line,
- aCursor, mFrom, mTo );
- }
Parsed in 0.017 seconds, using GeSHi 1.0.8.4
The DictionaryDbAdapter class is based on the one used in the Notepad
tutorial sample.
Thanks,
Paul