//main布局LinearLayout
android:layout_width="wrap_content"
android:layout_height="100px"></com.lixinyang.zhoukao1_test.Views.MyTitle>
android:layout_width="match_parent"
android:layout_height="1px"
android:background="@color/black" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Myattr:textString="热搜"
Myattr:textSize="20dp"/>
android:layout_width="match_parent"
android:layout_height="200px">
android:layout_width="wrap_content"
android:layout_height="70px"
android:text="羊毛衫 新品" />
android:layout_width="wrap_content"
android:layout_height="70px"
android:text="碟子" />
android:layout_height="70px"
android:text="苹果8" />
android:layout_height="70px"
android:text="髌骨带" />
android:layout_height="70px"
android:text="胸罩" />
android:layout_height="70px"
android:text="瑜伽球" />
android:layout_height="70px"
android:text="超级实惠三只老鼠" />
android:layout_height="70px"
android:text="冬季运动保暖裤" />
android:layout_height="70px"
android:text="毛衣" />
android:layout_height="70px"
android:text="加长版风衣" />
</com.lixinyang.zhoukao1_test.Views.MyGroupView>
<com.lixinyang.zhoukao1_test.Views.MyText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Myattr:textString="历史搜索"
Myattr:textSize="20dp"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/lv"></ListView>
<com.lixinyang.zhoukao1_test.Views.MyFooter
android:layout_width="wrap_content"
android:layout_height="100px"></com.lixinyang.zhoukao1_test.Views.MyFooter>
//activity_title布局RelativeLayout
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/bb"
android:id="@+id/iv"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="30px"/>
<EditText
android:layout_width="500px"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="@drawable/radioimage"
android:layout_centerVertical="true"
android:id="@+id/et"
android:gravity="center"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="80dp"
android:src="@drawable/dd" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="搜索"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="30px"
android:id="@+id/tv"/>
//activity_item布局
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_item1"/>
<LinearLayout
android:layout_width="200px"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentRight="true">
<TextView
android:background="@drawable/buttonborder"
android:layout_width="0px"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="@+id/tv_item2"/>
<TextView
android:background="@drawable/buttonborder"
android:layout_width="0px"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="@+id/tv_item3"/>
<TextView
android:background="@drawable/buttonborder"
android:layout_width="0px"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="@+id/tv_item4"/>
</LinearLayout>
//activity_footer
android:layout_width="500px"
android:layout_height="70px"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="清空历史记录"
android:background="@drawable/buttonborder"
android:id="@+id/btn_clear"
/>
<ImageView
android:layout_height="wrap_content"
android:src="@drawable/delete"
android:layout_centerVertical="true"
android:layout_marginLeft="250px"/>
//Main
public class MainActivity extends AppCompatActivity {
private static ListView lv;
private static List<Users> select;
private static MyAdapter adapter;
private static Dao dao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lv);
dao = new Dao(MainActivity.this);
select = dao.select();
adapter = new MyAdapter();
lv.setAdapter(adapter);
}
class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
return select.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if(view==null){
view=View.inflate(MainActivity.this,R.layout.activity_item,null);
}
TextView tv_item1 = view.findViewById(R.id.tv_item1);
TextView tv_item2 = view.findViewById(R.id.tv_item2);
TextView tv_item3 = view.findViewById(R.id.tv_item3);
TextView tv_item4 = view.findViewById(R.id.tv_item4);
tv_item1.setText(select.get(i).getA());
tv_item2.setText(select.get(i).getAa());
tv_item3.setText(select.get(i).getAaa());
tv_item4.setText(select.get(i).getAaaa());
return view;
}
}
public static void aa(){
select = dao.select();
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
//Views
//MyTitle
private ImageView imageView;
private TextView textView;
this(context,null);
}
this(context, attrs,0);
}
super(context, attrs, defStyleAttr);
View view = LayoutInflater.from(context).inflate(R.layout.activity_title, this, true);
editText = view.findViewById(R.id.et);
imageView = view.findViewById(R.id.iv);
textView = view.findViewById(R.id.tv);
imageView.setOnClickListener(this);
textView.setOnClickListener(this);
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
public boolean onTouchEvent(MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
}
return true;
}
public void onClick(View view) {
switch(view.getId()){
case R.id.iv:
Toast.makeText(getContext(),"我点了返回",Toast.LENGTH_SHORT).show();
break;
case R.id.tv:
Dao dao = new Dao(getContext());
String a = editText.getText().toString();
Users users = new Users(a,"新品","特别厚","挨冻");
dao.ins(users);
MainActivity.aa();
break;
}
public static void ss(String str){
editText.setText(str);
}
}
//MyText
public class MyText extends View {
private float size;
private String content;
private Paint paint;
private Rect rect;
public MyText(Context context) {
this(context,null);
}
this(context, attrs,0);
}
super(context, attrs, defStyleAttr);
//获取到你控件的属性并形成数组
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.mystyle, defStyleAttr, 0);
//取出里面的属性
content = ta.getString(R.styleable.mystyle_textString);
size = ta.getDimension(R.styleable.mystyle_textSize, 40);
//初始化画笔
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setTextSize(size);
rect = new Rect();
//第一个参数放置你的文本
// 第二个参数是你字符串开始的下标
//第三个参数是你要字符串的长度
//第四个参数返回给调用者
paint.getTextBounds(content,0,content.length(), rect);
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawText(content,0, 30,paint);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//获取测量的模式和测量的尺寸
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int width;
int height;
//判断测量模式并将你的控件大小改变
if(widthMode==MeasureSpec.EXACTLY){
width=widthSize;
}else{
int width1 = rect.width();
width=width1+getPaddingLeft()+getPaddingRight()+10;
}
if(heightMode==MeasureSpec.EXACTLY){
height=heightSize;
}else{
int height1 = rect.height();
height = height1+getPaddingTop()+getPaddingBottom()+10;
}
//将屏幕宽高度从新赋值
setMeasuredDimension(width,height);
}
//MyGroupView
Button btn;
View view;
AttributeSet attributeSet;
public MyGroupView(Context context) {
this(context,null);
}
super(context, attrs,0);
}
super(context, attrs, defStyleAttr);
}
@Override
public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs)
{
return new MarginLayoutParams(getContext(), attrs);
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
measureChildren(widthMeasureSpec, heightMeasureSpec);
protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
/**
* 一般是定义为int top;一个top实际上是数组的下标
left : 指定矩形框左上角的x坐标
top: 指定矩形框左上角的y坐标
right: 指定矩形框右下角的x坐标
bottom:指定矩形框右下角的y坐标
*/
int width = getWidth();
int height = getHeight();
int tw = 0;
int th = 0;
for (int ii = 0; ii < getChildCount(); ii++) {
View child = getChildAt(ii);
view = child;
btn = (Button) view;
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Dao dao = new Dao(getContext());
Button bt = (Button) view;
String a = bt.getText().toString();
Users users = new Users(a,"新品","特别厚","挨冻");
dao.ins(users);
MainActivity.aa();
MyTitle.ss(a);
}
});
tw = 0;
th += child.getMeasuredHeight(); //超过屏幕的宽度,自动换行
}
tw += child.getMeasuredWidth();
}
}
//MyFooter
public MyFooter(Context context) {
this(context,null);
}
this(context, attrs,0);
}
super(context, attrs, defStyleAttr);
View view = LayoutInflater.from(context).inflate(R.layout.activity_footer, this,true);
Button btn = view.findViewById(R.id.btn_clear);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Dao dao = new Dao(getContext());
dao.clear();
MainActivity.aa();
}
});
}
}
//MySqlHelp
//Dao
//获取帮助类对象--->通过帮助类对象得到操作数据库对象(mh.getWritableDatabase())----> SQLiteDatabase
Mysql mh;
super();
mh=new Mysql(ctx); //获取帮助类实例对象
}
public void add(){
//获取SQLiteDatabase对象
SQLiteDatabase database = mh.getWritableDatabase();
//执行添加的操作
database.execSQL("insert into t_users values (?,'zhaojunchao','15110033445')"); //无返回值
database.close();
}*/
public void delete(){
//获取SQLiteDatabase对象
SQLiteDatabase database = mh.getWritableDatabase();
//执行添加的操作 执行sql语句
database.execSQL("delete from t_users where _id=1"); //无返回值
database.close();
}
// 创建存放所有行对象集合
List<Users> us=new ArrayList<Users>();
SQLiteDatabase database = mh.getWritableDatabase();
Cursor query = database.rawQuery("select * from t_users", null); //null是为sequel语句中的占位符赋值
while(query.moveToNext()){
//获取当前行中每个列的值
int _id = query.getInt(0);
String uname= query.getString(1);
String tel= query.getString(2);
Users u=new Users(_id, uname, tel);
us.add(u);
}
database.close();
}*/
public List<Users> select() {
// 创建存放所有行对象集合
List<Users> us = new ArrayList<Users>();
SQLiteDatabase database = mh.getWritableDatabase();
Cursor query = database.rawQuery("select * from t_shop", null); //null是为sequel语句中的占位符赋值
while (query.moveToNext()) {
//获取当前行中每个列的值
String a = query.getString(0);
String aa = query.getString(1);
String aaa = query.getString(2);
String aaaa = query.getString(3);
Users u = new Users(a, aa, aaa, aaaa);
us.add(u);
}
database.close();
return us;
}
public void ins(Users users){
SQLiteDatabase database = mh.getWritableDatabase();
database.execSQL("insert into t_shop values('"+users.getA()+"','"+users.getAa()+"','"+users.getAaa()+"','"+users.getAaaa()+"')");
database.close();
}
public void clear(){
SQLiteDatabase database = mh.getWritableDatabase();
database.execSQL("delete from t_shop");
}
}
//Mysql
public class Mysql extends SQLiteOpenHelper {
public Mysql(Context context) {
super(context, "lixinyang", null, 1);
}
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("create table t_shop (a varchar(20), aa varchar(11), aaa varchar(20) , aaaa varchar(20))");
sqLiteDatabase.execSQL("insert into t_shop values('羊毛衫男','新品','恒轩祥','高领')");
sqLiteDatabase.execSQL("insert into t_shop values('衬衣','新品','男长袖','保暖')");
}
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
//Users
private String a;
private String aa;
private String aaa;
private String aaaa;
this.a = a;
this.aa = aa;
this.aaa = aaa;
this.aaaa = aaaa;
}
return a;
}
this.a = a;
}
return aa;
}
this.aa = aa;
}
return aaa;
}
this.aaa = aaa;
}
return aaaa;
}
this.aaaa = aaaa;
}
}