1. 程式人生 > >流式佈局+搜尋框(歷史記錄+資料庫)

流式佈局+搜尋框(歷史記錄+資料庫)

//FrameLayout 裡面的程式碼

public class LiuLayout extends FrameLayout {

private final int V_With=20;

public LiuLayout(Context context) {
    super(context);
}

public LiuLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public LiuLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

public void addTextView(String name) {
    TextView textView = (TextView) View.inflate(getContext(), R.layout.liu_item, null);
    textView.setText(name);
    LayoutParams layoutParams = new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    textView.setLayoutParams(layoutParams);
    addView(textView);
    //Log.e("zmz", "新增成功!");
}

@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 disWith = V_With;

    for (int i = 0; i < getChildCount(); i++) {
        //尋找子控制元件
        View child = getChildAt(i);

        //子控制元件的寬高
        int childWidth = child.getWidth();
        int childHeight = child.getHeight();

        //判斷是否滿行,
        if (disWith + childWidth > width) {
            //如果滿行,則換行
            row++;

            //子控制元件開始座標還原
            disWith = V_With;
        }

        child.layout(disWith, row * childHeight, disWith + childWidth, childHeight * (row + 1));
        disWith += childWidth;

    }
}
}

//Activity裡面的程式碼

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
LiuLayout liuLayout;
EditText editText;
Button button;
Dao dao;
ArrayList<String>list;
TextView del_textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //尋找控制元件
    editText = findViewById(R.id.edi_name);
    button = findViewById(R.id.button_ss);
    liuLayout = findViewById(R.id.liuLayout);
    del_textView = findViewById(R.id.del_text);

    //點選事件
    button.setOnClickListener(this);
    del_textView.setOnClickListener(this);
    //建立資料庫
    dao = new Dao(this);

    //查詢資料庫資料新增到流式佈局
    initData();
}

private void initData() {

    Cursor cursor = dao.sel("person",null,"",new String[]{},null,null,null);

    while (cursor.moveToNext()){

        String name = cursor.getString(cursor.getColumnIndex("name"));

        liuLayout.addTextView(name);
    }
}


@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.button_ss:

            //獲取資料新增到流式佈局和資料庫
            addData();

            break;
            case R.id.del_text:
                //刪除,需要把資料庫裡的資料也要刪除
                delData();

                break;
    }
    //清除輸入框中的值
    editText.getText().clear();
}

private void delData() {
    //提示框
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("提示");
    builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            list = new ArrayList<String>();
            //先查出有什麼資料
            Cursor cursor = dao.sel("person",null,"",new String[]{},null,null,null);

            while (cursor.moveToNext()){

                String names = cursor.getString(cursor.getColumnIndex("name"));
                //存入list集合作為刪除的欄位
                list.add(names);
            }

            //迴圈刪除資料庫的資料
            for (int i = 0; i <list.size() ; i++) {
                long delete = dao.del("person","name=?",new String[]{list.get(i)});
            }
            //刪除流式佈局的內容
            liuLayout.removeAllViews();

        }
    });
    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(MainActivity.this, "您點選了取消!", Toast.LENGTH_SHORT).show();
        }
    });
    builder.show();


}

private void addData() {
    //獲取輸入框中的值
    String name = editText.getText().toString().trim();

    //非空判斷
    if (name.isEmpty()) {
        Toast.makeText(this, "請輸入搜尋內容", Toast.LENGTH_SHORT).show();
        return;
    }

    //新增到流式佈局
    liuLayout.addTextView(name);

    //新增資料庫
    ContentValues values = new ContentValues();
    values.put("name",name);
    long insert = dao.add("person",null,values);
}

}

//dao層

public class Dao {

private SQLiteDatabase db;
public Dao(Context context) {
    MHolder mHolder = new MHolder(context);

    db = mHolder.getWritableDatabase();
}

public long add(String table, String nullColumnHack, ContentValues values) {

    return db.insert(table,nullColumnHack,values);
}


public Cursor sel(String table, String[] columns, String selection,
                  String[] selectionArgs, String groupBy, String having,
                  String orderBy){

    return db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
}

public long del(String table, String whereClause, String[] whereArgs) {
    return db.delete(table,whereClause,whereArgs);
}
}

//佈局

<?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:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:orientation="horizontal">
    <EditText
        android:id="@+id/edi_name"
        android:layout_width="300dp"
        android:background="@drawable/sty_color"
        android:hint="請輸入......."
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/button_ss"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="2dp"
        android:text="搜尋"
        android:background="@drawable/btn_color"/>

</LinearLayout>

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:orientation="horizontal">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:textSize="15sp"
    android:text="搜尋歷史:"/>

    <TextView
        android:id="@+id/del_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:padding="10dp"
        android:textSize="10sp"
        android:text="清除歷史記錄"/>

</RelativeLayout>

<zhao.com.hy.LiuLayout
   android:id="@+id/liuLayout"
   android:layout_width="match_parent"
   android:layout_height="match_parent">
 </zhao.com.hy.LiuLayout>
</LinearLayout>

//效果圖
在這裡插入圖片描述

在這裡插入圖片描述