流式布局+搜索框+数据库

implementation 'com.hjm:BottomTabBar:1.1.1'
    //okhttp
    implementation 'com.squareup.okhttp3:okhttp:3.5.0'
    //gson
    implementation 'com.google.code.gson:gson:2.6.2'
    //流式布局
    implementation 'com.fynn.fluidlayout:fluidlayout:1.0'
    implementation 'com.jcodecraeer:xrecyclerview:1.3.2'
    implementation 'io.reactivex.rxjava2:rxjava:2.0.7'
    implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
    implementation 'com.squareup.retrofit2:retrofit:2.1.0'
    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.2.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.1.0'
    implementation 'com.squareup.retrofit2:converter-scalars:2.1.0'
    implementation 'com.xhb:xbanner:1.2.2'
    implementation 'com.github.bumptech.glide:glide:3.7.0'
// 一般依赖:
    implementation 'com.facebook.fresco:fresco:0.14.1'
    // 如果需要支持gif,再添加:
//依赖
implementation 'com.hjm:BottomTabBar:1.1.1'
    //okhttp
    implementation 'com.squareup.okhttp3:okhttp:3.5.0'
    //gson
    implementation 'com.google.code.gson:gson:2.6.2'
    //流式布局
    implementation 'com.fynn.fluidlayout:fluidlayout:1.0'
    implementation 'com.jcodecraeer:xrecyclerview:1.3.2'
    implementation 'io.reactivex.rxjava2:rxjava:2.0.7'
    implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
    implementation 'com.squareup.retrofit2:retrofit:2.1.0'
    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.2.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.1.0'
    implementation 'com.squareup.retrofit2:converter-scalars:2.1.0'
    implementation 'com.xhb:xbanner:1.2.2'
    implementation 'com.github.bumptech.glide:glide:3.7.0'
// 一般依赖:
    implementation 'com.facebook.fresco:fresco:0.14.1'
    // 如果需要支持gif,再添加:
    implementation 'com.facebook.fresco:animated-gif:0.12.0'
    // 在 API < 14 上的机器支持 WebP 时,需要添加
    implementation 'com.facebook.fresco:animated-base-support:0.12.0'
    // 支持 WebP (静态图+动图),需要添加
    implementation 'com.facebook.fresco:animated-webp:0.12.0'
    implementation 'com.facebook.fresco:webpsupport:0.12.0'
    // 仅支持 WebP 静态图,需要添加
    implementation 'com.facebook.fresco:webpsupport:0.12.0'

    implementation 'com.squareup.okhttp3:logging-interceptor:3.4.2'
    implementation 'com.youth.banner:banner:1.4.9'
    implementation 'org.greenrobot:eventbus:3.0.0'
    implementation files('libs/open_sdk_r5990_lite.jar')
    implementation 'com.fynn.fluidlayout:fluidlayout:1.0'
    compile 'com.github.bumptech.glide:glide:3.7.0'
//activity_main.xml


    

        

            
            
        

        

            

            
            
        

        

            
            
        
    

//MainActivity
package bwei.com.uuuuu0531;

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

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private TextView sousuo;
    private LinearLayout ssk;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

    }

    private void initView() {
        sousuo = (TextView) findViewById(R.id.sousuo);
        ssk = (LinearLayout) findViewById(R.id.ssk);
        sousuo.setOnClickListener(this);
        ssk.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.ssk:

                break;
            case R.id.sousuo:
                Intent intent = new Intent(MainActivity.this,Main2Activity.class);
                startActivity(intent);
                break;
        }
    }

}
//Sql
//MySql
package bwei.com.uuuuu0531.SQl;

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

/**
 * Created by mac on 2018/4/17.
 */

public class MySql extends SQLiteOpenHelper {

