技术标签: java html5 android android搜索框 数据库
最近自己在尝试做app开发,遇到搜索框功能,便查找了一下但是感觉自己想的或许更好理解和记住,便自己思考了一下。废话不多说,下面是实现代码,供大家参考,有待改进。
先说一下我整体思路,因为刚开始写所以相关数据都没有上传服务器过。首先建立一个数据库,将可以搜索的相关内容存储到数据库的搜索表当中,然后在搜索框中获取输入的第一个字符,按照字符搜索相关内容。同时创建历史搜索表,将搜索过的内容放入到搜索历史表当中去。每一次进入搜索页面都从搜索历史表当中获取之前的搜索历史,点击清空搜索历史将删除表中的所有内容。
这是我点击跳转到搜索界面,只需要关注最顶上即可
其中第一步就是自定listview布局,这一块一搬自定义的大多数相同
package com.example.tjtcexample.subsidiary.services1.search;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;
public class ListViewForScrollView extends ListView {
public ListViewForScrollView(Context context) {
super(context);
}
public ListViewForScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ListViewForScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expected=MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2,MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expected);
}
}
然后在xml中建立布局,效果见上图
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="1"
android:background="@drawable/back"
android:layout_gravity="center"
android:id="@+id/iv_searchback"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="8"
android:orientation="horizontal"
android:background="@drawable/search_round"
android:id="@+id/linear_searchitem">
<EditText
android:id="@+id/et_searchtext"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:hint="输入关键字搜索"
android:background="@null"
android:textSize="18sp"
android:drawableLeft="@android:drawable/ic_menu_search"
android:singleLine="true"
android:imeOptions="actionSearch"
/>
<Button
android:id="@+id/btn_search"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="搜索"
android:textSize="18sp"
android:background="@drawable/btn_round"/>
</LinearLayout>
</LinearLayout>
<com.example.tjtcexample.subsidiary.services1.search.ListViewForScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/search_listview"/>
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="45dp"
android:text="搜索历史"
android:textSize="18sp"
android:layout_gravity="center"
android:gravity="center"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tv_searchhistory"
android:background="@color/teal_200"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="@color/gray"/>
<TextView
android:id="@+id/tv_clearsearch"
android:layout_width="match_parent"
android:layout_height="45dp"
android:text="清空搜索历史"
android:textSize="18sp"
android:layout_gravity="center"
android:gravity="center"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
public class SearchActivity extends AppCompatActivity {
private ImageView iv_searchBack;
private Button btn_search;
private EditText et_searchText;
private ListViewForScrollView listViewForScrollView;
private TextView tv_historyText,tv_clearHistory;
private List<String> searchList=new ArrayList<>();
private int count=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
initView();
setListeners();
}
/**
* 获取相对应的控件
*/
private void initView() {
iv_searchBack=findViewById(R.id.iv_searchback);
btn_search=findViewById(R.id.btn_search);
et_searchText=findViewById(R.id.et_searchtext);
listViewForScrollView=findViewById(R.id.search_listview);
tv_historyText=findViewById(R.id.tv_searchhistory);
tv_clearHistory=findViewById(R.id.tv_clearsearch);
}
/**
* 实现搜索功能
*/
private void setListeners() {
/**
* 存放搜索历史的表
*/
SQLiteOpenHelper helper=SearchSQLiteOpenHelper.getmInstance(SearchActivity.this);
/**
* 返回服务页面
*/
iv_searchBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(SearchActivity.this, Services1Activity.class);
startActivity(intent);
}
});
/**
* 给搜索历史传入空
*/
tv_historyText.setText(" ");
/**
* 搜索按钮的监听
*/
btn_search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String obtain=et_searchText.getText().toString().trim();
/**
* 下一次点击搜索按钮时清空前一次搜索列表
*/
count++;
if(count%1==0){
searchList.clear();
}
/**
* 将搜索框内容放入到搜索历史当中去
*/
tv_historyText.append(obtain+" ");
/**
* 将搜索框内容放入到搜索历史表当中去
*/
SQLiteDatabase db_history=helper.getWritableDatabase();
if(db_history.isOpen()){
String add_historysearchname_sql="insert into historysearch(historyname) values(?);";
db_history.execSQL(add_historysearchname_sql,new Object[]{obtain});
Toast.makeText(SearchActivity.this,"增加成功",Toast.LENGTH_SHORT).show();
}
db_history.close();
/**
* 判断搜索框是否为空
*/
if(obtain.isEmpty()){
Toast.makeText(SearchActivity.this,"搜索框为空",Toast.LENGTH_SHORT).show();
searchList.clear();
}else{
/**
* 获取数据库中的表,取出搜索框中的首字符放入查询语句进行查询相匹配的内容
*/
SQLiteDatabase db_search=helper.getReadableDatabase();
if(db_search.isOpen()){
String firstChar=obtain.substring(0,1);
String query_sql="select * from search where searchname like '"+firstChar+"%'";
Cursor cursor = db_search.rawQuery(query_sql,null);
if(cursor.getCount()==0){
Toast.makeText(SearchActivity.this,"没有该服务",Toast.LENGTH_SHORT).show();
}else{
cursor.moveToFirst();
String searchname=cursor.getString(cursor.getColumnIndex("searchname"));
searchList.add(searchname);
}
while(cursor.moveToNext()){
String searchname1=cursor.getString(cursor.getColumnIndex("searchname"));
searchList.add(searchname1);
}
cursor.close();
}
db_search.close();
}
/**
* 自定义搜索适配器,将适配器放入自定义的listview当中
*/
SearchBaseAdapter searchBaseAdapter=new SearchBaseAdapter();
listViewForScrollView.setAdapter(searchBaseAdapter);
}
});
/**
* 搜索列表的点击事件
*/
listViewForScrollView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(SearchActivity.this,"点击"+searchList.get(position),Toast.LENGTH_SHORT).show();
}
});
SQLiteDatabase db_get_history=helper.getReadableDatabase();
if(db_get_history.isOpen()){
String sql_history_query="select * from historysearch;";
Cursor cursor = db_get_history.rawQuery(sql_history_query, null);
if(cursor.getCount()==0){
Toast.makeText(SearchActivity.this,"没有搜索历史",Toast.LENGTH_SHORT).show();
}else{
cursor.moveToFirst();
String history_name=cursor.getString(cursor.getColumnIndex("historyname"));
tv_historyText.append(history_name+" ");
}
while (cursor.moveToNext()){
String history_name=cursor.getString(cursor.getColumnIndex("historyname"));
tv_historyText.append(history_name+" ");
}
cursor.close();
}
db_get_history.close();
tv_clearHistory.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db_delete_history=helper.getWritableDatabase();
if(db_delete_history.isOpen()){
String sql_delete_history="delete from historysearch;";
db_delete_history.execSQL(sql_delete_history);
Toast.makeText(SearchActivity.this,"删除成功",Toast.LENGTH_SHORT).show();
}
tv_historyText.setText(" ");
}
});
}
/**
* 适配器获取数据库中搜索表所存放的内容
*/
class SearchBaseAdapter extends BaseAdapter{
@Override
public int getCount() {
return searchList.size();
}
@Override
public Object getItem(int position) {
return searchList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder=null;
if(convertView==null){
convertView=View.inflate(SearchActivity.this,R.layout.searchlist_item,null);
holder=new ViewHolder();
holder.tv_searchLisItem=convertView.findViewById(R.id.tv_searchlistitem);
convertView.setTag(holder);
}else{
holder=(ViewHolder)convertView.getTag();
}
holder.tv_searchLisItem.setText(searchList.get(position));
return convertView;
}
}
class ViewHolder{
TextView tv_searchLisItem;
}
}
下面这就是建库建表的了
public class SearchSQLiteOpenHelper extends SQLiteOpenHelper {
private static SQLiteOpenHelper mInstance=null;
public static synchronized SQLiteOpenHelper getmInstance(Context context){
if(mInstance==null){
mInstance=new SearchSQLiteOpenHelper(context,"searchitem.db",null,3);
}
return mInstance;
}
public SearchSQLiteOpenHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql="create table search(_id integer primary key autoincrement,searchname varchar(20));";
db.execSQL(sql);
String sql_history="create table historysearch(_id integer primary key autoincrement,historyname varchar(20));";
db.execSQL(sql_history);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
如果帮助到你,哈哈哈哈
文章浏览阅读1w次,点赞2次,收藏27次。来源:机器人小妹 很多时候企业拥有重复,乏味且困难的工作流程,这些流程往往会减慢生产速度并增加运营成本。为了降低生产成本,企业别无选择,只能自动化某些功能以降低生产成本。 通过数字化..._人工智能平台
文章浏览阅读2.2k次。热加载能够在每次保存修改的代码后自动刷新 electron 应用界面,而不必每次去手动操作重新运行,这极大的提升了开发效率。安装 electron 热加载插件热加载虽然很方便,但是不是每个 electron 项目必须的,所以想要舒服的开发 electron 就只能给 electron 项目单独的安装热加载插件[electron-reloader]:// 在项目的根目录下安装 electron-reloader,国内建议使用 cnpm 代替 npmnpm install electron-relo._electron-reloader
文章浏览阅读942次。在11.0 进行定制化开发,会根据需要去掉recovery模式的一些选项 就是在device.cpp去掉一些选项就可以了。_android recovery 删除 部分菜单
文章浏览阅读3.7k次。https://www.yuque.com/mnn/cn/cvrt_linux_mac基础依赖这些依赖是无关编译选项的基础编译依赖• cmake(3.10 以上)• protobuf (3.0 以上)• 指protobuf库以及protobuf编译器。版本号使用 protoc --version 打印出来。• 在某些Linux发行版上这两个包是分开发布的,需要手动安装• Ubuntu需要分别安装 libprotobuf-dev 以及 protobuf-compiler 两个包•..._mnn 编译linux
文章浏览阅读1.8k次。CSS3新增动画属性“@-webkit-keyframes”,从字面就可以看出其含义——关键帧,这与Flash中的含义一致。利用CSS3制作动画效果其原理与Flash一样,我们需要定义关键帧处的状态效果,由CSS3来驱动产生动画效果。下面讲解一下如何利用CSS3制作淡入淡出的动画效果。具体实例可参考刚进入本站时的淡入效果。1. 定义动画,名称为fadeIn@-webkit-keyf_css3入场效果淡入淡出
文章浏览阅读2.8k次。计算机系统应包括硬件和软件两个子系统,硬件和软件又必须依次分别包括中央处理器和系统软件。按人的要求接收和存储信息,自动进行数据处理和计算,并输出结果信息的机器系统。计算机是脑力的延伸和扩充,是近代科学的重大成就之一。计算机系统由硬件(子)系统和软件(子)系统组成。前者是借助电、磁、光、机械等原理构成的各种物理部件的有机组合,是系统赖以工作的实体。后者是各种程序和文件,用于指挥全系统按指定的要求进行..._计算机系统包括硬件系统和软件系统 软件又必须包括
文章浏览阅读7.9k次,点赞3次,收藏22次。一 定义这是最早出现的置换算法。该算法总是淘汰最先进入内存的页面,即选择在内存中驻留时间最久的页面予以淘汰。该算法实现简单,只需把一个进程已调入内存的页面,按先后次序链接成一个队列,并设置一个指针,称为替换指针,使它总是指向最老的页面。但该算法与进程实际运行的规律不相适应,因为在进程中,有些页面经常被访问,比如,含有全局变量、常用函数、例程等的页面,FIFO 算法并不能保证这些页面不被淘汰。这里,我_进程调度fifo算法代码
文章浏览阅读133次。rownum是oracle才有的写法,rownum在oracle中可以用于取第一条数据,或者批量写数据时限定批量写的数量等mysql取第一条数据写法SELECT * FROM t order by id LIMIT 1;oracle取第一条数据写法SELECT * FROM t where rownum =1 order by id;ok,上面是mysql和oracle取第一条数据的写法对比,不过..._mysql 替换@rownum的写法
文章浏览阅读790次,点赞3次,收藏4次。官网下载下载链接:http://www.eclipse.org/downloads/点击Download下载完成后双击运行我选择第2个,看自己需要(我选择企业级应用,如果只是单纯学习java选第一个就行)进入下一步后选择jre和安装路径修改jvm/jre的时候也可以选择本地的(点后面的文件夹进去),但是我们没有11版本的,所以还是用他的吧选择接受安装中安装过程中如果有其他界面弹出就点accept就行..._ecjelm
文章浏览阅读245次。原文链接:https://linux.cn/article-7801-1.htmlifconfigping <IP地址>:发送ICMP echo消息到某个主机traceroute <IP地址>:用于跟踪IP包的路由路由:netstat -r: 打印路由表route add :添加静态路由路径routed:控制动态路由的BSD守护程序。运行RIP路由协议gat..._ifconfig 删除vlan
文章浏览阅读224次。reduxredux里要求把数据都放在公共的存储区域叫store里面,组件中尽量少放数据,假如绿色的组件要给很多灰色的组件传值,绿色的组件只需要改变store里面对应的数据就行了,接着灰色的组件会自动感知到store里的数据发生了改变,store只要有变化,灰色的组件就会自动从store里重新取数据,这样绿色组件的数据就很方便的传到其它灰色组件里了。redux就是把公用的数据放在公共的区域去存..._redux redis
文章浏览阅读2.2k次,点赞3次,收藏6次。unzip版本不支持4G以上的压缩包所以要使用p7zip:Linux一个高压缩率软件wget http://sourceforge.net/projects/p7zip/files/p7zip/9.20.1/p7zip_9.20.1_src_all.tar.bz2tar jxvf p7zip_9.20.1_src_all.tar.bz2cd p7zip_9.20.1make && make install 如果安装失败,看一下报错是不是因为没有下载gcc 和 gcc ++(p7_linux 7za解压中文乱码