Android 搜索内容保存历史记录
Posted ~~~周
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 搜索内容保存历史记录相关的知识,希望对你有一定的参考价值。
android 搜索内容保存历史记录
一、界面布局
主界面布局 activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_marginTop="10dp">
<EditText
android:id="@+id/e1"
android:layout_weight="5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@android:color/transparent"
android:maxLength="20"
android:textSize="13sp"
android:textColor="#000"
android:hint="输入关键词进行搜索"/>
<Button
android:id="@+id/b1"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="确定"
android:background="@null"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="5dp"
android:background="#74D5D5D5"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="历史搜索"
android:textColor="#000"
android:padding="20dp"
android:textSize="16sp"/>
<TextView
android:id="@+id/t1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:layout_marginRight="20dp"
android:text="清除搜索记录"/>
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/r1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"/>
</LinearLayout>
次界面布局 activity_main2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:background="#FF6200EE">
<ImageView
android:id="@+id/fh"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:src="@mipmap/photos3"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="20dp"
android:gravity="center"
android:textSize="20sp"
android:textColor="#fff"
android:text="商品列表"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<LinearLayout
android:layout_width="350dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:background="#2CCACACA">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@mipmap/photos4"/>
<TextView
android:id="@+id/e1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@android:color/transparent"
android:maxLength="20"
android:textSize="13sp"/>
</LinearLayout>
<ImageView
android:id="@+id/i3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/photos11"/>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="50.0dip"
android:background="#FFFFFF">
<!--layout_weight这个属性为权重,让两个textview平分这个linearLayout-->
<TextView
android:id="@+id/t1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:gravity="center"
android:text="综合"
android:textSize="15dp"
android:textColor="#F36778D5" />
<TextView
android:id="@+id/t2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:gravity="center"
android:text="销量"
android:textSize="15dp"
android:textColor="#000000" />
<LinearLayout
android:layout_weight="1.0"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/t3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="价格"
android:textSize="15dp"
android:textColor="#000000" />
<ImageView
android:id="@+id/i1"
android:layout_width="20dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@mipmap/photos8"/>
</LinearLayout>
<LinearLayout
android:layout_weight="1.0"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/t4"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="筛选"
android:textSize="15dp"
android:textColor="#000000" />
<ImageView
android:id="@+id/i2"
android:layout_width="20dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@mipmap/photos9"/>
</LinearLayout>
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/r1"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
列表界面布局 list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<TextView
android:id="@+id/t1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:singleLine="true"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:text="椅子"
android:textColor="#000"/>
</LinearLayout>
界面展示
二、功能实现
新建一个Adapter.class
package com.likeit.note7;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder>
private String[]a;
private int length;
private String result;
Adapter(String[] a, String result)
this.a=a;
this.result=result;
@NonNull
@Override
public Adapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
@Override
public void onBindViewHolder(Adapter.ViewHolder holder, int position)
if(result.equals("1"))
holder.t1.setText("");
else
holder.t1.setText(a[position]);
@Override
public int getItemCount()
try
length=a.length;
catch (Exception e)
e.printStackTrace();
length=0;
return length;
public class ViewHolder extends RecyclerView.ViewHolder
private TextView t1;
public ViewHolder(@NonNull View itemView)
super(itemView);
t1 = itemView.findViewById(R.id.t1);//绑定id
MainActivity.class
package com.likeit.note7;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity
private EditText e1;
private Button b1;
private TextView t1;
private RecyclerView r1;
private String result;
private String[] str1;
private GridLayoutManager gridLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1=(EditText)findViewById(R.id.e1);
b1=(Button)findViewById(R.id.b1);
t1=(TextView)findViewById(R.id.t1);
r1=(RecyclerView)findViewById(R.id.r1);
gridLayoutManager=new GridLayoutManager(MainActivity.this,5);
r1.setLayoutManager(gridLayoutManager);
//读取文件
StringBuffer stringBuffer=new StringBuffer();
try
InputStream inputStream=openFileInput("myfile");
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
String cout=null;
while((cout=bufferedReader.readLine())!=null)
stringBuffer.append(cout);
//将获取到的文件内容写入数组
str1=stringBuffer.toString().substring(0,stringBuffer.length()-1).split(",");
Adapter adapter=new Adapter(str1,"0");
r1.setAdapter(adapter);
bufferedReader.close();
catch (Exception e)
e.printStackTrace();
b1.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
result = e1.getText().toString().trim();
//判断搜索内容是否重复,重复则不写入
try
for(int i=0;i<=str1.length;i++)
if(result.equals(str1[i]))
Intent intent=new Intent(MainActivity.this,MainActivity2.class);
intent.putExtra("ss",result);
startActivity(intent);
return;
catch (Exception e)
e.printStackTrace();
//将内容写入文件
try
OutputStream outputStream = openFileOutput("myfile", MODE_APPEND);
//判断搜索内容是否为空,为空则不写入
if(result.equals(""))
Intent intent=new Intent(MainActivity.this,MainActivity2.class);
intent.putExtra("ss",result);
startActivity(intent);
return;
String str = result + ",";//用句号分隔开
outputStream.write(str.getBytes());
outputStream.flush();
outputStream.close();
Intent intent=new Intent(MainActivity.this,MainActivity2.class);
intent.putExtra("ss",result);
startActivity(intent);
catch (Exception e)
e.printStackTraceAndroid搜索功能的案例,本地保存搜索历史记录......