1. 程式人生 > >流式佈局+資料庫+刪除

流式佈局+資料庫+刪除

1.佈局

<?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">

    <EditText
       android:id="@+id/ettext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="搜尋"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:textFontWeight="500dp"
        android:layout_marginLeft="20dp"
        android:text="搜尋歷史"/>

    <com.bw.ymy.demo09.CurstomLayout
        android:id="@+id/fall"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>


    <Button
        android:id="@+id/dell"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="清空"
        android:layout_gravity="center"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:layout_marginLeft="30dp"
        android:text="熱門"/>
    <com.bw.ymy.demo09.CurstomLayout
        android:id="@+id/hotview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>






</LinearLayout>

2.熱門展示資料佈局

<?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="match_parent"
    android:orientation="horizontal">
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:gravity="center"
        android:layout_weight="1"/>

</LinearLayout>

3.主頁面

package com.bw.ymy.demo09;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    //向裡面新增熱門內容
    private  String[] desc=new String[]{"陳獨秀","李雲龍","和尚","秀芹","秀兒","是","陳獨秀","陳獨秀","陳獨秀","陳獨秀",};
     CurstomLayout fall;
      CurstomLayout hotview;
     EditText et;
     Dao dao;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //dao層
        dao= new Dao(MainActivity.this);
            //獲取資源id
             fall=findViewById(R.id.fall);
             et=findViewById(R.id.ettext);
              hotview=findViewById(R.id.hotview);
         //迴圈新增熱門內容
         for(String str:desc){
            TextView textView=new TextView(MainActivity.this);
            textView.setText(str);
            textView.setTextSize(24);
            hotview.addView(textView);
        }

        //新增到資料庫
        List<String> list=dao.selall();
        if(list.size()!=0)
        {
           for (String string:list)
           {
               TextView tv=new TextView(MainActivity.this);
               tv.setText(et.getText());
               tv.setTextColor(Color.GREEN);
               fall.addView(tv);
           }

        }
            //點選新增按鈕
            findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView tv=new TextView(MainActivity.this);
                tv.setText(et.getText());
                tv.setTextColor(Color.GREEN);
                //判斷資料庫是否有重複的 重複的加不進去
               boolean b=dao.sel(et.getText().toString());
               if(!et.getText().toString().equals("")&&b)
               {
                dao.add(et.getText().toString());
                //沒有重複的新增
                fall.addView(tv);
               }
            }
        });



        ///清空
        findViewById(R.id.dell).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //刪除資料庫
                dao.delall();
                //刪除列表
                fall.removeAllViews();
            }
        });
    }
}

4.View

package com.bw.ymy.demo09;

import android.content.Context;
import android.graphics.Canvas;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

public class CurstomLayout extends LinearLayout {

    //孩子最高的一個
    private  int mChildMaxHeight;

    //每一個左邊距20 px
    private  int mHSpace=20;

    //上下間距 20 px
    private  int mVSpace=20;

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

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

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //拿到父容器的寬高和計算模式
        int sizeWidth=MeasureSpec.getSize(widthMeasureSpec);
        int sizeHeight=MeasureSpec.getSize(heightMeasureSpec);

        //測量孩子大小,必須寫
        measureChildren(widthMeasureSpec,heightMeasureSpec);

        //尋找孩子最高的一個
        //在下方
        findMaxChildMaxHeight();

        //初始化值
        int left=0,top=0;

        //開始迴圈
        int childCount=getChildCount();
        for (int i=0;i<childCount;i++)
        {
            View view=getChildAt(i);
            //是否是一行的第一個
            if(left!=0)
            {
                //換行,放不下
                if((left +view.getMeasuredWidth())>sizeWidth)
                {
                    top+=mChildMaxHeight+mVSpace;
                    left=0;
                }
            }
            left+=view.getMeasuredWidth()+mHSpace;
        }
        setMeasuredDimension(sizeWidth,(top+mChildMaxHeight)>sizeHeight?sizeHeight:top+mChildMaxHeight);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        findMaxChildMaxHeight();
        //
        //開始
        //初始化
        int left=0,top=0;
        //迴圈
        int childCount=getChildCount();
        for (int i=0;i<childCount;i++)
        {
            View view=getChildAt(i);
            //是否第一個
            if(left !=0)
            {
                if((left+ view.getMeasuredWidth())>getWidth())
                {
                    //計算下一行top
                    top+=mChildMaxHeight+mVSpace;
                    //換行
                    left=0;
                }
            }
            //安排位置
            view.layout(left,top,left+view.getMeasuredWidth(),top+getMeasuredHeight());
            left+=view.getMeasuredWidth()+mHSpace;
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

    }

    //z最高的孩子
    private void findMaxChildMaxHeight() {
        mChildMaxHeight=0;
        int childCount =getChildCount();
        for (int i=0;i<childCount;i++)
        {
            View view=getChildAt(i);
            if(view.getMeasuredHeight()>mChildMaxHeight)
            {
                mChildMaxHeight=view.getMeasuredHeight();
            }
        }
    }
}

5.Dao層

package com.bw.ymy.demo09;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

public class Dao {
    private SQLiteDatabase database;


    public Dao(Context context) {
     database=new Sqlitehelp(context).getWritableDatabase();
    }
    //新增到資料庫
    public  void add(String string)
    {
        ContentValues values=new ContentValues();
        values.put("context",string);
        database.insert("history",null,values);
    }
    //刪除
    public void  delall()
    {
        database.delete("history",null,null);
    }

    public  boolean sel(String string)
    {
        Cursor query = database.query("history", null, "context=?", new String[]{string}, null, null, null);
        return  query.getCount()==0?true:false;
    }
    //查詢
    public List<String> selall()
    {
        List<String> list=new ArrayList<>();
        Cursor query = database.query("history", null, null, null, null, null, null);
        while (query.moveToNext())
        {
            list.add(query.getString(query.getColumnIndex("context")));

        }
        return list;
    }
}

6.sqlite建立資料庫

package com.bw.ymy.demo09;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class Sqlitehelp extends SQLiteOpenHelper {
    public Sqlitehelp(Context context) {
        super(context, "User.db", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table history(id integer primary key autoincrement,"+"context text)");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

展示效果圖

#熱門的文字是死資料