流式佈局+SQLite儲存,歷史記錄的儲存和刪除
阿新 • • 發佈:2018-12-08
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;
}
}