[source code] Android Development Tutorial - 49 to 54

+32 Bucky Roberts · January 6, 2015

MainActivity.java
package com.thenewboston.sqlitedatabaseexample;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

    EditText buckysInput;
    TextView buckysText;
    MyDBHandler dbHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        buckysInput = (EditText) findViewById(R.id.buckysInput);
        buckysText = (TextView) findViewById(R.id.buckysText);
        dbHandler = new MyDBHandler(this, null, null, 1);
        printDatabase();
    }

    //Add a product to the database
    public void addButtonClicked(View view){
        Product product = new Product(buckysInput.getText().toString());
        dbHandler.addProduct(product);
        printDatabase();
    }

    //Delete items
    public void deleteButtonClicked(View view){
        String inputText = buckysInput.getText().toString();
        dbHandler.deleteProduct(inputText);
        printDatabase();
    }

    //Print the database
    public void printDatabase(){
        String dbString = dbHandler.databaseToString();
        buckysText.setText(dbString);
        buckysInput.setText("");
    }

}


Product.java
package com.thenewboston.sqlitedatabaseexample;

//Each row in the database can be represented by an object
//Columns will represent the objects properties
public class Product {

    private int _id;
    private String _productname;

    public Product(){
    }

    public Product(String productname){
        this._productname = productname;
    }

    public void set_id(int _id) {
        this._id = _id;
    }

    public void set_productname(String _productname) {
        this._productname = _productname;
    }

    public int get_id() {
        return _id;
    }

    public String get_productname() {
        return _productname;
    }

}


MyDBHandler.java
package com.thenewboston.sqlitedatabaseexample;

import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.Cursor;
import android.content.Context;
import android.content.ContentValues;

public class MyDBHandler extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "productDB.db";
    public static final String TABLE_PRODUCTS = "products";
    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_PRODUCTNAME = "productname";

    //We need to pass database information along to superclass
    public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, DATABASE_NAME, factory, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String query = "CREATE TABLE " + TABLE_PRODUCTS + "(" +
                COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_PRODUCTNAME + " TEXT " +
                ");";
        db.execSQL(query);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS);
        onCreate(db);
    }

    //Add a new row to the database
    public void addProduct(Product product){
        ContentValues values = new ContentValues();
        values.put(COLUMN_PRODUCTNAME, product.get_productname());
        SQLiteDatabase db = getWritableDatabase();
        db.insert(TABLE_PRODUCTS, null, values);
        db.close();
    }

    //Delete a product from the database
    public void deleteProduct(String productName){
        SQLiteDatabase db = getWritableDatabase();
        db.execSQL("DELETE FROM " + TABLE_PRODUCTS + " WHERE " + COLUMN_PRODUCTNAME + "=\"" + productName + "\";");
    }

    public String databaseToString(){
        String dbString = "";
        SQLiteDatabase db = getWritableDatabase();
        String query = "SELECT * FROM " + TABLE_PRODUCTS + " WHERE 1";

        //Cursor points to a location in your results
        Cursor c = db.rawQuery(query, null);
        //Move to the first row in your results
        c.moveToFirst();

        //Position after the last row means the end of the results
        while (!c.isAfterLast()) {
            if (c.getString(c.getColumnIndex("productname")) != null) {
                dbString += c.getString(c.getColumnIndex("productname"));
                dbString += "\n";
            }
            c.moveToNext();
        }
        db.close();
        return dbString;
    }

}


activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <EditText
        android:id="@+id/buckysInput"
        android:width="300dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="35dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <Button
        android:text="Add"
        android:id="@+id/addButton"
        android:onClick="addButtonClicked"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="38dp"
        android:layout_below="@+id/buckysInput"
        android:layout_alignLeft="@+id/buckysInput"
        android:layout_alignStart="@+id/buckysInput" />

    <Button
        android:text="Delete"
        android:id="@+id/deleteButton"
        android:onClick="deleteButtonClicked"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/addButton"
        android:layout_alignRight="@+id/buckysInput"
        android:layout_alignEnd="@+id/buckysInput" />

    <TextView
        android:id="@+id/buckysText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

Post a Reply

Replies

- page 1
Oldest  Newest  Rating
+4 Rip Van Winkle · August 14, 2015
I learn much better when I type in my code myself and figure out whatever errors might be there. In any case, the code given in the tutorial videos only needed two changes (both in MyDBHandler.java):



  • In databaseToString, add c.moveToNext() to the while loop.

  • In onCreate, add a comma after AUTOINCREMENT.


   
After those two changes, it worked fine for me. (Both of those changes are already reflected in the code posted here, but since it's just a list of two things, this summary might help other people.)
+2 Garth Toland · April 1, 2015
Thank god I found this.
+2 Tomas Repisky · March 30, 2015
I finally found where I should look for these! :-)
0 Dilip Sanghvi · July 15, 2015
Can someone please help me getting the source code for all the tutorials ??
Thank You
0 Greg Murray · November 16, 2015
+prince khan 
Veryyy what...?
0 Adil Shaikh · August 8, 2015
This is unfair! I wrote the code myself after watching the tutorials and it gave me a bunch of errors! But here, what i see is an additional line "c.moveToNext()" which wasn't there in the videos...Please confirm the errors INBETWEEN the videos please!
0 Donald Genes · August 14, 2015
Nice one Bukky, God bless you
0 Akshat Bhardwaj · April 2, 2015
Where to look for the code of Android Development tutorials?
0 james fox · November 17, 2015
Whenever I run the app and use the addProduct method it will freeze and not give me any errors. Does anyone else have this issue and does anyone know what I'm doing wrong?
0 Vishesh Nagpal · February 2, 2016
add part works fine but somehow the delete part doesnt work for me.. any one who can help?

Java / Android Development

128,173 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator