创建SQLite 数据库
public class DBHelper extends SQLiteOpenHelper {
public DBHelper( Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table why (id integer primary key autoincrement,data text)");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
封装 SQLite 增删改查方法,进行回调
public class DBUtil {
private SQLiteDatabase db;
public DBUtil(Context context) {
DBHelper dbHelper = new DBHelper(context, "why.db", null, 1);
db = dbHelper.getWritableDatabase();
}
/**
* 插入数据
*
* @param data
*/
public void insert(String data) {
ContentValues values = new ContentValues();
values.put("data", data);
db.insert("why", null, values);
}
/**
* 查询全部
* @return
*/
public List<String> queryAll(){
List<String> list = new ArrayList<>();
Cursor cursor = db.query("why", null, null, null, null, null, null);
while (cursor.moveToNext()) {
String data = cursor.getString(cursor.getColumnIndex("data"));
list.add(data);
}
return list;
}
/**
* 查询
*/
public String query() {
Cursor cu = db.rawQuery("select data from news", null);
String data = "";
while (cu.moveToNext()) {
data = cu.getString(cu.getColumnIndex("data"));
}
return data;
}
/**
* 全部删除
*/
public void delete() {
db.delete("why", null, null);
}
/**
* 根据ID删除
*
* @param id
*/
public void delete(int id) {
db.delete("why", null, null);
}
}
自定义 流式布局
public class CustomerView extends LinearLayout {
final int mMaxSize = 20;//设置输入长度
private List<String> mList;
Context mContext;
private DBUtil mDBUtil;
public CustomerView(Context context) {
this(context, null);
}
public CustomerView(Context context, AttributeSet attrs) {
this(context, attrs, -1);
}
public CustomerView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext = context;
//初始化
init();
//获取数据库对象
mDBUtil = new DBUtil(mContext);
}
//初始化
private void init() {
setOrientation(VERTICAL);
}
/**
* 把数据存储到 list集合中
*
* @param list
*/
public void setData(List<String> list) {
this.mList = list;
//初始化数据
showData();
}
//初始化数据
private void showData() {
removeAllViews();
LinearLayout linearLayout = (LinearLayout) View.inflate(mContext, R.layout.linearlayout_view, null);
addView(linearLayout);
int len = 0;
for (int i = 0; i < mList.size(); i++) {
final String str = mList.get(i);
len += str.length();
if (len == 0) {
Toast.makeText(mContext, "您输入的内容不能为空!!!", Toast.LENGTH_SHORT).show();
} else {
if (len > mMaxSize) {
linearLayout = (LinearLayout) View.inflate(mContext, R.layout.linearlayout_view, null);
addView(linearLayout);
len = str.length();
}
View view = View.inflate(mContext, R.layout.linear_layout_item_view, null);
TextView txtView = view.findViewById(R.id.txt_item_view);
txtView.setText(str);
linearLayout.addView(view);
//设置权重
LinearLayout.LayoutParams layoutParams = (LayoutParams) view.getLayoutParams();
layoutParams.weight = 1;
view.setLayoutParams(layoutParams);
final int index = i;
/**
* 点击事件
*/
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(mContext, str, Toast.LENGTH_SHORT).show();
}
});
/**
* 长按点击事件
*/
view.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
mList.remove(index);
showData();
return false;
}
});
}
}
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
/**
* 请输入内容
*/
private EditText mEditview;
private ImageView mSearch;
private ImageView mDelete;
private CustomerView mTvLossviewHot;
private CustomerView mTvLossviewHistory;
private DBUtil mDBUtil;
private List<String> mList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
initView();
mDBUtil = new DBUtil(this);
mSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String str = mEditview.getText().toString();
mList.add(str);
mDBUtil.insert(str);
mTvLossviewHistory.setData(mList);
//初始化数据
initData();
}
});
initData();
mDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mDBUtil.delete();
mTvLossviewHot.removeAllViews();
}
});
}
private void initData() {
List<String> queryAll = mDBUtil.queryAll();
mTvLossviewHot.setData(queryAll);
}
//初始化控件
private void initView() {
mEditview = (EditText) findViewById(R.id.editview);
mSearch = (ImageView) findViewById(R.id.search);
mDelete = (ImageView) findViewById(R.id.delete);
mTvLossviewHot = (CustomerView) findViewById(R.id.tv_lossview_hot);
mTvLossviewHistory = (CustomerView) findViewById(R.id.tv_lossview_history);
}
}
xml布局:(布局中图片自己找)
activity_main
<?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="0dp"
android:layout_weight="1">
<EditText
android:id="@+id/editview"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="9"
android:hint="请输入内容"
android:textSize="18sp" />
<ImageView
android:id="@+id/search"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@drawable/sousuo" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="热门搜索"
android:textColor="#000"
android:textSize="20sp" />
<ImageView
android:id="@+id/delete"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:layout_marginRight="10dp"
android:src="@drawable/delete" />
</RelativeLayout>
<com.baway.day_week3.customer.CustomerView
android:id="@+id/tv_lossview_hot"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="搜索历史"
android:textColor="#000"
android:textSize="20sp" />
</RelativeLayout>
<com.baway.day_week3.customer.CustomerView
android:id="@+id/tv_lossview_history"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5" />
</LinearLayout>
linearlayout_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
linear_layout_item_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/txt_item_view"
android:background="@drawable/edit_bg"
android:layout_weight="1"
android:textSize="24sp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:gravity="center"/>
</LinearLayout>