總網頁瀏覽量

關於我自己

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

2012年1月6日 星期五

SQLiteOpenHelper ~ 旅遊匯率隨時查(四)











進一步修改setOnItemClickListener的選單事件, 除了「刪除」選項, 再添加「修改內容」讓User更新已輸入過的資料.

首先將.setMessage(R.string.delete)的單一選項修改為.setItems(R.array.array_onitemclick, di_onclick), 使為多選項

Activity_ExchangeRate.java
lv_shopping.setOnItemClickListener(new OnItemClickListener()
     {
   public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) 
   {
    intItemSelected = arg2;
    value_shopping_icon = allarray_shopping_icon.get(arg2);
    value_shopping_item = allarray_shopping_item.get(arg2);
    value_shopping_nt = allarray_shopping_nt.get(arg2);

    new AlertDialog.Builder(context)
    .setTitle(R.string.str_dialog_shopping_title)
    //.setMessage(R.string.delete)
          .setItems(R.array.array_onitemclick, di_onclick)
          .setNegativeButton(R.string.str_button_cancel, new DialogInterface.OnClickListener()
          {
           public void onClick(DialogInterface dialog, int whichButton)
           {
             
           }
          })
          .show();
   }      
     });
    }

在此的di_onclick是一個DialogInterface.OnClickListener物件, 用來實作setOnItemClickListener的選單事件, 故在程式中加入以下片段:

Activity_ExchangeRate.java
DialogInterface.OnClickListener di_onclick = new DialogInterface.OnClickListener()
 {
  public void onClick(DialogInterface dialog, int which)
  {  
   String[] aryShop = getResources().getStringArray(R.array.array_onitemclick);
   switch(which)
   {
    case ID_MODIFY:
     dailog_id_modify();
     break;
    case ID_DELETE:
     dailog_id_delete();                      
     break;
   }
  }
 };

為「修改內容」這個選項新增function, 並於其內載入額外設計的layout, 使之佈局如右

並將User所修改的et_dialog_shopping_modify_item.getText().toString()及

et_dialog_shopping_modify_cash.getText().toString()值
存入資料庫欄位"fn_shopping_item"和
"fn_shopping_nt"











Activity_ExchangeRate.java
public void dailog_id_modify()
 {
  LayoutInflater factory = LayoutInflater.from(context);
  view_shopping_modify = factory.inflate(R.layout.layout_dialog_shopping_modify, null);
  et_dialog_shopping_modify_item = (EditText)view_shopping_modify.findViewById(R.id.et_dialog_shopping_modify_item);
  et_dialog_shopping_modify_cash = (EditText)view_shopping_modify.findViewById(R.id.et_dialog_shopping_modify_cash);
  et_dialog_shopping_modify_item.setText(value_shopping_item);
  et_dialog_shopping_modify_cash.setText(value_shopping_nt);
  new AlertDialog.Builder(context)
  .setView(view_shopping_modify)
  .setPositiveButton(R.string.str_button_save, new DialogInterface.OnClickListener()
        {
         public void onClick(DialogInterface dialog, int whichButton)
         {
          Cursor cursor_sqlite_table_listview_shopping = null;
          try
          { 
           cursor_sqlite_table_listview_shopping = dbHelper.select(tables[3], null, null, null, null, null, null);
           cursor_sqlite_table_listview_shopping.moveToFirst();  
           String[] updateFields = {"fn_shopping_item", "fn_shopping_nt"};
           String[] updateValues = { 
                   et_dialog_shopping_modify_item.getText().toString(), 
                   et_dialog_shopping_modify_cash.getText().toString()
                 };
           String where = "fn_shopping_icon=?";
           String[] whereValue = 
           {
            value_shopping_icon
           };
           System.out.println("et_dialog_shopping_modify_item = "+et_dialog_shopping_modify_item.getText().toString());
           System.out.println("et_dialog_shopping_modify_cash = "+et_dialog_shopping_modify_cash.getText().toString());
           System.out.println("value_shopping_icon = "+value_shopping_icon);
              dbHelper.update(tables[3], updateFields, updateValues, where, whereValue);
              update_listview_shopping();
          }
          catch(Exception e)
          {
           System.out.println("821 Exception : case ID_MODIFY");
          }
          finally
          {
           cursor_sqlite_table_listview_shopping.close();
          }
         }
        })
        .setNegativeButton(R.string.str_button_cancel, new DialogInterface.OnClickListener()
        {
         public void onClick(DialogInterface dialog, int whichButton)
         {
           
         }
        })
  .show();
 }

至於case ID_DELETE:
也記得刪除資料庫資料後在更新一次ListView
新增function:

Activity_ExchangeRate.java
public void dailog_id_delete()
 {
  new AlertDialog.Builder(context)
        .setMessage("刪除資料「 "+value_shopping_item+" 」?")
        .setPositiveButton(R.string.str_button_yes, new DialogInterface.OnClickListener()
        {
         public void onClick(DialogInterface dialog, int whichButton)
         {
             Cursor cursor_sqlite_table_listview_shopping = null;
             try
             {
              cursor_sqlite_table_listview_shopping = dbHelper.select(tables[3], null, null, null, null, null, null);
           String where = "fieldname_id=?";
           cursor_sqlite_table_listview_shopping.moveToPosition(intItemSelected);
           String[] whereValue = 
           {
            cursor_sqlite_table_listview_shopping.getString(0)
           };
        dbHelper.delete(tables[3], where, whereValue);
        update_listview_shopping();
             }
             catch(Exception e)
             {
              
             }
             finally
             {
              cursor_sqlite_table_listview_shopping.close();
             }
         }
        })
        .setNegativeButton(R.string.str_button_cancel, new DialogInterface.OnClickListener()
        {
         public void onClick(DialogInterface dialog, int whichButton)
         {
           
         }
        })
        .show();
 }

沒有留言:

張貼留言