Android搜索历史的实现0918.docx

上传人:b****1 文档编号:3316375 上传时间:2023-05-05 格式:DOCX 页数:14 大小:23.23KB
下载 相关 举报
Android搜索历史的实现0918.docx_第1页
第1页 / 共14页
Android搜索历史的实现0918.docx_第2页
第2页 / 共14页
Android搜索历史的实现0918.docx_第3页
第3页 / 共14页
Android搜索历史的实现0918.docx_第4页
第4页 / 共14页
Android搜索历史的实现0918.docx_第5页
第5页 / 共14页
Android搜索历史的实现0918.docx_第6页
第6页 / 共14页
Android搜索历史的实现0918.docx_第7页
第7页 / 共14页
Android搜索历史的实现0918.docx_第8页
第8页 / 共14页
Android搜索历史的实现0918.docx_第9页
第9页 / 共14页
Android搜索历史的实现0918.docx_第10页
第10页 / 共14页
Android搜索历史的实现0918.docx_第11页
第11页 / 共14页
Android搜索历史的实现0918.docx_第12页
第12页 / 共14页
Android搜索历史的实现0918.docx_第13页
第13页 / 共14页
Android搜索历史的实现0918.docx_第14页
第14页 / 共14页
亲,该文档总共14页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

Android搜索历史的实现0918.docx

《Android搜索历史的实现0918.docx》由会员分享,可在线阅读,更多相关《Android搜索历史的实现0918.docx(14页珍藏版)》请在冰点文库上搜索。

Android搜索历史的实现0918.docx

Android搜索历史的实现0918

Android-搜索历史的实现-(2016-9-18)

项目:

SearchModel29环境:

AndroidStudio1.0

源码

Java

DBHelper.java

/**

*SQLite辅助类

*/

publicclassDBHelperextendsSQLiteOpenHelper{

/**

*构造器

*@paramcontext

*/

publicDBHelper(Contextcontext){

super(context,"dbkeyword",null,1);

}

@Override

publicvoidonCreate(SQLiteDatabasedb){

//创建:

建表+插入数

db.execSQL(

"createtablekw(_idintegerprimarykeyautoincrement,keywordvarchar,inputnointeger)");

//插入初始数据

db.execSQL("insertintokw(keyword,inputno)values('book','0')");

db.execSQL("insertintokw(keyword,inputno)values('书本','0')");

}

@Override

publicvoidonUpgrade(SQLiteDatabasedb,intoldVersion,intnewVersion){

//升级

}

}

Kw.java

/**

*Createdby钧on2016/9/18.

*关键词实体类

*/

publicclassKw{

publicintid;

publicStringkeyword;

publicintinputno;

publicintgetId(){

returnid;

}

publicvoidsetId(intid){

this.id=id;

}

publicStringgetKeyword(){

returnkeyword;

}

publicvoidsetKeyword(Stringkeyword){

this.keyword=keyword;

}

publicintgetInputno(){

returninputno;

}

publicvoidsetInputno(intinputno){

this.inputno=inputno;

}

@Override

publicStringtoString(){

return"Kw{"+

"id="+id+

",keyword='"+keyword+'\''+

",inputno="+inputno+

'}';

}

}

KwDao.java

/**

*Createdby钧on2016/9/18.

*封装对关键词表的数据库访问操作

*/

publicclassKwDao{

publicKwDao(){}

publicKwDao(Contextcontext){

this.context=context;

}

privateContextcontext;

privateStringtablename="kw";

/**

*判断数据库中是否存在key

*@paramkey

*@return

*/

publicbooleancontains(Stringkey){

Listlist=newArrayList();

//实例化辅助类

DBHelperhelper=newDBHelper(context);

//获取SQLite数据库对象

SQLiteDatabasedb=helper.getReadableDatabase();

//执行查询命令,接收游标返回

Cursorcursor=db.rawQuery("selectcount(*)from"+this.tablename

+"wherekeyword='"+key+"'",null);

intnumber=0;

while(cursor.moveToNext()){//moveToNext:

移动到下一行

number=cursor.getInt(0);//count(*)

}

cursor.close();//游标关闭

db.close();//db关闭

if(number==0){

returnfalse;

}else{

returntrue;

}

}

/**

*插入一个全新的关键词

*@paramkey

*@return

*/

publiclonginsert(Stringkey){

DBHelperhelper=newDBHelper(context);

SQLiteDatabasedb=helper.getReadableDatabase();

//准备插入的数据

ContentValuesvalues=newContentValues();//

values.put("keyword",key);

values.put("inputno",1);

longid=db.insert(tablename,null,values);//

Log.i("spl","insertid="+id);

db.close();//关闭连接

returnid;

}

/**

*增加某个关键词的使用频度

*@paramkey

*/

publicvoidupdate(Stringkey){

DBHelperhelper=newDBHelper(context);

SQLiteDatabasedb=helper.getReadableDatabase();

db.execSQL("updatekwsetinputno=inputno+1wherekeyword='"+key+"'");

db.close();//关闭连接

}

/**

*根据"用户的输入"返回和这个词相关的关键词集合

*@paramkey

*@return

*/

publicListselect(Stringkey){

Listlist=newArrayList();

//实例化辅助类

DBHelperhelper=newDBHelper(context);

//获取SQLite数据库对象

SQLiteDatabasedb=helper.getReadableDatabase();

//执行查询命令,接收游标返回

Cursorcursor=db.rawQuery("select*from"+this.tablename

+"wherekeywordlike'%"+key+"%'orderbyinputnodescLIMIT10",null);

while(cursor.moveToNext()){//moveToNext:

移动到下一行

Kwkw=newKw();

kw.setId(cursor.getInt(0));

kw.setKeyword(cursor.getString

(1));

kw.setInputno(cursor.getInt

(2));

list.add(kw);//添加到集合

}

cursor.close();//游标关闭

db.close();//db关闭

returnlist;

}

}

