模拟搜索功能

public class MyView extends LinearLayout {

    private Dao dao;

    private List<String> select;
    private MyViewGroup mvg;
  
    private ListView lv;
    private ArrayAdapter<String> arrayAdapter;

    public MyView(Context context) {
        this(context,null);
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        dao = new Dao(getContext());
        initview(context);


        mvg = findViewById(R.id.mvg);
    }

    public interface TitleActivityClickLinsner{
        void onClickleft();

    }

    private TitleActivityClickLinsner titleActivityClickLinsner;

    public void setOnTitleClickLinsner(TitleActivityClickLinsner titleActivityClickLinsner){
        this.titleActivityClickLinsner=titleActivityClickLinsner;
    }





    private void initview(final Context context) {
        View view = View.inflate(context, R.layout.myview, this);
        final EditText et_name = view.findViewById(R.id.et_name);
        TextView sou = view.findViewById(R.id.sou);
        ImageView dele = view.findViewById(R.id.dele);
        lv = view.findViewById(R.id.tv_lv);
        dele.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                dao.deleteData();
               // lv.removeAllViews();

               // arrayAdapter.notifyDataSetChanged();
                select = dao.select();
                select.clear();
                arrayAdapter = new ArrayAdapter(getContext(), android.R.layout.simple_list_item_1,select);
                lv.setAdapter(arrayAdapter);
                arrayAdapter.notifyDataSetChanged();
            }
        });
        sou.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
              /* if (titleActivityClickLinsner!=null){
                    titleActivityClickLinsner.onClickleft();
                }*/
                String name = et_name.getText().toString().trim();
                dao.insertData(name);
                Toast.makeText(context,name,Toast.LENGTH_LONG).show();
                select = dao.select();
               // initChildViews();
                Log.e("zzz","查询的数据为"+select.size());
               // for (int i = 0; i < select.size(); i++) {
                   // String s = select.get(i);
                arrayAdapter = new ArrayAdapter(getContext(), android.R.layout.simple_list_item_1,select);
               /* for (int i = 0; i < select.size(); i++) {
                    lv.setText(select.get(i));
                }*/

                    lv.setAdapter(arrayAdapter);
                    arrayAdapter.notifyDataSetChanged();
               // }

                // 创建adapter适配器对象,装入模糊搜索的结果
             /*   adapter = new SimpleAdapter(this, android.R.layout.simple_list_item_1,s);
                // 设置适配器
                lvSearch.setAdapter(adapter);
                adapter.notifyDataSetChanged();*/
            /* new Thread(){
                 @Override
                 public void run() {
                     ArrayAdapter<String> arrayAdapter = new ArrayAdapter(getContext(), android.R.layout.simple_list_item_1,s);

                 }
             }.start();*/

            }
        });

}

}

public class MainActivity extends AppCompatActivity {
    private String mNames[] = {
            "羊毛衫", "裤子", "碟子",
            "apple8", "瑜伽球", "气垫",
            "欧莱雅", "素颜霜", "防晒霜",
            "连衣裙", "帆布鞋", "大衣",
           };
    private MyViewGroup mvg;
    private MyView ding;
    private MyView zdy_view;
    private Dao dao;
    private TextView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dao = new Dao(this);


        zdy_view = findViewById(R.id.zdy_view);
        mvg = findViewById(R.id.mvg);
    /*   zdy_view.setOnTitleClickLinsner(new MyView.TitleActivityClickLinsner() {
            @Override
            public void onClickleft() {
                List<String> select = dao.select();
                for (int i = 0; i < select.size(); i++) {
                    lv.setText(i);
                }
            }
        });*/
        initChildViews();
    }

    private void initChildViews() {


        ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.leftMargin = 5;
        lp.rightMargin = 5;
        lp.topMargin = 5;
        lp.bottomMargin = 5;
        for (int i = 0; i < mNames.length; i++) {
            TextView view = new TextView(this);
            view.setText(mNames[i]);
            view.setTextColor(Color.WHITE);
            view.setBackgroundColor(Color.GRAY);
            // view.setWidth(120);
            view.setPadding(15, 3, 15, 4);
            //view.setBackgroundDrawable(getResources().getDrawable(R.drawable.textview_bg));
            mvg.addView(view, lp);
        }

    }
}

最后数据库dao类

public class Dao {

    private final SQLiteDatabase db;

    public Dao(Context context) {
        MyHelper helper = new MyHelper(context);
        db = helper.getWritableDatabase();
    }

    /*插入数据*/
    public void insertData(String tempName) {
        ContentValues values = new ContentValues();
        values.put("name",tempName);
        long records = db.insert("records", null, values);
        Log.e("zzz","插入了"+records);
       /* db.execSQL("insert into records(name) values('" + tempName + "')");
        db.close();*/
    }

    public List<String> select(){
        List<String> list = new ArrayList<>();
        Log.e("zzz","查询了数据库");
        String name="";
        Cursor cursor = db.query("records", null, null,null, null, null, null);
        while (cursor.moveToNext()){
             name = cursor.getString(cursor.getColumnIndex("name"));
            list.add(name);
        }



        return list;
    }

    public void deleteData() {

        db.delete("records",null,null);
    }


}



阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页