MainActivity.java
public class MainActivity extends AppCompatActivity {
List ff=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
final EditText editText =findViewById(R.id.edit);
final CustomWaterFallViewGroup customWaterFallViewGroup = findViewById(R.id.water_fall);
Button button = findViewById(R.id.btn); //搜索
Button button1 = findViewById(R.id.btn_clear); //清除
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String s = editText.getText().toString();
WebDao.getInstance(MainActivity.this).add(s); //点击按钮添加到数据库
String query = WebDao.getInstance(MainActivity.this).query(); //通过全查方法吐司显示数据库全部内容
Toast.makeText(MainActivity.this,query,Toast.LENGTH_SHORT).show();
ff.add(s);
customWaterFallViewGroup.setData(ff);
}
});
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
customWaterFallViewGroup.clearAll();
}
});
}
}
activity_main.xml
CustomWaterFallViewGroup.java
public class CustomWaterFallViewGroup extends LinearLayout{
List StringList=new ArrayList<>();
int MAX=20;
Context mContext;
WebDao webDao;
public CustomWaterFallViewGroup(Context context) {
super(context);
mContext=context;
init();
}
public CustomWaterFallViewGroup(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
mContext=context;
init();
}
private void init() {
setOrientation(VERTICAL);
webDao = new WebDao(mContext);
}
public void setData(List data) {
this.StringList=data;
shouData();
}
private void shouData() {
removeAllViews();
LinearLayout linearLayout = (LinearLayout) View.inflate(mContext, R.layout.item_water_fall_h, null);
addView(linearLayout);//首先先存放一个布局
int len=0;
for (int i=0;iMAX){ //如果第一条存放已满,接着添加往下添加布局
linearLayout = (LinearLayout) View.inflate(mContext,R.layout.item_water_fall_h,null);
addView(linearLayout);
len=StringList.size();
}
View view = View.inflate(mContext, R.layout.item_water_fall, null);
TextView textView = view.findViewById(R.id.tv_item_water_fall);
textView.setText(StringList.get(i));
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,StringList.get(index),Toast.LENGTH_SHORT).show();
}
});
view.setOnLongClickListener(new OnLongClickListener() { //长按历史记录删除
@Override
public boolean onLongClick(View view) {
String sss=StringList.get(index); //删除长按数据库内容
WebDao.getInstance(mContext).delete(sss);
String query = WebDao.getInstance(mContext).query();
Toast.makeText(mContext,query,Toast.LENGTH_SHORT).show();
StringList.remove(index); //长按删除单个记录
shouData();
return true;
}
});
}
}
public void clearAll() { //点击按钮清除全部历史记录
StringList.clear();
shouData();
WebDao.getInstance(mContext).delAll(); //清除数据库内容
String query = WebDao.getInstance(mContext).query();
Toast.makeText(mContext,query,Toast.LENGTH_SHORT).show();
}
}
item_water_fall_h.xml
item_water_fall.xml
WebView.java
public class WebDao {
MHelper mHelper;
SQLiteDatabase database;
ArrayList list;
public WebDao(Context context) {
mHelper = new MHelper(context);
database = mHelper.getReadableDatabase();
}
private static WebDao instance;
public static WebDao getInstance(Context context){
if (instance==null){
instance =new WebDao(context);
}
return instance;
}
public void add(String data){
//database.execSQL("insert into num(data) values(?)",new Object[]{data});
ContentValues values=new ContentValues();
values.put("data",data);
database.insert("num",null,values);
}
public String query(){
Cursor cursor = database.rawQuery("select * from num", null);
//list = new ArrayList<>();
StringBuilder builder = new StringBuilder();
while (cursor.moveToNext()){
String data = cursor.getString(cursor.getColumnIndex("data"));
builder.append(data);
}
return builder.toString();
}
public void delete(String data){
database.execSQL("delete from num where data=?",new Object[]{data});
//database.delete("num","data=?",new String[]{data});
}
public void delAll(){
database.delete("num",null,null);
}
}
MHelper.java
public class MHelper extends SQLiteOpenHelper{
public MHelper(Context context) {
super(context, "bw.db",null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table num (id integer primary key autoincrement,data text)");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}