這篇新增加一個可以自行輸入購買商品資訊的功能,
讓外出旅遊除了可以隨時計算匯率,
更可以紀錄自己敗了哪些東西啦~
為此,
除了修改layout (layout_exchangerate.xml),
再新增Adapter_ListView_Shopping.java來自訂第2個ListView的樣式, 其載入layout_adapter_listview_shopping.xml 內含三個TextView,
code的架構如下
Adapter_ListView_Shopping.java
package com.tsots.ExchangeRate; import java.util.List; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; public class Adapter_ListView_Shopping extends BaseAdapter { private LayoutInflater mInflater; Listadapter_allarray_shopping_icon; List adapter_allarray_shopping_item; List adapter_allarray_shopping_nt; TextView column_shopping_icon; TextView column_shopping_item; TextView column_shopping_nt; public Adapter_ListView_Shopping ( Context context, int simple_list_item_single_choice, List allarray_shopping_icon, List allarray_shopping_item, List allarray_shopping_nt ) { mInflater = LayoutInflater.from(context); adapter_allarray_shopping_icon = allarray_shopping_icon; adapter_allarray_shopping_item = allarray_shopping_item; adapter_allarray_shopping_nt = allarray_shopping_nt; } public int getCount() { return adapter_allarray_shopping_item.size(); } public Object getItem(int position) { return adapter_allarray_shopping_item.size(); } public long getItemId(int position) { return position; } public View getView(int position,View convertView,ViewGroup parent) { convertView = mInflater.inflate(R.layout.layout_adapter_listview_shopping,null); column_shopping_icon = (TextView)convertView.findViewById(R.id.column_shopping_icon); column_shopping_item = (TextView)convertView.findViewById(R.id.column_shopping_item); column_shopping_nt = (TextView)convertView.findViewById(R.id.column_shopping_nt); column_shopping_icon.setText(adapter_allarray_shopping_icon.get(position).toString()); column_shopping_item.setText(adapter_allarray_shopping_item.get(position).toString()); column_shopping_nt.setText(adapter_allarray_shopping_nt.get(position).toString()); return convertView; } }
在主程式的部份,
新增bt_save button的onClick事件,
將user所輸入的et_item和et_money值存入資料庫並更新到ListView (lv_shopping)
新增bt_save button的onClick事件,
將user所輸入的et_item和et_money值存入資料庫並更新到ListView (lv_shopping)
Activity_ExchangeRate.java
bt_save.setOnClickListener(new OnClickListener() { public void onClick(View v) { et_item.getText().toString(); et_money.getText().toString(); //存入資料庫 save_sqlite_table_listview_shopping(); //另外呼叫function做setAdapter update_listview_shopping(); } });
/*
* 更新ListView畫面資料
*/
Activity_ExchangeRate.java
public void update_listview_shopping() { Log.i(Tag, "設定listview_shopping內容"); allarray_shopping_icon = new ArrayList(); allarray_shopping_item = new ArrayList (); allarray_shopping_nt = new ArrayList (); 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(); do { allarray_shopping_icon.add(cursor_sqlite_table_listview_shopping.getString(0)); allarray_shopping_item.add(cursor_sqlite_table_listview_shopping.getString(2)); allarray_shopping_nt.add(cursor_sqlite_table_listview_shopping.getString(3)); }while(cursor_sqlite_table_listview_shopping.moveToNext()); } catch(Exception e) { System.out.println("501 Exception : update_listview()"); } finally { cursor_sqlite_table_listview_shopping.close(); } /*新增購物列表*/ adapter_listview_shopping = new Adapter_ListView_Shopping ( context, android.R.layout.simple_list_item_1, allarray_shopping_icon, allarray_shopping_item, allarray_shopping_nt ); //將ListAdapter的可選(Focusable)選單選項打開 lv_shopping.setItemsCanFocus(true); lv_shopping.setFocusable(true); //設定ListView選單選項設為每次只能單一選項 lv_shopping.setChoiceMode(ListView.CHOICE_MODE_SINGLE); lv_shopping.setAdapter(adapter_listview_shopping); }
/*
* 新增tables[3]內的資料
* {"fieldname_id", "fn_shopping_icon", "fn_shopping_item", "fn_shopping_nt"}
*/
Activity_ExchangeRate.java
public void save_sqlite_table_listview_shopping() { Log.i(Tag, "儲存tables[3]資料ing..."); 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_icon", "fn_shopping_item", "fn_shopping_nt"}; String[] updateValues = {"1", et_item.getText().toString(), et_money.getText().toString()}; dbHelper.insert(tables[3], updateFields, updateValues); //dbHelper.update(tables[3], updateFields, updateValues, null, null); } catch(Exception e) { System.out.println("219 Exception : save_sqlite_table_listview_shopping()"); } finally { cursor_sqlite_table_listview_shopping.close(); } }
最後加入lv_shopping的onItemClick事件,
讓user可以將加入過的資料做刪除
Activity_ExchangeRate.java
lv_shopping.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) { intItemSelected = arg2; new AlertDialog.Builder(context) .setMessage("delete item "+arg2+" ?") .setPositiveButton("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=?"; String[] whereValue = { allarray_shopping_icon.get(intItemSelected) }; dbHelper.delete(tables[3], where, whereValue); update_listview_shopping(); } catch(Exception e) { } finally { cursor_sqlite_table_listview_shopping.close(); } } }) .setNegativeButton("no", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { } }) .show(); } });
為使lv_shopping的"fn_shopping_icon"在Activity顯示重新編號, 修改以下部份
public void update_listview_shopping() { ... try { cursor_sqlite_table_listview_shopping = dbHelper.select(tables[3], null, null, null, null, null, null); cursor_sqlite_table_listview_shopping.moveToFirst(); int count =0; do { count++; //allarray_shopping_icon.add(cursor_sqlite_table_listview_shopping.getString(0)); allarray_shopping_icon.add(String.valueOf(count)); allarray_shopping_item.add(cursor_sqlite_table_listview_shopping.getString(2)); allarray_shopping_nt.add(cursor_sqlite_table_listview_shopping.getString(3)); }while(cursor_sqlite_table_listview_shopping.moveToNext()); } ... }
lv_shopping.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) { ... 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 = { //allarray_shopping_icon.get(intItemSelected) cursor_sqlite_table_listview_shopping.getString(0) }; dbHelper.delete(tables[3], where, whereValue); update_listview_shopping(); } });
沒有留言:
張貼留言