增删改查
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:background="@drawable/huangdou" 6 android:orientation="vertical" 7 > 8 <LinearLayout 9 android:layout_width="match_parent" 10 android:layout_height="wrap_content" 11 android:orientation="horizontal" 12 android:gravity="center"> 13 <ImageView 14 android:layout_width="200dp" 15 android:layout_height="200dp" 16 android:background="@drawable/katong" 17 /> 18 </LinearLayout> 19 20 <LinearLayout 21 android:layout_width="match_parent" 22 android:layout_height="wrap_content" 23 android:orientation="horizontal" 24 android:layout_marginTop="10dp"> 25 26 <TextView 27 android:layout_width="wrap_content" 28 android:layout_height="wrap_content" 29 android:text="姓名:" 30 android:textSize="35dp"/> 31 <EditText 32 android:id="@+id/e_name" 33 android:layout_width="match_parent" 34 android:layout_height="wrap_content" 35 android:hint="请输入姓名" 36 android:textSize="35dp" 37 /> 38 39 </LinearLayout> 40 <LinearLayout 41 android:layout_width="match_parent" 42 android:layout_height="wrap_content" 43 android:orientation="horizontal" 44 android:layout_marginTop="10dp"> 45 46 <TextView 47 android:layout_width="wrap_content" 48 android:layout_height="wrap_content" 49 android:text="电话:" 50 android:textSize="35dp"/> 51 <EditText 52 android:id="@+id/e_phone" 53 android:layout_width="match_parent" 54 android:layout_height="wrap_content" 55 android:hint="请输入手机号码" 56 android:textSize="35dp" 57 /> 58 59 </LinearLayout> 60 <LinearLayout 61 android:layout_width="match_parent" 62 android:layout_height="wrap_content" 63 android:orientation="horizontal" 64 android:padding="10dp"> 65 66 <Button 67 android:id="@+id/insert" 68 android:layout_marginLeft="30dp" 69 android:layout_width="wrap_content" 70 android:layout_height="wrap_content" 71 android:text="添加" 72 android:textSize="34dp" 73 android:drawableLeft="@drawable/bianse1" 74 android:onClick="add" 75 /> 76 <Button 77 android:id="@+id/selet" 78 android:layout_marginLeft="40dp" 79 android:layout_width="wrap_content" 80 android:layout_height="wrap_content" 81 android:text="查询" 82 android:textSize="34dp" 83 android:drawableLeft="@drawable/bianse2" 84 android:onClick="search" 85 /> 86 </LinearLayout> 87 88 <LinearLayout 89 android:layout_width="match_parent" 90 android:layout_height="wrap_content" 91 android:orientation="horizontal" 92 android:padding="10dp"> 93 94 <Button 95 android:id="@+id/update" 96 android:layout_marginLeft="30dp" 97 android:layout_width="wrap_content" 98 android:layout_height="wrap_content" 99 android:text="修改" 100 android:textSize="34dp" 101 android:drawableLeft="@drawable/bianse3" 102 android:onClick="update" 103 /> 104 <Button 105 android:id="@+id/drop" 106 android:layout_marginLeft="40dp" 107 android:layout_width="wrap_content" 108 android:layout_height="wrap_content" 109 android:text="删除" 110 android:textSize="34dp" 111 android:drawableLeft="@drawable/bianse4" 112 android:onClick="delete" 113 /> 114 </LinearLayout> 115 <ListView 116 android:layout_width="fill_parent" 117 android:layout_height="fill_parent" 118 android:id="@+id/tv_show" 119 android:layout_marginTop="20dp" 120 android:textSize="20sp"/> 121 122 </LinearLayout> 123 124 125 126 127 package com.example.zj; 128 129 import androidx.appcompat.app.AppCompatActivity; 130 131 import android.content.ContentValues; 132 import android.database.Cursor; 133 import android.database.sqlite.SQLiteDatabase; 134 import android.database.sqlite.SQLiteOpenHelper; 135 import android.os.Bundle; 136 import android.view.View; 137 import android.view.ViewGroup; 138 import android.widget.BaseAdapter; 139 import android.widget.Button; 140 import android.widget.EditText; 141 import android.widget.ListView; 142 import android.widget.TextView; 143 import android.widget.Toast; 144 145 import java.util.ArrayList; 146 import java.util.List; 147 148 public class HdouActivity extends AppCompatActivity { 149 150 @Override 151 protected void onCreate(Bundle savedInstanceState) { 152 super.onCreate(savedInstanceState); 153 setContentView(R.layout.activity_hdou); 154 } 155 156 public void add(View view) { 157 MyHelper myHelper = new MyHelper(this); 158 SQLiteDatabase db = myHelper.getWritableDatabase(); 159 String name = ((EditText) findViewById(R.id.e_name)).getText() 160 .toString(); 161 int phone = Integer.parseInt(((EditText) findViewById(R.id.e_phone)) 162 .getText().toString()); 163 db.execSQL("insert into stu (name,phone) values(?,?)", new Object[]{ 164 name, phone}); 165 Toast.makeText(this, "ok", Toast.LENGTH_SHORT).show(); 166 } 167 168 public void delete(View view) { 169 MyHelper myHelper = new MyHelper(this); 170 SQLiteDatabase db = myHelper.getWritableDatabase(); 171 db.execSQL("delete from stuinfo where name=?", new Object[]{2}); 172 Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show(); 173 } 174 175 public void update(View view) { 176 MyHelper myHelper = new MyHelper(this); 177 SQLiteDatabase db = myHelper.getWritableDatabase(); 178 db.execSQL("update stu set name=? where name=?", new Object[]{ 179 "micky", 3}); 180 Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show(); 181 } 182 183 List<Student> list = new ArrayList<Student>(); 184 185 public void search(View view) { 186 System.out.println(list.size() + "在search里"); 187 MyHelper myHelper = new MyHelper(this); 188 SQLiteDatabase db = myHelper.getWritableDatabase(); 189 Cursor cursor = db.rawQuery("select * from stu", null); 190 if (cursor.getCount() != 0) { 191 //每循环一次,创建一个学生对象并添加到集合中 192 while (cursor.moveToNext()) { 193 // s += cursor.getInt(0) + " " + cursor.getString(1) + " " 194 // + cursor.getInt(2) + "\n"; 195 Student s1 = new Student(); 196 s1.setName(cursor.getString(0)); 197 s1.setPhone(cursor.getInt(1)); 198 list.add(s1); 199 } 200 } 201 ListView lv = (ListView) findViewById(R.id.tv_show); 202 lv.setAdapter(new myadapter()); 203 } 204 205 private class myadapter extends BaseAdapter { 206 @Override 207 public int getCount() { 208 return list.size(); 209 } 210 211 @Override 212 public Object getItem(int i) { 213 return null; 214 } 215 216 @Override 217 public long getItemId(int i) { 218 return 0; 219 } 220 221 @Override 222 public View getView(int i, View view, ViewGroup viewGroup) { 223 View view1=View.inflate(HdouActivity.this,R.layout.list_item,null); 224 TextView tvname=(TextView)view.findViewById(R.id.tv_name); 225 TextView tvphone=(TextView) view.findViewById(R.id.tv_phone); 226 227 System.out.println(list.get(i).getName()); 228 tvname.setText(list.get(i).getName()); 229 tvphone.setText(list.get(i).getName()+""); 230 231 return view; 232 } 233 } 234 } 235 236 package com.example.zj; 237 238 import android.content.Context; 239 import android.database.sqlite.SQLiteDatabase; 240 import android.database.sqlite.SQLiteOpenHelper; 241 242 import androidx.annotation.Nullable; 243 244 public class MyHelper extends SQLiteOpenHelper { 245 246 247 public MyHelper(@Nullable Context context) { 248 super(context,"itcase.db", null, 1); 249 } 250 251 @Override 252 public void onCreate(SQLiteDatabase db) { 253 db.execSQL("create table stu(name varchar(20) primary key,phone int(10))"); 254 } 255 256 @Override 257 public void onUpgrade(SQLiteDatabase db, int i, int i1) { 258 259 } 260 } 261 262 package com.example.zj; 263 264 public class Student { 265 private String name; 266 private int phone; 267 268 269 public String getName(){ 270 return name; 271 } 272 273 public void setName(String name){ 274 this.name=name; 275 } 276 public int getPhone(){ 277 return phone; 278 } 279 280 public void setPhone(int phone) { 281 this.phone = phone; 282 } 283 }