Mm

dp = new DataPersist(this); dp.initData(); dp. setConditionToNew();

^ setConditionToNew()ffiÄi^fgfrM'NEW'SA SharedPreferences

2. A ac01 ßÄffi±®T<Go>®ffi ' I^IM^f^ onClick()®m -startActivity()^A ' »fi^t^ DispActivity ßfffi ' ft^Wg^AAAW onPause()®m ' dp f^fr«^ ' M dp fUSAM null =

4. # DispActivity InAMM®^ ac01 IA > onResume()®m ' ^A ^fsA : if( dp == null) dp = new DataPersist(this);

MdpftÄnull ' ^AAM^AMonPause()7 ' SharedPreferences^frffiAfö^A

5. ac01 f^frfP DispActiviy ^frAStlA-ffi DataPersist ^fr ' setConditionToNew() ' SharedPreferences ifrfißft^frf'

6. Hß setConditionToNew() M Ä ^ ig fr ('NEW' ^ 'OLD') SharedPreferences fMfrffi'^At^MSAMÄffi °

HÄArM^MÄjArffi^j(Commit)#AAAM ° SQLite iA^if^^iffiSSfflßftfe - AffiMTO SharedPreferences ^frM SQLite Sffl^ffi ' :

• Ö SharedPreferences

2. $PAÍ£A<Input Data>í£M

Ij TransActivity

AB^MftfMA ' UTOAfiASfA ° 5. ' M^<Commit>M® ' AlMA"COMMIT"^S > MM® ac01 :

Step-1: M X Android Ifflg^H : SQ05 ' S ^ ^ ac01.java *

DatabaseHelper.j ava - DispActivity. j ava fP TransActivity.java ^ 4 [email protected]

Step-2: MM Activity : ac01 • : // ac01.java g^«

package com.misoo.SQ05;

import android.app.Activity;

import android.content. ContentValues;

import android.content.Intent;

import android.content.SharedPreferences;

import android.content.SharedPreferences.Editor;

import android.database.sqlite.SQLiteDatabase;

import android.os.Bundle;

import android.view.View;

import android.view.ViewGroup;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.LinearLayout;

public class ac01 extends Activity implements OnClickListener {

private final int WC = ViewGroup.LayoutParams. WRAP_CONTENT; private Button btn, btn2, btn3, btn4; private String state; private DatabaseHelper mOpenHelper; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle);

LinearLayout layout = new LinearLayout(this); layout.setOrientation(LinearLayout. VERTICAL); btn = new Button(this); btn.setText("Input Data..."); btn. setOnClickListener(this);

layout.addView(btn, new LinearLayout.LayoutParams(WC, WC)); btn2 = new Button(this); btn2.setText("Query"); btn2. setOnClickListener(this);

layout.addView(btn2, new LinearLayout.LayoutParams(WC, WC)); btn3 = new Button(this); btn3.setText("Clear"); btn3. setOnClickListener(this);

layout.addView(btn3, new LinearLayout.LayoutParams(WC, WC)); btn4 = new Button(this); btn4.setText("Exit"); btn4. setOnClickListener(this);

layout.addView(btn4, new LinearLayout.LayoutParams(WC, WC)); setContentView(lay out);

