Studyon Minte9.com
Android




Crud 1.0



* Add Form

             // Add article Form
             // Screen on
             // Insert in DB
             // Message success

	%--- activity_main.xml

		<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
		    xmlns:tools="http://schemas.android.com/tools"
		    android:orientation="vertical"
		    android:layout_width="fill_parent"
		    android:layout_height="wrap_content"
		    tools:context=".MainActivity">
		    
		    <LinearLayout
			android:id="@+id/group"
			android:layout_width="fill_parent"
			android:layout_height="wrap_content"
			android:orientation="vertical">

			<!-- Input text - Title -->
			
			<EditText 
			    android:id="@+id/title"
					android:layout_width="fill_parent"
					android:layout_height="wrap_content"
					android:hint="Title"
					android:inputType="text"
			    />

			 <!-- Button - Add -->

			<Button 
			    android:id="@+id/add"
				android:layout_width="wrap_content"
				android:layout_height="wrap_content"
				android:text="Add"
				android:onClick="addArticle"
			    />
			
		    </LinearLayout>
			
		</LinearLayout>

	%--- Article.java

		package com.example.myapp;

		public class Article { // Article Object
			
			private long id;
			private String title;
			
			public long getId() {
				return id;
			}
			
			public void setId(long id) {
				this.id = id;
			}
			
			public String getTitle() {
				return title;
			}
			
			public void setTitle(String title) {
				this.title = title;
			}
		}

	%--- MainActivity.java

		package com.example.myapp;
		import android.app.Activity;
		import android.os.Bundle;
		import android.view.View;
		import android.view.WindowManager;
		import android.widget.Toast;

		public class MainActivity extends Activity {
			
			ArticleDataAccess dao;
			
			Boolean debug = true;

			@Override
		        protected void onCreate(Bundle state) {
				
				super.onCreate(state);
				//setContentView(R.layout.activity_main);
				
				//SCREEN ON (degug only)
				if (debug) {
					getWindow().addFlags(
					/WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|
					WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD|
					WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED|
					WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
				}

				setContentView( R.layout.activity_main);
				
				//OPEN (db connection)
				dao = new ArticleDataAccess(this);
				dao.open();
				
				//SHOW (article list)
			}
			
			public void addArticle (View view) {
				try {
					//INSERT (article)
					EditText mEdit = (EditText) findViewById(R.id.title);
					String title = mEdit.getText().toString();
					dao.insert(title);
						Toast.makeText(getApplicationContext(), "Inserted - " + title, 
                                                          Toast.LENGTH_LONG).show();
					
				} catch (Exception e) {
					//Log.i("myapp", "Insert failed - addArticle");
				}
				
			}
		}

	%--- MySQLHelper.java

		package com.example.myapp;

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

		public class MySQLiteHelper extends SQLiteOpenHelper {
			
			public static final String TB_ARTICLE = "article";
			
			private static final String DB_NAME = "articles.db";
			private static final int DB_VERSION = 1;
			
			public MySQLiteHelper(Context context) {
				super(context, DB_NAME, null, DB_VERSION);
			}
			
			@Override
			public void onCreate(SQLiteDatabase db) {
				
				String sql = "CREATE TABLE " + TB_ARTICLE + " (" +
						"_id integer primary key autoincrement," +
						"title varchar not null" +
						")";
				
				db.execSQL(sql);
			}
			
			@Override
			public void onUpgrade(SQLiteDatabase db, int version_old, int version_new) {
				
			}
			
		}

	%--- ArticleDataAccess.java

		package com.example.myapp;

		import android.content.ContentValues;
		import android.content.Context;
		import android.database.Cursor;
		import android.database.sqlite.SQLiteDatabase;
		import android.util.Log;

		public class ArticleDataAccess {
			
			private MySQLiteHelper dbHelper;
			private SQLiteDatabase db;
			
			public ArticleDataAccess(Context context) {
				dbHelper = new MySQLiteHelper(context);
			}
			
			public void open() {
				db = dbHelper.getWritableDatabase();
			}
			
			public void close() {
				dbHelper.close();
			}
			
			public Article insert(String title) {

				//Insert
				ContentValues values = new ContentValues();
				values.put("title", title);
				long insertId = db.insert(MySQLiteHelper.TB_ARTICLE, null, values);
				
					Log.i("catalin", "insertId: " + insertId);
				
				//Get last inserted
				Cursor cursor = db.rawQuery(
                                      "SELECT * FROM " + MySQLiteHelper.TB_ARTICLE + 
                                      " WHERE _id = " + insertId, null);
				cursor.moveToFirst();
				Article article = cursorToArticle(cursor);
				
				cursor.close();
				
				return article;
			}
			
			private Article cursorToArticle(Cursor cursor) {
				Article article = new Article();
				article.setId(cursor.getLong(0));
				article.setTitle(cursor.getString(1));
				return article;
			}
			
			public void delete(Article article) {
				
			}
			
			public void update(Article article) {
				
			}
		}


http://www.vogella.com/articles/AndroidSQLite/article.html#databasetutorial_intro