Android作业----------------listview1.0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | <? xml version="1.0" encoding="utf-8"?> < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/huangdou" android:orientation="vertical" > < LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center"> < ImageView android:layout_width="200dp" android:layout_height="200dp" android:background="@drawable/katong" /> </ LinearLayout > < LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="10dp"> < TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="姓名:" android:textSize="35dp"/> < EditText android:id="@+id/e_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入姓名" android:textSize="35dp" /> </ LinearLayout > < LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="10dp"> < TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="电话:" android:textSize="35dp"/> < EditText android:id="@+id/e_phone" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入手机号码" android:textSize="35dp" /> </ LinearLayout > < LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="10dp"> < Button android:id="@+id/insert" android:layout_marginLeft="30dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="添加" android:textSize="34dp" android:drawableLeft="@drawable/bianse1" android:onClick="add" /> < Button android:id="@+id/selet" android:layout_marginLeft="40dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="查询" android:textSize="34dp" android:drawableLeft="@drawable/bianse2" android:onClick="search" /> </ LinearLayout > < LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="10dp"> < Button android:id="@+id/update" android:layout_marginLeft="30dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="修改" android:textSize="34dp" android:drawableLeft="@drawable/bianse3" android:onClick="update" /> < Button android:id="@+id/drop" android:layout_marginLeft="40dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="删除" android:textSize="34dp" android:drawableLeft="@drawable/bianse4" android:onClick="delete" /> </ LinearLayout > < ListView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/tv_show" android:layout_marginTop="20dp" android:textSize="20sp"/> </ LinearLayout > |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; 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.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class HdouActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_hdou); } public void add(View view) { MyHelper myHelper = new MyHelper( this ); SQLiteDatabase db = myHelper.getWritableDatabase(); String name = ((EditText) findViewById(R.id.e_name)).getText() .toString(); int phone = Integer.parseInt(((EditText) findViewById(R.id.e_phone)) .getText().toString()); db.execSQL( "insert into stu (name,phone) values(?,?)" , new Object[]{ name, phone}); Toast.makeText( this , "ok" , Toast.LENGTH_SHORT).show(); } public void delete(View view) { MyHelper myHelper = new MyHelper( this ); SQLiteDatabase db = myHelper.getWritableDatabase(); db.execSQL( "delete from stuinfo where name=?" , new Object[]{ 2 }); Toast.makeText( this , "删除成功" , Toast.LENGTH_SHORT).show(); } public void update(View view) { MyHelper myHelper = new MyHelper( this ); SQLiteDatabase db = myHelper.getWritableDatabase(); db.execSQL( "update stu set name=? where name=?" , new Object[]{ "micky" , 3 }); Toast.makeText( this , "修改成功" , Toast.LENGTH_SHORT).show(); } List<Student> list = new ArrayList<Student>(); public void search(View view) { System.out.println(list.size() + "在search里" ); MyHelper myHelper = new MyHelper( this ); SQLiteDatabase db = myHelper.getWritableDatabase(); Cursor cursor = db.rawQuery( "select * from stu" , null ); if (cursor.getCount() != 0 ) { //每循环一次,创建一个学生对象并添加到集合中 while (cursor.moveToNext()) { // s += cursor.getInt(0) + " " + cursor.getString(1) + " " // + cursor.getInt(2) + "\n"; Student s1 = new Student(); s1.setName(cursor.getString( 0 )); s1.setPhone(cursor.getInt( 1 )); list.add(s1); } } ListView lv = (ListView) findViewById(R.id.tv_show); lv.setAdapter( new myadapter()); } private class myadapter extends BaseAdapter { @Override public int getCount() { return list.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) { View view1=View.inflate(HdouActivity. this ,R.layout.list_item, null ); TextView tvname=(TextView)view.findViewById(R.id.tv_name); TextView tvphone=(TextView) view.findViewById(R.id.tv_phone); System.out.println(list.get(i).getName()); tvname.setText(list.get(i).getName()); tvphone.setText(list.get(i).getName()+ "" ); return view; } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | package com.example.myapplication; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import androidx.annotation.Nullable; public class MyHelper extends SQLiteOpenHelper { public MyHelper( @Nullable Context context) { super (context, "itcase.db" , null , 1 ); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL( "create table stu(name varchar(20) primary key,phone int(10))" ); } @Override public void onUpgrade(SQLiteDatabase db, int i, int i1) { } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | package com.example.myapplication; public class Student { private String name; private int phone; public String getName(){ return name; } public void setName(String name){ this .name=name; } public int getPhone(){ return phone; } public void setPhone( int phone) { this .phone = phone; } } |
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步