總網頁瀏覽量

關於我自己

我的相片
人生的必修課是接受無常,人生的選修課是放下執著。

2012年1月27日 星期五

SQLiteOpenHelper ~ EditText.setOnKeyListener ~ 範例: 財產分配(1)







本範例安置很多widget(不是很smart的方式), 故code很長, 分為兩篇~

預設為將財產分配以EditText呈現, 分為4個類別:遊學基金", "結婚基金", "買房基金", "養老基金", User是可以自行更改的, 變動會紀錄在SQLite中.

另外有+1萬與-1萬的ImageButton來調整資金分配, 最下方的TextView會統計總需金額~

Fund_SQLiteActivity.java

package com.tsots.Fund_SQLite;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Fund_SQLiteActivity extends Activity 
{
 @Override
 protected void onPause() 
 {
  SQLite_record_item1();
  SQLite_record_item2();
  SQLite_record_item3();
  SQLite_record_item4();
  super.onPause();
 }

 Context context = Fund_SQLiteActivity.this;
 String TAG = "Fund_SQLiteActivity.this";
 /*建立SQLite, 2個table表, 一個放存款目的紀錄, 一個放存款金額紀錄*/
 String tables[] = {"fund_data", "fund_item"};
 String fieldNames[][] =
 {
    { "fund_id", "fund_money1", "fund_money2", "fund_money3", "fund_money4"},
    { "fund_id", "fund_item1", "fund_item2", "fund_item3", "fund_item4"}
 }; 
 String fieldTypes[][] =
 {
    { "INTEGER PRIMARY KEY AUTOINCREMENT", "text", "text", "text", "text"},
    { "INTEGER PRIMARY KEY AUTOINCREMENT", "text", "text", "text", "text"}
 };
 int version = 1;
 private Fund_SQLiteOpenHelper dbHelper = new Fund_SQLiteOpenHelper (
                    this,
                    "fund_SQLite.db",
                    null, 
                    version,    
                    tables, 
                    fieldNames, 
                    fieldTypes
                     );
 EditText et_item1, et_item2, et_item3, et_item4;
 TextView tv_money1, tv_money2, tv_money3, tv_money4, tv_total;
 ImageButton ib_add1, ib_add2, ib_add3, ib_add4;
 ImageButton ib_del1, ib_del2, ib_del3, ib_del4;
 ImageView iv1_1,iv1_2,iv1_3,iv1_4,iv1_5,iv1_6,iv1_7,iv1_8,iv1_9,iv1_10;
 ImageView iv2_1,iv2_2,iv2_3,iv2_4,iv2_5,iv2_6,iv2_7,iv2_8,iv2_9,iv2_10;
 ImageView iv3_1,iv3_2,iv3_3,iv3_4,iv3_5,iv3_6,iv3_7,iv3_8,iv3_9,iv3_10;
 ImageView iv4_1,iv4_2,iv4_3,iv4_4,iv4_5,iv4_6,iv4_7,iv4_8,iv4_9,iv4_10;
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        et_item1 = (EditText) findViewById (R.id.et_item1);
        et_item2 = (EditText) findViewById (R.id.et_item2);
        et_item3 = (EditText) findViewById (R.id.et_item3);
        et_item4 = (EditText) findViewById (R.id.et_item4);
        tv_money1 = (TextView) findViewById (R.id.tv_money1);
        tv_money2 = (TextView) findViewById (R.id.tv_money2);
        tv_money3 = (TextView) findViewById (R.id.tv_money3);
        tv_money4 = (TextView) findViewById (R.id.tv_money4);
        tv_total = (TextView) findViewById (R.id.tv_total);
        ib_add1 = (ImageButton) findViewById (R.id.ib_add1);
        ib_del1 = (ImageButton) findViewById (R.id.ib_del1);
        ib_add2 = (ImageButton) findViewById (R.id.ib_add2);
        ib_del2 = (ImageButton) findViewById (R.id.ib_del2);
        ib_add3 = (ImageButton) findViewById (R.id.ib_add3);
        ib_del3 = (ImageButton) findViewById (R.id.ib_del3);
        ib_add4 = (ImageButton) findViewById (R.id.ib_add4);
        ib_del4 = (ImageButton) findViewById (R.id.ib_del4);
        iv1_1 = (ImageView) findViewById (R.id.iv1_1);
        iv1_2 = (ImageView) findViewById (R.id.iv1_2);
        iv1_3 = (ImageView) findViewById (R.id.iv1_3);
        iv1_4 = (ImageView) findViewById (R.id.iv1_4);
        iv1_5 = (ImageView) findViewById (R.id.iv1_5);
        iv1_6 = (ImageView) findViewById (R.id.iv1_6);
        iv1_7 = (ImageView) findViewById (R.id.iv1_7);
        iv1_8 = (ImageView) findViewById (R.id.iv1_8);
        iv1_9 = (ImageView) findViewById (R.id.iv1_9);
        iv1_10 = (ImageView) findViewById (R.id.iv1_10);
        iv2_1 = (ImageView) findViewById (R.id.iv2_1);
        iv2_2 = (ImageView) findViewById (R.id.iv2_2);
        iv2_3 = (ImageView) findViewById (R.id.iv2_3);
        iv2_4 = (ImageView) findViewById (R.id.iv2_4);
        iv2_5 = (ImageView) findViewById (R.id.iv2_5);
        iv2_6 = (ImageView) findViewById (R.id.iv2_6);
        iv2_7 = (ImageView) findViewById (R.id.iv2_7);
        iv2_8 = (ImageView) findViewById (R.id.iv2_8);
        iv2_9 = (ImageView) findViewById (R.id.iv2_9);
        iv2_10 = (ImageView) findViewById (R.id.iv2_10);
        iv3_1 = (ImageView) findViewById (R.id.iv3_1);
        iv3_2 = (ImageView) findViewById (R.id.iv3_2);
        iv3_3 = (ImageView) findViewById (R.id.iv3_3);
        iv3_4 = (ImageView) findViewById (R.id.iv3_4);
        iv3_5 = (ImageView) findViewById (R.id.iv3_5);
        iv3_6 = (ImageView) findViewById (R.id.iv3_6);
        iv3_7 = (ImageView) findViewById (R.id.iv3_7);
        iv3_8 = (ImageView) findViewById (R.id.iv3_8);
        iv3_9 = (ImageView) findViewById (R.id.iv3_9);
        iv3_10 = (ImageView) findViewById (R.id.iv3_10);
        iv4_1 = (ImageView) findViewById (R.id.iv4_1);
        iv4_2 = (ImageView) findViewById (R.id.iv4_2);
        iv4_3 = (ImageView) findViewById (R.id.iv4_3);
        iv4_4 = (ImageView) findViewById (R.id.iv4_4);
        iv4_5 = (ImageView) findViewById (R.id.iv4_5);
        iv4_6 = (ImageView) findViewById (R.id.iv4_6);
        iv4_7 = (ImageView) findViewById (R.id.iv4_7);
        iv4_8 = (ImageView) findViewById (R.id.iv4_8);
        iv4_9 = (ImageView) findViewById (R.id.iv4_9);
        iv4_10 = (ImageView) findViewById (R.id.iv4_10);
        SQLite_init();
        calculator_total();
        
        //載入SQLite裡的值
        try
        {
         Cursor c = dbHelper.select(tables[0], null, null, null, null, null, null);
         c.moveToFirst();
         tv_money1.setText(c.getString(1));
         show_coin1(Integer.valueOf(c.getString(1)));
         tv_money2.setText(c.getString(2));
         show_coin2(Integer.valueOf(c.getString(2)));      
         tv_money3.setText(c.getString(3));
         show_coin3(Integer.valueOf(c.getString(3)));
         tv_money4.setText(c.getString(4));
         show_coin4(Integer.valueOf(c.getString(4)));
         c.close();       
        }
        catch(Exception e)
        {
         Log.d(TAG, "Exception: " + e);
        }
        //載入SQLite裡的值
        try
        {
         Cursor c = dbHelper.select(tables[1], null, null, null, null, null, null);
         c.moveToFirst();
         et_item1.setText(c.getString(1));
         et_item2.setText(c.getString(2));     
         et_item3.setText(c.getString(3));
         et_item4.setText(c.getString(4));
         c.close();       
        }
        catch(Exception e)
        {
         Log.d(TAG, "Exception: " + e);
        }

        //監聽EditText的變化, 並更新到SQLite中
        et_item1.setOnKeyListener(new EditText.OnKeyListener()
        {
   public boolean onKey(View v, int keyCode, KeyEvent event) 
   {
    SQLite_record_item1();
    return false;
   }
  });
        et_item2.setOnKeyListener(new EditText.OnKeyListener()
        {
   public boolean onKey(View v, int keyCode, KeyEvent event) 
   {
    SQLite_record_item2();
    return false;
   }
  });
        et_item3.setOnKeyListener(new EditText.OnKeyListener()
        {
   public boolean onKey(View v, int keyCode, KeyEvent event) 
   {
    SQLite_record_item3();
    return false;
   }
  });
        et_item4.setOnKeyListener(new EditText.OnKeyListener()
        {
   public boolean onKey(View v, int keyCode, KeyEvent event) 
   {
    SQLite_record_item4();
    return false;
   }
  });

        //加一萬
        ib_add1.setOnClickListener(new ImageButton.OnClickListener()
        {
   public void onClick(View v) 
   {
    int money1 = Integer.parseInt(tv_money1.getText().toString());
    String str_money1 = String.valueOf(money1+1);
    tv_money1.setText(str_money1);
    SQLite_record1(str_money1);
    show_coin1(money1+1);
    calculator_total();
   }
  });
        //加一萬
        ib_add2.setOnClickListener(new ImageButton.OnClickListener()
        {
   public void onClick(View v) 
   {
    int money2 = Integer.parseInt(tv_money2.getText().toString());
    String str_money2 = String.valueOf(money2+1);
    tv_money2.setText(str_money2);
    SQLite_record2(str_money2);
    show_coin2(money2+1);
    calculator_total();
   }
  });
        //加一萬
        ib_add3.setOnClickListener(new ImageButton.OnClickListener()
        {
   public void onClick(View v) 
   {
    int money3 = Integer.parseInt(tv_money3.getText().toString());
    String str_money3 = String.valueOf(money3+1);
    tv_money3.setText(str_money3);
    SQLite_record3(str_money3);
    show_coin3(money3+1);
    calculator_total();
   }
  });
        //加一萬
        ib_add4.setOnClickListener(new ImageButton.OnClickListener()
        {
   public void onClick(View v) 
   {
    int money4 = Integer.parseInt(tv_money4.getText().toString());
    String str_money4 = String.valueOf(money4+1);
    tv_money4.setText(str_money4);
    SQLite_record4(str_money4);
    show_coin4(money4+1);
    calculator_total();
   }
  });
        //減一萬
        ib_del1.setOnClickListener(new ImageButton.OnClickListener()
        {
   public void onClick(View v) 
   {
    int money1 = Integer.parseInt(tv_money1.getText().toString());
    if(money1-1<0)
    {
     Toast.makeText(context, "不要負債喔", Toast.LENGTH_SHORT).show();
    }
    else
    {
     String str_money1 = String.valueOf(money1-1);
     tv_money1.setText(str_money1);
     SQLite_record1(str_money1);
     show_coin1(money1-1);
     calculator_total();
    }
   }
  });
        //減一萬
        ib_del2.setOnClickListener(new ImageButton.OnClickListener()
        {
   public void onClick(View v) 
   {
    int money2 = Integer.parseInt(tv_money2.getText().toString());
    if(money2-1<0)
    {
     Toast.makeText(context, "不要負債喔", Toast.LENGTH_SHORT).show();
    }
    else
    {
     String str_money2 = String.valueOf(money2-1);
     tv_money2.setText(str_money2);
     SQLite_record2(str_money2);
     show_coin2(money2-1);
     calculator_total();
    }
   }
  });
        //減一萬
        ib_del3.setOnClickListener(new ImageButton.OnClickListener()
        {
   public void onClick(View v) 
   {
    int money3 = Integer.parseInt(tv_money3.getText().toString());
    if(money3-1<0)
    {
     Toast.makeText(context, "不要負債喔", Toast.LENGTH_SHORT).show();
    }
    else
    {
     String str_money3 = String.valueOf(money3-1);
     tv_money3.setText(str_money3);
     SQLite_record3(str_money3);
     show_coin3(money3-1);
     calculator_total();
    }
   }
  });
        //減一萬
        ib_del4.setOnClickListener(new ImageButton.OnClickListener()
        {
   public void onClick(View v) 
   {
    int money4 = Integer.parseInt(tv_money4.getText().toString());
    if(money4-1<0)
    {
     Toast.makeText(context, "不要負債喔", Toast.LENGTH_SHORT).show();
    }
    else
    {
     String str_money4 = String.valueOf(money4-1);
     tv_money4.setText(str_money4);
     SQLite_record4(str_money4);
     show_coin4(money4-1);
     calculator_total();
    }
   }
  });
    }

    /*
     * 第一次啟動此AP時, 所有數據的初始值
     */
    public void SQLite_init()
    {
  String[] updateFields1 = {"fund_money1", "fund_money2", "fund_money3", "fund_money4"};
  String[] updateValues1 = {"0","0","0","0"};
  Cursor c1 = dbHelper.select(tables[0], null, null, null, null, null, null);
  c1.moveToFirst();
     if (c1.getCount() < 4) 
      {                            
     dbHelper.insert(tables[0], updateFields1, updateValues1);               
      }
     c1.close();
     
     String[] updateFields2 = {"fund_item1", "fund_item2", "fund_item3", "fund_item4"};
  String[] updateValues2 = {"遊學基金", "結婚基金", "買房基金", "養老基金"};
  Cursor c2 = dbHelper.select(tables[1], null, null, null, null, null, null);
  c2.moveToFirst();
     if (c2.getCount() < 4) 
      {                            
     dbHelper.insert(tables[1], updateFields2, updateValues2);               
      }
     c2.close();
    }

沒有留言:

張貼留言