1. 程式人生 > >Android接入WebView(四)——瀏覽器書籤與歷史記錄詳細處理

Android接入WebView(四)——瀏覽器書籤與歷史記錄詳細處理

最近寫了一個基於webview和zxing,sqlite實現的一款瀏覽器

主要功能有:掃描二維碼以及開啟本地二維碼載入網頁,書籤和歷史記錄管理,無痕瀏覽,分享網址,分享網址二維碼,瀏覽器出錯自定義等功能。

前段時間很忙,現在終於有時間來寫寫書籤和歷史記錄的詳細處理了

由於書籤處理和歷史記錄處理類似,我這裡只寫怎麼處理歷史記錄,總共包含如下幾個函式:

實現的功能:顯示歷史記錄並點選可進入,長按出現功能選單,以及檢視詳細資訊:

          

好了,現在說幾個重要的,文末會給出全部程式碼

我們進入history Activity後,會顯示最近瀏覽過的網址,按訪問時間逆序排序,即最後訪問的出現在最前面,這也符合大多數人的使用習慣。

這個函式實現的功能就是逆序查詢資料庫:

如果返回不為空,布林變數if_exsit就置為true,再把返回資料繫結到listview上;

如果返回為空,布林變數if_exsit就置為false,並把listview設定為預設的內容

map.put("title", "暫時沒有瀏覽記錄");
map.put("url", "此處是存放您歷史記錄地方");
//資料庫逆序查詢函式:
    public void queryinfo(){
        final ArrayList<HashMap<String, Object>> listItem = new ArrayList <HashMap<String,Object>>();/*在陣列中存放資料*/
        //第二個引數是資料庫名
        dbHelper = new MyDatabaseHelper(this,"usersDataBase",null,1);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        //Cursor cursor = db.rawQuery("select * from bookmarkDB", null);
        //查詢語句也可以這樣寫
        Cursor cursor = db.query("historyDB", null, null, null, null, null, "id desc");
        if (cursor != null && cursor.getCount() > 0) {
            if_exsit=true;
            while(cursor.moveToNext()) {
                get_id=cursor.getInt(0);//得到int型的自增變數
                get_title = cursor.getString(1);
                get_url = cursor.getString(2);
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("title", get_title);
                map.put("url", get_url);
                arrayList.add(get_id);
                listItem.add(map);
                //new String  資料來源, new int 資料到哪去
                SimpleAdapter mSimpleAdapter = new SimpleAdapter(this,listItem,R.layout.list_item,
                        new String[] {"title","url"},
                        new int[] {R.id.textView2,R.id.textView3});
                lv.setAdapter(mSimpleAdapter);//為ListView繫結介面卡
            }
        }
        else {
            if_exsit=false;
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("title", "暫時沒有瀏覽記錄");
            map.put("url", "此處是存放您歷史記錄地方");
            listItem.add(map);
            //new String  資料來源, new int 資料到哪去
            SimpleAdapter mSimpleAdapter = new SimpleAdapter(this,listItem,R.layout.item_list,
                    new String[] {"title","url"},
                    new int[] {R.id.textView2,R.id.textView3});
            lv.setAdapter(mSimpleAdapter);//為ListView繫結介面卡
        }

        cursor.close();
        db.close();
    }

然後是刪除資料後的處理:

刪除資料後,我們要重新整理一次UI,處理如下:

最後兩行程式碼是清空與listview繫結的ArrayList,並且重新執行queryinfo()載入UI

 //刪除函式:
    public void delete(int position){
        dbHelper = new MyDatabaseHelper(this,"usersDataBase",null,1);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.delete("historyDB","id=?",new String[] {String.valueOf(arrayList.get(position))});
        db.close();
        Toast.makeText(this,"刪除成功",Toast.LENGTH_SHORT).show();
        //刪除後清空陣列,重新放入資料,重新整理UI
        arrayList.clear();
        queryinfo();
    }

然後是點選listview,當if_exsit為true的時候才返回MainActivity載入網頁:

if (if_exsit) {
                    Intent mIntent = new Intent();//沒有任何引數(意圖),只是用來傳遞資料
                    mIntent.putExtra("2_data_return", query_by_id(position));
                    setResult(RESULT_OK, mIntent);
                    finish();
                }