    public MySql(Context context){
        super(context, "search.db", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {

        String sql = "create table search(id integer primary key autoincrement,name varchar(20))";
        sqLiteDatabase.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
}
//MySqlDao
package bwei.com.uuuuu0531.SQl;

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;

/**
 * Created by mac on 2018/4/17.
 */

public class MySqlDao {
    private MySql mySQlite;
    private SQLiteDatabase db;

    public MySqlDao(Context context){
        mySQlite = new MySql(context);
        db = mySQlite.getReadableDatabase();
    }

    public void add(String name){
        ContentValues values = new ContentValues();
        values.put("name",name);
        db.insert("search",null,values);
    }

    public void delete(String name){
        db.delete("search","name=?",new String[]{name});
    }

    public void deleteAll(){
        db.execSQL("DELETE FROM search");
    }

    public List select(){
        Cursor cursor = db.rawQuery("select * from search",null);
        List list = new ArrayList<>();

        while (cursor.moveToNext()){
            String name = cursor.getString(cursor.getColumnIndex("name"));
            list.add(name);
        }

        return list;
    }

}
//activity_main2.xml



    

        

        

            

            
        

        
    

    

    

    
    

    

    

//Main2Activity
package bwei.com.uuuuu0531;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.fynn.fluidlayout.FluidLayout;

import java.util.List;

import bwei.com.uuuuu0531.SQl.MySqlDao;

public class Main2Activity extends AppCompatActivity {

    MySqlDao mySQliteDao;
    EditText et;
    FluidLayout fluidLayout;
    Button bt_quxiao, bt_search;
    RadioButton rb_clear;
    ListView lv;
    List list;
    MyAdapter myAdapter;

    String[] arrs = {
            "倩女幽魂", "单机斗地主", "天堂战记", "妖精的尾巴", "极限挑战", "我们相爱吧", "倚天屠龙记",
            "明星大侦探", "丰乳肥臀", "大主宰", "盗墓笔记", "鬼吹灯", "盘龙", "完美世界", "柠檬初上", "WIFI",
            "锁屏", "异术超能", "东方不败", "巅峰战舰", "小说", "污"};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        et = findViewById(R.id.search_edit2);
        bt_quxiao = findViewById(R.id.bt_quxiao);
        bt_search = findViewById(R.id.bt_search);
        rb_clear = findViewById(R.id.rb_clear);
        fluidLayout = findViewById(R.id.fl);
        lv = findViewById(R.id.search_lv);
//配置热搜流式布局
        for (int i = 0; i < arrs.length; i++) {
            String ss = arrs[i];
            TextView textView = new TextView(Main2Activity.this);
            textView.setText(ss);
            textView.setTextSize(13);
            textView.setBackgroundColor(Color.parseColor("#f5f5f5"));

            FluidLayout.LayoutParams params = new FluidLayout.LayoutParams(150, 50);
            params.setMargins(12, 12, 12, 12);

            fluidLayout.addView(textView, params);
        }

        //查询数据库,配置适配器,显示历史记录listview
        mySQliteDao = new MySqlDao(Main2Activity.this);
        list = mySQliteDao.select();
        myAdapter = new MyAdapter();
        lv.setAdapter(myAdapter);


        //点击事件,返回首页
        bt_quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Main2Activity.this, MainActivity.class);
                startActivity(intent);
            }
        });
        //点击事件,添加搜索内容到数据库
        bt_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mySQliteDao.add(et.getText().toString());

                list = mySQliteDao.select();
                myAdapter.notifyDataSetChanged();


                Intent intent = new Intent(Main2Activity.this,shopActivity.class);
                startActivity(intent);
            }
        });
        //点击事件,清空数据库中的搜索内容
        rb_clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mySQliteDao.deleteAll();

                list.removeAll(list);
                myAdapter.notifyDataSetChanged();
            }
        });

    }

    //显示搜索内容listview的适配器
    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {

            TextView textView = new TextView(Main2Activity.this);
            textView.setText(list.get(i));
            textView.setPadding(15, 15, 15, 15);
            return textView;
        }
    }

}


//创建ShopActivity点击进入页面







你可能感兴趣的:(流式布局+搜索框+数据库)