How to read an SQLite DB in android with a cursorloader?

Android Guide suggests to create a ContentProvider when you want to share your data with other applications. If you don’t need this, you can just override method loadInBackgroud() of the CursorLoader class. For example write like this in your onCreateLoader: return new CursorLoader( YourContext, null, YourProjection, YourSelection, YourSelectionArgs, YourOrder ) { @Override public Cursor loadInBackground() … Read more

Get selected item from ListView bound with SimpleCursorAdapter

So a couple of points: after you fetch the cursor, you want to call startManagingCursor. This ties the cursor’s lifecycle with Activity’s lifecycle (so when the Activity gets destroyed the cursor gets closed/cleaned up). startManagingCursor(c); ListAdapter adapter = new SimpleCursorAdapter( this, android.R.layout.simple_list_item_1, c, new String[] {“name”}, new int[] {android.R.id.text1}); setListAdapter(adapter); Also, the database isn’t closed, … Read more

How to set Spinner Default by its Value instead of Position?

If you are setting the spinner values by ArrayList or Array you can assign the spinner’s selection by using the value’s index. String myString = “some value”; //the value you want the position for ArrayAdapter myAdapter = (ArrayAdapter) mySpinner.getAdapter(); //cast to an ArrayAdapter int spinnerPosition = myAdapter.getPosition(myString); //set the default according to the value spinner.setSelection(spinnerPosition); … Read more