Editor passwdfile = getSharedPreferences("TRANS", 0).edit(); passwdfile.putString("trans_state","INIT"); passwdfile.commit(); } @Override protected void onResume() { super.onResume();

SharedPreferences passwdfile = getSharedPreferences( "TRANS", 0);

state = passwdfile.getString("trans_state", null);

setTitle(state);

mOpenHelper = new DatabaseHelper(this); SQLiteDatabase db = mOpenHelper.getWritableDatabase(); ContentValues cv = new ContentValues(); String numb = passwdfile.getString("number", null); String na = passwdfile.getString("name", null); cv.put("number", numb); cv.put("name", na); db.insert("Employee", null, cv); } Editor editor = getSharedPreferences("TRANS", 0).edit(); editor. putString("trans_state","INIT"); editor. commit(); } public void onClick(View v){ if (v == btn){

Intent intent = new Intent();

intent.setClass(this, TransActivity.class); startActivity(intent); } else if (v == btn2){

Intent intent = new Intent();

intent. setClass(this, DispActivity.class); startActivity(intent); } else if (v == btn3){

mOpenHelper = new DatabaseHelper(this); SQLiteDatabase db = mOpenHelper.getWritableDatabase(); db.delete("Employee", null, null); m setTitle("Clear OK!"); } else if(v.equals(btn4)){ this.finish(); }

Step-3: MM SQLiteOpenHelper »ASA : DatabaseHelper ' :

package com.misoo.SQ05; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {

private static final String DB_NAME = "EmployeeDB.db"; private static final int DBVERSION = 2; private static final String TABLE_NAME = "Employee"; DatabaseHelper(Context context)

{ super(context, DB_NAME, null, DB VERSION); } @Override public void onCreate(SQLiteDatabase db) {

db.execSQL("CREATE TABLE ' ' + TABLE_NAME + " (" + "number" + " TEXT," + "name" + " TEXT," + "desc" + " TEXT" + ");"); }

@Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { }

Step-4: MM ListActivity »ASA : DispActivity ' ÄfMÄÄ^PT :

package com.misoo.SQ05;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.Map;

import android.app.ListActivity;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

import android.os.Bundle;

import android.view.View;

import android.widget. SimpleAdapter; import android.widget.ListView;

public class DispActivity extends ListActivity { private DatabaseHelper mOpenHelper;

@Override protected void onCreate(Bundle savedlnstanceState) { super.onCreate(savedlnstanceState); mOpenHelper = new DatabaseHelper(this); SQLiteDatabase db = mOpenHelper.getWritableDatabase(); String col[] = {"number", "name"};

Cursor cur = db.query("Employee", col, null, null, null, null, null); ArrayList<Map<String, Object» coll = new ArrayList<Map<String, Object>>(); Map<String, Object item; item = new HashMap<String, Object>();

item.put("c1", "--------Data-------"); coll.add(item);

cur. moveToFirst(); while(!cur.isAfterLast()) {

item = new HashMap<String, Object>(); item.put("c1", cur.getString(O) + ", " + cur.getString(l)); coll.add(item); cur.moveToNext(); } this.setListAdapter(new SimpleAdapter(this, coll, android.R. lay out. simple_list_item_1, new String[] { "cl" }, new int[] {android.R.id.text/})); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { finish(); }

Step-5: MM Activity : TransActivity ' :

// TransActivity.java fM^fif

package com.misoo.SQ05;

import android.app.Activity;

import android.content.SharedPreferences;

import android.content.SharedPreferences.Editor;

import android.os.Bundle;

import android.view.Menu;

import android.view.Menultem;

import android.view.View;

import android.view.ViewGroup;

import android.view.View.OnClickListener;

import android.widget.LinearLayout;

import android.widget.TextView;

public class TransActivity extends Activity implements OnClickListener { private final int WC = ViewGroup.LayoutParams. WRAP_CONTENT;

public static final int INPUTID = Menu.FIRST + 1; public static final int COMMITID = Menu.EIRST + 2; public static final int CANCELID = Menu.EIRST + 3; private String number, name, uc_state; TextView tx;

@Override public void onCreate(Bundle icicle) { super.onCreate(icicle);

this.retriveState(); setTitle("TransActivity");

tx = new TextView(this); tx.setText("state: " + uc_state); setContentView(tx, new LinearLayout.LayoutParams(WC, WC)); } @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); menu.add(0, INPUT ID, 0, "Input"); menu.add(0, COMMIT ID, 1, "Commit"); menu.add(0, CANCEL ID, 2, "Cancel"); return true; } public void GoTo_Input_State() { uc_state ="INPUT"; tx.setText("state: " + uc_state); number = "N10888"; name = "Linda"; } public void GoTo_Commit_State() { uc_state = "COMMIT";

Editor editor = getSharedPreferences("TRANS", 0).edit(); editor.putString("number",number); editor.putString("name",name); editor.commit(); this.finish(); } public void GoTo_Quit_State() { uc_state = "Cancel"; finish(); } @Override public void onPause() { super.onPause(); this.saveState(); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) {

case INPUT ID: { GoTo_Input_State(); return true; } case COMMIT ID: { if(uc_state != "INPUT") {

tx.setText("Input Data First!!"); return false; } GoTo_Commit_State(); return true; } case CANCELID: GoTo_Quit_State(); return true;

return super.onOptionsItemSelected(item);

private void retriveState() {

SharedPreferences passwdfile = getSharedPreferences( "TRANS", 0);

uc_state = passwdfile.getString("trans_state", null); } private void saveState() {

Editor editor = getSharedPreferences("TRANS", 0).edit(); editor.putString("trans_state",uc_state); editor. commit(); } public void onClick(View arg0) {}

1. ^ TransActivity ac01 ' g^WAAA^ ac01 » onResume()

' ft» SharedPreferences TransActivity AftSSA^Jit °

$nAA"COMMIT"^J ' ATO SharedPreferences number ^ name 0f4it ' SQLite SI4JA® -

switch (item.getItemId()) {

case COMMIT ID: {

if(uc_state != "INPUT") { tx.setText("Input Data First!!"); return false; }

GoTo_Commit_State();

return true; }

ADSim^ ' A!AMA"INPUT'm*Ait . AtbMAfiffSfMA ' TOAS

0 0

Post a comment