然後是檢視詳細資訊:

在history裡面,詳細資訊顯示為TextView,不可編輯,在bookmark裡面,為EditText,可以編輯

//url詳細資訊對話方塊
    public void url_infomation(final int position){
        final Dialog dialog = new Dialog(this, R.style.NormalDialogStyle);
        View view = View.inflate(this, R.layout.history_url_informatiom, null);
        text_title = (TextView) view.findViewById(R.id.textView4);
        text_url = (TextView) view.findViewById(R.id.textView5);
        dialog.setContentView(view);
        //使得點選對話方塊外部可消失對話方塊
        dialog.setCanceledOnTouchOutside(true);
        //設定對話方塊的大小
        view.setMinimumHeight((int) (ScreenSizeUtils.getInstance(this).getScreenHeight() * 0.23f));
        Window dialogWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (ScreenSizeUtils.getInstance(this).getScreenWidth() * 0.75f);
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.CENTER;
        dialogWindow.setAttributes(lp);
        //顯示詳細資訊
        text_url.setText(query_by_id(position));
        text_title.setText(titler);
        dialog.show();

    }

下面給出完整history Activity的程式碼:

package mountain_hua.browser;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by dhs on 2018/7/27.
 */

public class history extends AppCompatActivity {
    private ListView lv;
    private MyDatabaseHelper dbHelper;
    private String get_title,get_url;//暫存從資料庫得到的title和url
    private int get_id; //暫存從資料庫得到的id
    private String titler, url;//按值查詢詳細資訊
    int id; //按值查詢id
    private TextView text_title, text_url;//書籤詳細資訊edittext
    private ArrayList arrayList=new ArrayList();
    private boolean if_exsit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.history);

        lv=(ListView)findViewById(R.id.history_list);
        queryinfo();
        //點選函式:
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if (if_exsit) {
                    Intent mIntent = new Intent();//沒有任何引數(意圖),只是用來傳遞資料
                    mIntent.putExtra("2_data_return", query_by_id(position));
                    setResult(RESULT_OK, mIntent);
                    finish();
                }


            }
        });
        //長按選擇刪除
        lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                if (if_exsit)
                dialog_bottom(position);
                return true;
            }
        });

    }

    //刪除函式:
    public void delete(int position){
        dbHelper = new MyDatabaseHelper(this,"usersDataBase",null,1);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.delete("historyDB","id=?",new String[] {String.valueOf(arrayList.get(position))});
        db.close();
        Toast.makeText(this,"刪除成功",Toast.LENGTH_SHORT).show();
        //刪除後清空陣列,重新放入資料,重新整理UI
        arrayList.clear();
        queryinfo();
    }

    //全部刪除
    public void delete_all(){
        dbHelper = new MyDatabaseHelper(this,"usersDataBase",null,1);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        Cursor cursor = db.rawQuery("select * from historyDB", null);
        while (cursor.moveToNext()) {
            id=cursor.getInt(0);
            db.delete("historyDB","id=?",new String[] {String.valueOf(id)});
        }
        cursor.close();
        db.close();
        Toast.makeText(this,"刪除成功",Toast.LENGTH_SHORT).show();
        //刪除後清空陣列,重新放入資料,重新整理UI
        arrayList.clear();
        queryinfo();
    }

    //全部刪除對話方塊
    public void delete_all_dialog(){
        AlertDialog dialog = new AlertDialog.Builder(this)
                .setTitle("確定全部刪除嗎?")//設定對話方塊的標題
                //設定對話方塊的按鈕
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .setPositiveButton("確定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        delete_all();
                        dialog.dismiss();
                    }
                }).create();
        dialog.show();

    }

    //資料庫逆序查詢函式:
    public void queryinfo(){
        final ArrayList<HashMap<String, Object>> listItem = new ArrayList <HashMap<String,Object>>();/*在陣列中存放資料*/
        //第二個引數是資料庫名
        dbHelper = new MyDatabaseHelper(this,"usersDataBase",null,1);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        //Cursor cursor = db.rawQuery("select * from bookmarkDB", null);
        //查詢語句也可以這樣寫
        Cursor cursor = db.query("historyDB", null, null, null, null, null, "id desc");
        if (cursor != null && cursor.getCount() > 0) {
            if_exsit=true;
            while(cursor.moveToNext()) {
                get_id=cursor.getInt(0);//得到int型的自增變數
                get_title = cursor.getString(1);
                get_url = cursor.getString(2);
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("title", get_title);
                map.put("url", get_url);
                arrayList.add(get_id);
                listItem.add(map);
                //new String  資料來源, new int 資料到哪去
                SimpleAdapter mSimpleAdapter = new SimpleAdapter(this,listItem,R.layout.list_item,
                        new String[] {"title","url"},
                        new int[] {R.id.textView2,R.id.textView3});
                lv.setAdapter(mSimpleAdapter);//為ListView繫結介面卡
            }
        }
        else {
            if_exsit=false;
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("title", "暫時沒有瀏覽記錄");
            map.put("url", "此處是存放您歷史記錄地方");
            listItem.add(map);
            //new String  資料來源, new int 資料到哪去
            SimpleAdapter mSimpleAdapter = new SimpleAdapter(this,listItem,R.layout.item_list,
                    new String[] {"title","url"},
                    new int[] {R.id.textView2,R.id.textView3});
            lv.setAdapter(mSimpleAdapter);//為ListView繫結介面卡
        }

        cursor.close();
        db.close();
    }

    //按值查詢:
    public String query_by_id(int position){
        dbHelper = new MyDatabaseHelper(this,"usersDataBase",null,1);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        Cursor cursor = db.rawQuery("select * from historyDB", null);
        while (cursor.moveToNext()) {
            url = cursor.getString(2);
            titler=cursor.getString(1);
            id=cursor.getInt(0);
            //找到id相等的就返回url
            if (arrayList.get(position).equals(id))
                break;
        }
        cursor.close();
        db.close();
        return url;
    }

    //url詳細資訊對話方塊
    public void url_infomation(final int position){
        final Dialog dialog = new Dialog(this, R.style.NormalDialogStyle);
        View view = View.inflate(this, R.layout.history_url_informatiom, null);
        text_title = (TextView) view.findViewById(R.id.textView4);
        text_url = (TextView) view.findViewById(R.id.textView5);
        dialog.setContentView(view);
        //使得點選對話方塊外部可消失對話方塊
        dialog.setCanceledOnTouchOutside(true);
        //設定對話方塊的大小
        view.setMinimumHeight((int) (ScreenSizeUtils.getInstance(this).getScreenHeight() * 0.23f));
        Window dialogWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (ScreenSizeUtils.getInstance(this).getScreenWidth() * 0.75f);
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.CENTER;
        dialogWindow.setAttributes(lp);
        //顯示詳細資訊
        text_url.setText(query_by_id(position));
        text_title.setText(titler);
        dialog.show();

    }

    //底部對話方塊
    public void dialog_bottom(final int position){
        //彈出對話方塊
        final Dialog dialog = new Dialog(this, R.style.NormalDialogStyle);
        View view = View.inflate(this, R.layout.history_dialog_bottom, null);
        dialog.setContentView(view);
        dialog.setCanceledOnTouchOutside(true);
        view.setMinimumHeight((int) (ScreenSizeUtils.getInstance(this).getScreenHeight() * 0.23f));
        Window dialogWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (ScreenSizeUtils.getInstance(this).getScreenWidth() * 0.9f);
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.BOTTOM;
        dialogWindow.setAttributes(lp);
        dialog.show();
        //點選事件
        view.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //取消對話方塊
                dialog.dismiss();
            }
        });
        view.findViewById(R.id.delete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //刪除制定position,並取消對話方塊
                delete(position);
                dialog.dismiss();

            }
        });
        view.findViewById(R.id.lookup).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //檢視、修改
                dialog.dismiss();
                url_infomation(position);
            }
        });
        view.findViewById(R.id.clear_all).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //全部刪除
                dialog.dismiss();
                delete_all_dialog();

            }
        });
    }

}