MyAdapter.java

/**

*Createdbyuiluboon2015/9/11.

*适配器

*/

publicclassMyAdapterextendsBaseAdapter{

Listlist;

LayoutInflaterinflater;

publicMyAdapter(Contextcontext){

this.inflater=LayoutInflater.from(context);

}

publicvoidsetList(Listlist){

this.list=list;

}

@Override

publicintgetCount(){

return(list==null)?

0:

list.size();//三元运算

}

@Override

publicObjectgetItem(intposition){

returnlist.get(position);

}

@Override

publiclonggetItemId(intposition){

returnposition;

}

@Override

publicViewgetView(finalintposition,ViewconvertView,ViewGroupparent){

ViewHolderholder=null;

if(convertView==null){

convertView=inflater.inflate(R.layout.item,null);

holder=newViewHolder();

holder.text=(TextView)convertView.findViewById(R.id.text);

convertView.setTag(holder);

}else{

holder=(ViewHolder)convertView.getTag();

}

Kwitem=list.get(position);

holder.text.setText(item.getKeyword());

returnconvertView;

}

publicclassViewHolder{

TextViewtext;

}

}

MainActivity.java

publicclassMainActivityextendsActivityimplementsAdapterView.OnItemClickListener{

ListViewlv;

EditTexted_input;

KwDaodao;

MyAdapteradapter;

@Override

protectedvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initView();

initData();

}

privatevoidinitData(){

adapter=newMyAdapter(this);

lv.setAdapter(adapter);

Listlist=dao.select("");

updateListView(list);

}

privatevoidinitView(){

dao=newKwDao(this);

ed_input=(EditText)findViewById(R.id.ed_input);

lv=(ListView)findViewById(R.id.lv);

lv.setOnItemClickListener(this);

//对输入框的改变进行实时监听

ed_input.addTextChangedListener(newTextWatcher(){

@Override

publicvoidbeforeTextChanged(CharSequences,intstart,intcount,intafter){

}

@Override

publicvoidonTextChanged(CharSequences,intstart,intbefore,intcount){

}

@Override

publicvoidafterTextChanged(Editables){

//在用户输入之后,按照输入显示"建议列表"

Stringkey=ed_input.getText().toString();

Listlist=dao.select(key);

updateListView(list);

}

});

}

privatevoidupdateListView(Listlist){

adapter.setList(list);

adapter.notifyDataSetChanged();

}

publicvoidbtnClick(Viewv){

//插入到数据库中

Stringkey=ed_input.getText().toString();

if(dao.contains(key)){

dao.update(key);//+1

}else{

dao.insert(key);//新词条

}

Toast.makeText(this,"查询:

"+key,Toast.LENGTH_SHORT).show();

}

@Override

publicvoidonItemClick(AdapterView

>parent,Viewview,intposition,longid){

Kwkw=(Kw)parent.getItemAtPosition(position);

Toast.makeText(this,"查询:

"+kw.getKeyword(),Toast.LENGTH_SHORT).show();

ed_input.setText(kw.getKeyword());

//执行查询

dao.update(kw.getKeyword());//+1

}

}

xml布局

Main

xmlns:

android="

xmlns:

tools="

android:

layout_width="match_parent"

android:

layout_height="match_parent"

android:

paddingLeft="@dimen/activity_horizontal_margin"

android:

paddingRight="@dimen/activity_horizontal_margin"

android:

paddingTop="@dimen/activity_vertical_margin"

android:

paddingBottom="@dimen/activity_vertical_margin"

android:

orientation="vertical"

tools:

context=".MainActivity">

android:

layout_width="match_parent"

android:

layout_height="wrap_content"

android:

orientation="horizontal">

android:

id="@+id/ed_input"

android:

layout_width="0dp"

android:

layout_weight="4"

android:

layout_height="wrap_content"/>

android:

id="@+id/btn_search"

android:

layout_width="0dp"

android:

layout_weight="1"

android:

layout_height="wrap_content"

android:

text="查询"

android:

onClick="btnClick"/>

android:

id="@+id/lv"

android:

layout_width="match_parent"

android:

layout_height="wrap_content"/>

 

item

xmlns:

android="

xmlns:

tools="

android:

layout_width="match_parent"

android:

layout_height="match_parent"

android:

paddingTop="@dimen/activity_vertical_margin"

android:

paddingBottom="@dimen/activity_vertical_margin"

android:

orientation="vertical"

tools:

context=".MainActivity">

 

android:

id="@+id/text"

android:

text="关键词"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"/>

清单

xmlversion="1.0"encoding="utf-8"?

>

android="

package="com.spl.searchmodel29">

name="android.permission.WRITE_EXTERNAL_STORAGE"/>

android:

allowBackup="true"

android:

icon="@drawable/ic_launcher"

android:

label="@string/app_name"

android:

theme="@style/AppTheme">

android:

name=".MainActivity"

android:

label="@string/app_name"

android:

windowSoftInputMode="stateAlwaysHidden|adjustResize">

name="android.intent.action.MAIN"/>

name="android.intent.category.LAUNCHER"/>

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > IT计算机 > 电脑基础知识

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2