Detail Views

All of our apps with databases so far have been in a single Activity. As you can imagine, most apps have more than one screen and often need to access the database across each screen. Even something as simple as a to-do-list would need access to the same information in different activities, such as the list item on one screen, but all of the details on the next. All of this information needs to be pulled from the same database. Today we will learn about the correct way to access the same database to gather information across multiple activities.

LEARNING OBJECTIVES

After this lesson, you will be able to:

  • Determine the data to pass from master to detail view
  • Create a detail View in a new Activity
  • Override the correct methods to open a detail view from a single item in a list
  • Pass a database connection between master and detail views

Code Below contains the MainActivity & DetailActivity of this project. To view the full source, visit https://github.com/code-for-coffee/libres-to-read

MainActivity

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.Locale;

public class MainActivity extends AppCompatActivity {

    Intent mDetailIntent;
    ListView mListOfBooks;
    Cursor mCursor;
    AdapterView.OnItemClickListener mClickListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SQLiteDatabase db;
        db = openOrCreateDatabase("Libres.db",
                SQLiteDatabase.CREATE_IF_NECESSARY,
                null);

        db.setVersion(1);
        db.setLocale(Locale.getDefault());

        String titles[] = {
            "How to win at Fantasy Sports",
            "How to keep your hair long and youthful",
            "How to dread your hair when you give up"
        };
        String authors[] = {
            "Jack",
            "Bryon",
            "James"
        };
        Integer ratings[] = {
          5, 5, 1
        };

        /* CREATE TABLE IF NOT EXISTS libres
            (_id INTEGER PRIMARY KEY AUTOINCREMENT,
            title TEXT,
            author TEXT,
            rating INTEGER);
       */

        String createTableSql = "CREATE TABLE IF NOT EXISTS libres\n" +
                "            (_id INTEGER PRIMARY KEY AUTOINCREMENT,\n" +
                "            title TEXT,\n" +
                "            author TEXT,\n" +
                "            rating INTEGER);";

        db.execSQL(createTableSql);

        // for each item in my [], let us INSERT the sql
//        for (int inc = 0; inc < titles.length; inc++) {
//            /*
//            INSERT INTO libres Values (null, "booktitle", "author", 5);
//             */
//            String temporaryQuery = "INSERT INTO libres Values (null, '" + titles[inc] + "', '" +
//                    authors[inc] + "', " + Integer.toString(ratings[inc]) + ");";
//            db.execSQL(temporaryQuery);
//        }

        mCursor = db.query("libres", null, null, null, null, null, null, null);
        mListOfBooks = (ListView) findViewById(R.id.list_of_libres);

        CursorAdapter mCursorAdapter = new CursorAdapter(MainActivity.this, mCursor, 0) {
            @Override
            public View newView(Context context, Cursor cursor, ViewGroup parent) {
                return LayoutInflater.from(context).inflate(android.R.layout.simple_list_item_1,
                        parent, false);
            }

            @Override
            public void bindView(View view, Context context, Cursor cursor) {
                TextView txt = (TextView) view.findViewById(android.R.id.text1);
                String rowData = cursor.getString(cursor.getColumnIndex("author")) +
                        " WROTE " + cursor.getString(cursor.getColumnIndex("title"));
                txt.setText(rowData);
            }
        };

        mClickListener = new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mDetailIntent = new Intent(MainActivity.this, DetailActivity.class);

                String title = mCursor.getString(mCursor.getColumnIndex("title"));
                String author = mCursor.getString(mCursor.getColumnIndex("author"));
                String rating = mCursor.getString(mCursor.getColumnIndex("rating"));
                mDetailIntent.putExtra("TITLE", title);
                mDetailIntent.putExtra("AUTHOR", author);
                mDetailIntent.putExtra("RATING", rating);
                startActivity(mDetailIntent);
            }
        };

        mListOfBooks.setAdapter(mCursorAdapter);
        mListOfBooks.setOnItemClickListener(mClickListener);

    }
}

DetailActivity


public class DetailActivity extends AppCompatActivity {

    TextView mTxtTitle;
    TextView mTxtAuthor;
    TextView mTxtRating;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);

        mTxtTitle = (TextView) findViewById(R.id.book_title);
        mTxtAuthor = (TextView) findViewById(R.id.book_author);
        mTxtRating = (TextView) findViewById(R.id.book_rating);

        Intent listIntent = getIntent();
        String title = listIntent.getStringExtra("TITLE");
        String author = listIntent.getStringExtra("AUTHOR");
        String rating = listIntent.getStringExtra("RATING");

        mTxtTitle.setText(title);
        mTxtAuthor.setText(author);
        mTxtRating.setText(rating);

    }
}

results matching ""

    No results matching ""