How to make a SQL database accessable from a ContentProvider

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

How to make a SQL database accessable from a ContentProvider

Postby ppmoore » Fri Apr 17, 2009 7:55 am

Hello,

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:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class DictionarySQLAutoComplete extends Activity
  2.  
  3. {
  4.  
  5.   public static final String AUTHORITY = "com.xxx.android.examples";
  6.  
  7.   public static final Uri CONTENT_URI = Uri.parse("content://" +
  8.  
  9.                     AUTHORITY + "/dictionarysqlautocomplete");
  10.  
  11.  
  12.  
  13.   @Override
  14.  
  15.   public void onCreate(Bundle savedInstanceState)
  16.  
  17.   {
  18.  
  19.     DictionaryDbAdapter aDbHelper = new DictionaryDbAdapter( this );
  20.  
  21.     aDbHelper.open();
  22.  
  23.  
  24.  
  25.     // Fill the DB with the file contents
  26.  
  27.     AssetManager assetManager = getResources().getAssets();
  28.  
  29.     InputStream inStream =assetManager.open("filename.txt");
  30.  
  31.     DataInputStream dataInStream = new DataInputStream(inStream);
  32.  
  33.     BufferedReader bufReader = new BufferedReader(new InputStreamReader
  34.  
  35.                                                              (dataInStream));
  36.  
  37.  
  38.  
  39.     String aFileLine = null;
  40.  
  41.     while( ( aFileLine = bufReader.readLine() ) != null )
  42.  
  43.     {
  44.  
  45.       // Copy read line to next item to the dictionary
  46.  
  47.       long result = aDbHelper.createDictionaryEntry(aFileLine, "empty for now");
  48.  
  49.     }
  50.  
  51.     bufReader.close();
  52.  
  53.  
  54.  
  55.     // Get the cursor for the full DB contents query
  56.  
  57.     Cursor aCursor = mDbHelper.fetchAllDictionaryEntries();
  58.  
  59.  
  60.  
  61.     //Create the auto-complete text widget
  62.  
  63.     AutoCompleteTextView textView = (AutoCompleteTextView) findViewById
  64.  
  65.                                  (R.id.edit);
  66.  
  67.  
  68.  
  69.     // Create the adapter from the database cursor
  70.  
  71.     mFrom = new String[] { DictionaryDbAdapter.KEY_ROWID};
  72.  
  73.     mTo = new int[] { android.R.id.text1 };
  74.  
  75.     SimpleCursorAdapter adapter = new SimpleCursorAdapter( this,
  76.  
  77.             android.R.layout.simple_dropdown_item_1line,
  78.  
  79.             aCursor, mFrom, mTo );
  80.  
  81.  
  82.  
  83. }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


The DictionaryDbAdapter class is based on the one used in the Notepad
tutorial sample.

Thanks,
Paul
ppmoore
Freshman
Freshman
 
Posts: 9
Joined: Thu Apr 02, 2009 5:16 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Yahoo [Bot] and 6 guests