流式布局+SQLite存储,历史记录的存储和删除

1.先解决流式布局的FrameLayout,以方便拉入布局界面

public class FlowLayout extends FrameLayout {
private final static int H_DISTANCE=20;//水平间距20
private final static int V_DISTANCE=20;//竖直间距20
public FlowLayout(@NonNull Context context) {
  	  super(context);
}

public FlowLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
}

public FlowLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}
public void addTextView(String keys){
    //加载字体布局
    TextView textView = (TextView) View.inflate(getContext(), R.layout.flow_item, null);
      textView.setText(keys);
    //布局宽高自适应
    LayoutParams params = new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    textView.setLayoutParams(params);//控件设置上布局出参数
    addView(textView);
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);
    int width=getWidth();//获取本空间宽度,用于计算行数
    int row=0;//行数
    int disWidth=H_DISTANCE;//子控件左边的左表
    for (int i = 0; i < getChildCount(); i++) {
        View view=getChildAt(i);
        int viewWidth = view.getWidth();
        int viewHeight = view.getHeight();
        Log.i("dt", "textHeight "+viewHeight);
        if (disWidth+viewWidth>width){
            row++;
            disWidth=H_DISTANCE;//还原左边边距
        }
        int ViewTop =row*viewHeight+row*V_DISTANCE;
        view.layout(disWidth,ViewTop,disWidth+viewWidth,ViewTop+viewHeight);//子控件布局
        disWidth+=(viewWidth+H_DISTANCE);//记录下一个子控件的左边坐标

    }

}
}

加载的字体布局flow_item放layout文件夹下

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:textSize="20sp"
android:textColor="@color/colorPrimaryDark"
android:background="@drawable/car_btn_bg"/>

自定义的car_btn_bg写drawable文件夹下

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
    android:color="@android:color/holo_red_dark"
    android:width="2dp">

</stroke>
![在这里插入图片描述](https://img-blog.csdnimg.cn/201812011046459.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzcxNzQ0Nw==,size_16,color_FFFFFF,t_70)</shape>

2.解决了流式布局,然后添加到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:orientation="horizontal">
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/edit_keys"
        android:layout_weight="1"/>
    <Button
        android:layout_width="80dp"
        android:layout_height="50dp"
        android:id="@+id/btn_search"
        android:text="搜索"/>
</LinearLayout>

<com.baidu.week1tast1.FlowLayout
    android:id="@+id/flow_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1">

</com.baidu.week1tast1.FlowLayout>
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="onClick"
    android:text="清除历史记录"/>
</LinearLayout>

3.搞定数据库,简单写写就好,该重写的重写

public class RecordSQLiteOpenHelper extends SQLiteOpenHelper {

public RecordSQLiteOpenHelper(@Nullable Context context) {
    super(context, "temp.db", null, 1);

}
@Override
public void onCreate(SQLiteDatabase db) {
    //打开数据库,建立了一个叫records的表,里面只有一列name来存储历史记录:
    db.execSQL("create table records(id integer primary key autoincrement,name varchar(200))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}
}

4最后把Activity补全,数据库的方法写完

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private EditText mEdit;
private Button msearch;
private FlowLayout mFlowLayout;
private RecordSQLiteOpenHelper helper;
private SQLiteDatabase db;
private List<String> data;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mEdit = findViewById(R.id.edit_keys);
    msearch = findViewById(R.id.btn_search);
    mFlowLayout = findViewById(R.id.flow_layout);
    helper=new RecordSQLiteOpenHelper(this);
    msearch.setOnClickListener(this);
    data = getRecordsList();
    for (int i = 0; i < data.size(); i++) {
        mFlowLayout.addTextView(data.get(i));

    }

}

//点击事件
public void onClick(View v){
    if (v.getId()==R.id.btn_search){//搜索点击
        String keys=mEdit.getText().toString();

       initData(keys);


        mFlowLayout.addTextView(keys);
    }else {//删除点击
        mFlowLayout.removeAllViews();
        deleteData();
    }
}
//数据库添加
private void initData(String tempName){
    db = helper.getWritableDatabase();
    db.execSQL("insert into records(name) values('" + tempName + "')");
    db.close();
}
//数据库清空
private void deleteData(){
   db = helper.getWritableDatabase();
   db.execSQL("delete from records");
   db.close();
}
public List<String> getRecordsList() {
    List<String> recordsList = new ArrayList<>();
    db = helper.getReadableDatabase();
    Cursor cursor = db.query("records",null,null,null,null,null,null);
    while (cursor.moveToNext()) {
        String name = cursor.getString(cursor.getColumnIndexOrThrow("name"));
        recordsList.add(name);
    } //关闭数据库
    db.close();
    return recordsList;
}
}

在这里插入图片描述