Android接入WebView(一)——基本用法
Android接入WebView(二)——与JavaScript交互
Android接入WebView(三)——浏览器书签与历史记录与二维码分享
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();
}
});
}
}