If you think the Android project sqlite-provider listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package com.novoda.sqliteprovider.demo.simple.ui;
/*www.java2s.com*/import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.novoda.sqliteprovider.demo.simple.R;
import com.novoda.sqliteprovider.demo.simple.provider.FireworkProvider;
publicclass MainFragment extends Fragment {
/**
* See /assets/migrations/1_SETUP.SQL for the database creation
*/
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**
* You can save multiple ways - this is just an example of using Uri's
* do not normally do this on the UI Thread
*/
saveNewShopToDatabase();
/**
* You can retrieve from the database multiple ways - this is just an example of using Uri's
*/
retrieveShopsFromDatabase();
}
privatevoid saveNewShopToDatabase() {
Uri table = FireworkProvider.SHOPS;
ContentValues values = new ContentValues(1);
values.put(FireworkProvider.COL_SHOP_NAME, "MyNewShop" + System.currentTimeMillis());
values.put(FireworkProvider.COL_SHOP_POSTCODE, "LN11YA");
getActivity().getContentResolver().insert(table, values);
}
privatevoid retrieveShopsFromDatabase() {
getActivity().getSupportLoaderManager()
.initLoader(R.id.loader_shop, null, new LoaderManager.LoaderCallbacks<Cursor>() {
@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
returnnew ShopCursorLoader(getActivity());
}
@Override
publicvoid onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
if (!cursor.moveToFirst()) {
Log.d("demo", "Nothing in DB, returning early");
}
do {
String shopName = cursor.getString(cursor.getColumnIndex("name"));
String shopPostcode = cursor.getString(cursor.getColumnIndex("postcode"));
Log.d("demo", "Found shop: " + shopName);
Log.d("demo", "Found postcode: " + shopPostcode);
} while (cursor.moveToNext());
}
@Override
publicvoid onLoaderReset(Loader<Cursor> cursorLoader) {
}
});
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main, container, false);
}
privatestaticclass ShopCursorLoader extends CursorLoader {
public ShopCursorLoader(Context context) {
super(context, FireworkProvider.SHOPS, null, null, null, null);
}
}
}