1. 程式人生 > >模擬瀑布流+SQLite資料庫,長按刪除,清空。

模擬瀑布流+SQLite資料庫,長按刪除,清空。

MainActivity

package com.example.fallsview_listview;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

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

public class MainActivity extends AppCompatActivity {

    List<String> strList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         initView();
    }

    private void initView() {
        final TextView textView = findViewById(R.id.textView1);
        final EditText editText = findViewById(R.id.editText);
        final CustomFallsView customFallsView = findViewById(R.id.custom_falls);
        findViewById(R.id.add).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //獲取輸入框內容
                String str = editText.getText().toString();
                Dao.getInstance(MainActivity.this).add(str);
                //Toast.makeText(MainActivity.this, "您添加了:"+str,Toast.LENGTH_SHORT).show();
                List<String> strList = Dao.getInstance(MainActivity.this).query();
                customFallsView.setDatas(strList);

            }
        });



       //點選檢視資料
        findViewById(R.id.look).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<String> strList = Dao.getInstance(MainActivity.this).query();
                customFallsView.setDatas(strList);
            }
        });
        //清空歷史記錄
        findViewById(R.id.delAll).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    customFallsView.setclear();
            }
        });

    }
}

CustomFallsView自定類

package com.example.fallsview_listview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

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

public class CustomFallsView extends LinearLayout {
    final int mMaxSize = 20;

    List<String> strList = new ArrayList<>();
    Context mContext;

    public CustomFallsView(Context context) {
        super(context);
        this.mContext = context;
        init();
    }

    public CustomFallsView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        init();
    }

    private void init() {
        //最外層垂直
        setOrientation(VERTICAL);
    }
    //傳入輸入框裡的內容
    public void setDatas(List<String> strList) {
         this.strList = strList;
         showData();
    }

    //展示內容
    private void showData() {
        //移除以前所有View
        removeAllViews();
        //先新增橫向LinearLayout到根佈局
        LinearLayout linearLayout_h = (LinearLayout) View.inflate(mContext, R.layout.linearlayout_h, null);
        addView(linearLayout_h);

        int len = 0;
        for (int i = 0; i < strList.size(); i++) {
            final String str = strList.get(i);
            len += str.length();

            final int index = i;
            if (len > mMaxSize){
               // 新增新的橫向佈局
                linearLayout_h = (LinearLayout) View.inflate(mContext, R.layout.linearlayout_h, null);
                addView(linearLayout_h);
                len = str.length();
            }
            //新增textview賦值
            View view = View.inflate(mContext, R.layout.falls_text, null);
            TextView textView = view.findViewById(R.id.text_fall);
            textView.setText(strList.get(i));
            linearLayout_h.addView(view);

            // 設定權重,讓每一行內所有控制元件相加充滿整行,併合理分配
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
            layoutParams.weight = 1;
            view.setLayoutParams(layoutParams);

            //點選吐司
            view.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(mContext,strList.get(index),Toast.LENGTH_SHORT).show();
                }
            });
            //長按刪除
            view.setOnLongClickListener(new OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    Dao.getInstance(mContext).del(strList.get(index));
                    List<String> query = Dao.getInstance(mContext).query();
                    strList.remove(index);
                    showData();
                    return true;
                }
            });
        }
    }
     //清空資料方法
    public void setclear() {
        strList.clear();
        showData();
        Dao.getInstance(mContext).delAll();
        List<String> strList = Dao.getInstance(mContext).query();
        showData();
    }
}

Dao層

package com.example.fallsview_listview;

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 static Dao instance;
    private SQLiteDatabase database;
    private Dao(Context context){
        database = new MyHelper(context).getWritableDatabase();
    }
    public static Dao getInstance(Context context){
        if (instance == null){
            instance = new Dao(context);
        }
        return instance;
    }

    //新增
    public void add(String name){
        ContentValues values = new ContentValues();
        values.put("name", name);
        database.insert("user", null, values);
    }

    //單刪
    public void del(String name){
        database.delete("user", "name = ?", new String[]{name});
    }
    //全刪
    public void delAll(){
        database.delete("user", null, null);
    }

    //查詢
    public List<String> query(){
        List<String> stringBuilder = new ArrayList<>();
        Cursor cursor = database.query("user", null, null, null, null, null, null, null);
        while (cursor.moveToNext()){
            String name = cursor.getString(cursor.getColumnIndex("name"));
            stringBuilder.add(name);
        }
        return stringBuilder;
    }


}

activity.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">

    <android.support.constraint.ConstraintLayout
        android:id="@+id/con"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <EditText
            android:id="@+id/editText"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:hint="輸入一些內容"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintRight_toLeftOf="@+id/add"
            />
        <Button
            android:id="@+id/add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="錄入"
            android:textSize="15sp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintRight_toLeftOf="@+id/look"
            app:layout_constraintBottom_toBottomOf="@+id/editText"
            app:layout_constraintLeft_toRightOf="@+id/editText"
            />
        <Button
            android:id="@+id/look"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="檢視"
            android:textSize="15sp"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toRightOf="@+id/add"
            app:layout_constraintBottom_toBottomOf="@+id/add"
            app:layout_constraintRight_toLeftOf="@+id/delAll"
            android:layout_marginLeft="5dp"
            />
        <Button
            android:id="@+id/delAll"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="清空"
            android:textSize="15sp"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="@+id/look"
            app:layout_constraintLeft_toRightOf="@+id/look"
            />
    </android.support.constraint.ConstraintLayout>


        <com.example.fallsview_listview.CustomFallsView
            android:id="@+id/custom_falls"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        </com.example.fallsview_listview.CustomFallsView>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="250dp"
        />


</LinearLayout>

模擬瀑布流