Android Open Source - sqlite-analyzer Main Fragment






From Project

Back to project page sqlite-analyzer.

License

The source code is released under:

Apache License

If you think the Android project sqlite-analyzer 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;
/*  ww  w .jav a 2 s . c om*/
import android.app.Fragment;
import android.app.LoaderManager;
import android.content.ContentValues;
import android.content.Context;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.novoda.sqliteprovider.demo.simple.DB;
import com.novoda.sqliteprovider.demo.simple.R;
import com.novoda.sqliteprovider.demo.simple.provider.FireworkProvider;

public class MainFragment extends Fragment {

    /**
     * See /assets/migrations/1_SETUP.SQL for the database creation
     */
    @Override
    public void 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();
    }

    private void saveNewShopToDatabase() {
        ContentValues values = new ContentValues(1);
        DB.Shop.setName("MyNewShop" + System.currentTimeMillis(), values);
        DB.Shop.setPostcode("LN11YA", values);
        getActivity().getContentResolver().insert(FireworkProvider.SHOPS, values);
    }

    private void retrieveShopsFromDatabase() {
        getActivity().getLoaderManager()
                .initLoader(R.id.loader_shop, null, new LoaderManager.LoaderCallbacks<Cursor>() {

                    @Override
                    public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
                        return new ShopCursorLoader(getActivity());
                    }

                    @Override
                    public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
                        if (!cursor.moveToFirst()) {
                            Log.d("demo", "Nothing in DB, returning early");
                        }

                        do {
                            DB.Shop shop = DB.Shop.fromCursor(cursor);
                            Log.d("demo", "Found shop: " + shop.getName());
                            Log.d("demo", "Found postcode: " + shop.getPostcode());
                        } while (cursor.moveToNext());

                    }

                    @Override
                    public void onLoaderReset(Loader<Cursor> cursorLoader) {

                    }
                });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_main, container, false);
    }

    private static class ShopCursorLoader extends CursorLoader {

        public ShopCursorLoader(Context context) {
            super(context, FireworkProvider.SHOPS, null, null, null, null);
        }
    }
}




Java Source Code List

com.example.MainActivity.java
com.example.MainActivity.java
com.example.MyDatabase.java
com.example.MyDatabase.java
com.novoda.sqlite.Analyzer.java
com.novoda.sqlite.Connector.java
com.novoda.sqlite.FileConnector.java
com.novoda.sqlite.MigrationsConnector.java
com.novoda.sqlite.MigrationsInDir.java
com.novoda.sqlite.Migrations.java
com.novoda.sqlite.NumberedFilesComparator.java
com.novoda.sqlite.StringUtilOld.java
com.novoda.sqlite.StringUtil.java
com.novoda.sqlite.impl.SQLFile.java
com.novoda.sqlite.impl.TableCreateStatementParser.java
com.novoda.sqlite.model.Column.java
com.novoda.sqlite.model.DataAffinity.java
com.novoda.sqlite.model.Database.java
com.novoda.sqlite.model.Table.java
com.novoda.sqliteprovider.demo.simple.provider.FireworkProvider.java
com.novoda.sqliteprovider.demo.simple.ui.MainActivity.java
com.novoda.sqliteprovider.demo.simple.ui.MainFragment.java