[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 2
Oldest  Newest  Rating
0 Daniel Baker · August 22, 2015
OMG, that missing c.moveToNext() drove me maaad. It wasnt until i read Adil's comment that I added it to my while loop. Works perfectly now :) Thanks
0 atul yadav · October 17, 2015
thanks for the tutorial's dude 
you doing a great job ?? 
0 Sid null · October 21, 2015
we used value.put  for String . what w can we for Integer ?

Value.put show error on integer 
0 Greg Murray · November 16, 2015
+prince khan 
Veryyy what...?
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 Danielle Kapon · January 11, 2016
I'm Getting a white screen when I run the app, even though I made the necessary corrections. any idea's why? 
0 Vishesh Nagpal · February 2, 2016
add part works fine but somehow the delete part doesnt work for me.. any one who can help?
0 Topi Salmela · April 11, 2016


add part works fine but somehow the delete part doesnt work for me.. any one who can help?


Same thing for me. Been going over the source code for a while and cant seem to find the reason for this. :ermm:
0 Topi Salmela · April 11, 2016
Anyone having problems with deleteProduct() not working check that you are on MainActivity.java you are saving buckysInput to inputText instead of buckysText. Had me finding the answer for a while..?
0 Mrinal Jhamb · July 9, 2016
I want to add an activity before this.
But my application crashes while trying to switch to this activity using a button click from my home activity. 
  • 1
  • 2

Java / Android Development

106,964 followers
About

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

Links
Moderators
Bucky Roberts Administrator