iT邦幫忙

0

在Android Studio 3.x版開發Android系統的開發記事-如何使用SQLite(六)

比賽結束後,我看了一下程式,才發現,沒有寫到刪除的功能,這樣,就不夠完整。所以,這個章節,就來將最後一個功能,刪除完成。

所以,此功能,執行的方式為,在ListView,如果長時間按著某一個項目,就會出現是否確定要刪除的訊息,再確定是否要刪除此資料。不過,如果要開發此功能,就要重新更新列表的程式架構。

而且,刪除此資料後,要重新整理列表的資料,測試了不少方式,發現用執行緒來重新整理是比較好的方式。先看一下,完整的程式碼:
public class frm_cust_list extends AppCompatActivity implements ListView.OnItemClickListener,ListView.OnItemLongClickListener {

    //宣告物件
    private SQLiteDatabase db = null;
    private Cursor cursor = null;
    private SimpleCursorAdapter adapter = null;

    ListView lsv_cust;

    private String strid = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_frm_cust_list);

        lsv_cust = (ListView)findViewById(R.id.lsvcust);
        lsv_cust.setOnItemClickListener(this);
        lsv_cust.setOnItemLongClickListener(this);

        //建立SQLite 資料庫的實體檔案
        db = openOrCreateDatabase("cust.db", Context.MODE_PRIVATE,null);

        //建立資料表的SQL語法
        String strcreatedb = "CREATE TABLE IF NOT EXISTS " +
                "tcustomer (_id INTEGER PRIMARY KEY, cname TEXT, ctel TEXT) ";

        //執行SQL語法,建立資料表
        db.execSQL(strcreatedb);

        //查詢資料
        cursor = db.rawQuery("SELECT * FROM tcustomer",null);

        //如果查詢有資料的話,就跟ListView做連結
        if (cursor != null && cursor.getCount() >=0)
        {
            adapter = new SimpleCursorAdapter(this,
                android.R.layout.simple_list_item_2,
                cursor,
                new String[] {"_id","cname"},
                new int[] {android.R.id.text1,android.R.id.text2},
                0);

            lsv_cust.setAdapter(adapter);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        //取得按下ListView的那個Item的值。
        TextView txtid = (TextView) view.findViewById(android.R.id.text1);

        //prc_showmessage("id:" + txtid.getText().toString() + " " );

        //按下Item時,要呼叫編輯的Activity,利用Intent物件帶參數過去。
        Intent it = new Intent();
        it= new Intent(this,frm_cust_edit.class);

        it.putExtra("pid",txtid.getText().toString());

        startActivity(it);

        //關閉本身的Activity。
        this.finish();

    }

    public void prc_btnadd(View v)
    {
        //按下新增時,要呼叫新增的Activity,利用Intent物件。
        Intent it = new Intent();
        it= new Intent(this,frm_cust_add.class);

        startActivity(it);

        //關閉本身的Activity。
        this.finish();
    }

    //顯示訊息
    public void prc_showmessage(String strmessage)
    {
        Toast objtoast = Toast.makeText(this,strmessage, Toast.LENGTH_SHORT);
        objtoast.show();
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

        //取得按下ListView的那個Item的值。
        TextView txtid = (TextView) view.findViewById(android.R.id.text1);

        strid = txtid.getText().toString();

        new AlertDialog.Builder(this)
                .setTitle("刪除資料")
                .setMessage("您確定要刪除資料?")
                .setPositiveButton("是", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        String strupdatesql = "DELETE from tcustomer WHERE _id=" + strid;

                        db.execSQL(strupdatesql);

                        //透過執行緒,更新ListView的資料。
                        new RefreshList().execute();
                    }
                })
                .setNegativeButton("否", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })
                .show();

        //要設定成true,才不會觸發到onitemclick
        return true;
    }

    private class RefreshList extends AsyncTask<Void,Void,Cursor> {

        //通過執行緒AsyncTask來讀取資料庫,再更新Cursor。

        @Override
        protected Cursor doInBackground(Void... voids) {
            //在執行緒中,重新取得資料庫的資料,再回傳新的Cursor。
            Cursor newCurosr = db.rawQuery("SELECT * FROM tcustomer",null);
            return newCurosr;
        }

        protected void onPostExecute(Cursor newCursor) {
            //更新Cursor,再關才原來的Curosr。
            adapter.changeCursor(newCursor);
            cursor.close();
            cursor = newCursor;
        }
    }
}

執行出來的列表畫面,如下圖所示:
https://ithelp.ithome.com.tw/upload/images/20181114/20000953nmM51QGQhb.jpg

在列表資料,如果長時間按著某一個項目,就會出現是否確定要刪除的訊息,再確定是否要刪除此資料。按下「是」,就會直接刪除此資料,再重新整理列表資料。
https://ithelp.ithome.com.tw/upload/images/20181114/200009532pWOmtJIvN.jpg

重新整理的列表資料:
https://ithelp.ithome.com.tw/upload/images/20181114/20000953D1J26Q3ZkV.jpg

終於寫好了,比賽後的第一篇文章。後續,預計將要重新整理我之前寫的記帳系統的需求分析、系統分析、系統設計、撰寫程式、封裝上市。這次會再簡化、依新版的Android,重新設計介面及撰寫程式等步驟。


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言