網站首頁 編程語言 正文
本文實例為大家分享了AndriodStudio利用ListView和數據庫實現簡單學生管理的具體代碼,供大家參考,具體內容如下
數據庫的創建
package com.example.myapp; ? import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.widget.Toast; ? public class DbHelper extends SQLiteOpenHelper { ? ? final String create_table="CREATE TABLE Student (_id integer primary key autoincrement,xm text,xh text,bj text,zy text,cj text)"; ? ? final String create_register="CREATE TABLE Register (_id integer primary key autoincrement,xm text,xh text)"; //創建兩張表,一張student,一張register表 ? ? Context context; ? ? public DbHelper(Context context,String dbname,int version){ ? ? ? ? super(context,dbname,null,version); ? ? ? ? this.context=context;//上下文 ? ? } ? ? @Override ? ? public void onCreate(SQLiteDatabase db) {db.execSQL(create_table); db.execSQL(create_register);} ? ? ? @Override ? ? public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { ? ? ? ? db.execSQL("drop table if exists Student"); ? ? ? ? db.execSQL("drop table if exists Register"); ? ? ? ? db.execSQL(create_table); ? ? ? ? db.execSQL(create_register); //完成創建 ? ? } // 對表進行數據的插入方法 ? ? public void insert(String tableName, ContentValues values){ ? ? ? ? SQLiteDatabase db = getReadableDatabase(); ? ? ? ? db.insert(tableName,null,values); ? ? ? ? Toast.makeText(context,"成功插入數據!",Toast.LENGTH_SHORT).show(); ? ? } //查詢表中所有 ? ? public Cursor queryAll(String tableName){ ? ? ? ? SQLiteDatabase db = getReadableDatabase(); ? ? ? ? Cursor cursor = db.query(tableName,null,null,null,null,null,null); ? ? ? ? return cursor; ? ? } //對表中的姓名和學號進行單獨的查詢 ? ? public Boolean queryByStudentXhAndXm(String tableName,String xm,String xh){ ? ? ? ? SQLiteDatabase db = getReadableDatabase(); ? ? ? ? Cursor cursor = db.query(tableName,new String[]{"xm,xh"},"xm=? and xh=?",new String[]{xm,xh},null,null,null); ? ? ? ? if (cursor.moveToFirst()) { ? ? ? ? ? ? return true; ? ? ? ? }else { ? ? ? ? ? ? return false; ? ? ? ? } ? ? } //刪除表中數據的方法 ? ? public void delStudent(String id){ ? ? ? ? SQLiteDatabase db = getWritableDatabase(); ? ? ? ? db.delete("Student","_id=?",new String[]{id}); ? ? } //對表進行更新 ? ? public void ?updateStudent(String id,ContentValues values){ ? ? ? ? SQLiteDatabase db = getWritableDatabase(); ? ? ? ? db.update("Student",values,"_id=?",new String[]{id}); ? ? } }
創建了兩張表格,一張用來儲存學生信息,Student表:id(主鍵),姓名,學號,班級,專業,成績。
一張register表:id(主鍵),姓名,學號。用來儲存登錄信息
其余類和其布局文件
MainActivity.class(登錄界面)
package com.example.myapp; ? import androidx.appcompat.app.AppCompatActivity; ? import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; ? public class MainActivity extends AppCompatActivity { ? ? EditText et1,et2; ? ? Button btn1,btn2; ? ? DbHelper dbHelper; ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? dbHelper=new DbHelper(MainActivity.this,"MyDataBase,",3); ? ? ? ? setContentView(R.layout.activity_main); ? ? ? ? et1=findViewById(R.id.et1); ? ? ? ? et2=findViewById(R.id.et2); ? ? ? ? btn1=findViewById(R.id.dl); ? ? ? ? btn2=findViewById(R.id.zc); ? ? ? ? ? btn1.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View v) { ? ? ? ? ? ? ? ? String xm=et1.getText().toString(); ? ? ? ? ? ? ? ? String xh=et2.getText().toString(); ? ? ? ? ? ? ? ? if (xm.isEmpty()||xh.isEmpty()){ ? ? ? ? ? ? ? ? ? ? Toast.makeText(MainActivity.this,"姓名或者學號不可為空",Toast.LENGTH_SHORT).show(); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? if (dbHelper.queryByStudentXhAndXm("Register",xm,xh)){ ? ? ? ? ? ? ? ? ? ? Intent intent=new Intent(MainActivity.this,Manage.class); ? ? ? ? ? ? ? ? ? ? startActivity(intent); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? }); ? ? ? ? btn2.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View v) { ? ? ? ? ? ? ? ? Intent intent=new Intent(MainActivity.this,Register.class); ? ? ? ? ? ? ? ? startActivity(intent); ? ? ? ? ? ? } ? ? ? ? }); ? ? } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:app="http://schemas.android.com/apk/res-auto" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? android:orientation="vertical" ? ? tools:context=".MainActivity"> ? ? ? <EditText ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:id="@+id/et1" ? ? ? ? android:hint="輸入姓名"/> ? ? <EditText ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:id="@+id/et2" ? ? ? ? android:hint="學號"/> ? ? <Button ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:id="@+id/dl" ? ? ? ? android:text="登錄"/> ? ? <Button ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:text="注冊" ? ? ? ? android:id="@+id/zc"/> ? </LinearLayout>
Register.class(注冊界面)
package com.example.myapp; ? import androidx.appcompat.app.AppCompatActivity; ? import android.content.ContentValues; import android.content.Intent; import android.database.Cursor; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; ? public class Register extends AppCompatActivity { ? ? EditText et1,et2; ? ? Button btn1,btn2; ? ? DbHelper dbHelper; ? ? TextView show; ? ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.activity_register); ? ? ? ? dbHelper=new DbHelper(Register.this,"MyDataBase,",3); ? ? ? ? et1=findViewById(R.id.xm_zc); ? ? ? ? et2=findViewById(R.id.xh_zc); ? ? ? ? show=findViewById(R.id.tv_show); ? ? ? ? btn1=findViewById(R.id.qd_zc); ? ? ? ? btn2=findViewById(R.id.fh); ? ? ? ? ? btn1.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View v) { ? ? ? ? ? ? ? ? String xm=et1.getText().toString(); ? ? ? ? ? ? ? ? String xh=et2.getText().toString(); ? ? ? ? ? ? ? ? if(TextUtils.isEmpty(xm)||TextUtils.isEmpty(xh)){ ? ? ? ? ? ? ? ? ? ? Toast.makeText(Register.this,"姓名或者學號不能為空",Toast.LENGTH_SHORT).show(); ? ? ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ContentValues values = new ContentValues(); ? ? ? ? ? ? ? ? values.put("xm",xm); ? ? ? ? ? ? ? ? values.put("xh",xh); ? ? ? ? ? ? ? ? dbHelper.insert("Register",values); ? ? ? ? ? ? ? ? showUser(); ? ? ? ? ? ? } ? ? ? ? }); ? ? ? ? btn2.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View v) { ? ? ? ? ? ? ? ? Intent intent=new Intent(Register.this,MainActivity.class); ? ? ? ? ? ? ? ? startActivity(intent); ? ? ? ? ? ? } ? ? ? ? }); ? ? } ? ? public void showUser(){ ? ? ? ? Cursor cursor = dbHelper.queryAll("Register"); ? ? ? ? String str = "_id ? ? ? ?xm ? ? ? ? xh\n"; ? ? ? ? if (cursor.moveToFirst()) ? ? ? ? ? ? while (cursor.moveToNext()){ ? ? ? ? ? ? ? ? str += cursor.getString(0)+" ? ? ?"; ? ? ? ? ? ? ? ? str += cursor.getString(1)+" ? ? ?"; ? ? ? ? ? ? ? ? str += cursor.getString(2)+"\n"; ? ? ? ? ? ? }; ? ? ? ? show.setText(str); ? ? } }
activity_register.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:app="http://schemas.android.com/apk/res-auto" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? android:orientation="vertical" ? ? tools:context=".Register"> ? ? <EditText ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:id="@+id/xm_zc" ? ? ? ? android:hint="輸入姓名"/> ? ? <EditText ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:id="@+id/xh_zc" ? ? ? ? android:hint="輸入學號"/> ? ? <Button ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:text="確認注冊" ? ? ? ? android:id="@+id/qd_zc"/> ? ? <Button ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:text="返回上一層" ? ? ? ? android:id="@+id/fh"/> ? ? <TextView ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="match_parent" ? ? ? ? android:id="@+id/tv_show"/> ? </LinearLayout>
Manage.class(管理界面)
package com.example.myapp; ? import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; ? import android.content.ContentValues; import android.content.DialogInterface; import android.content.Intent; import android.database.Cursor; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.Button; import android.widget.ListView; import android.widget.SimpleCursorAdapter; ? public class Manage extends AppCompatActivity { ? ? ListView listView; ? ? Button btn; ? ? DbHelper dbHelper; ? ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.activity_manage); ? ? ? ? AlertDialog.Builder builder = new AlertDialog.Builder(Manage.this); ? ? ? ? dbHelper = new DbHelper(Manage.this,"MyDataBase,",3); ? ? ? ? listView=findViewById(R.id.list); ? ? ? ? dbHelper.getWritableDatabase(); ? ? ? ? renderListView(); ? ? ? ? btn=findViewById(R.id.new_list); ? ? ? ? btn.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View v) { ? ? ? ? ? ? ? ? Intent intent=new Intent(Manage.this,NewStudent.class); ? ? ? ? ? ? ? ? startActivityForResult(intent,1); ? ? ? ? ? ? } ? ? ? ? }); ? ? ? ? listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onItemClick(AdapterView<?> parent, View view, int position, long idi) { ? ? ? ? ? ? ? ? Cursor cursor = dbHelper.queryAll("Student"); ? ? ? ? ? ? ? ? cursor.move(position+1); ? ? ? ? ? ? ? ? String id = cursor.getString(cursor.getColumnIndex("_id")); ? ? ? ? ? ? ? ? String xm = cursor.getString(cursor.getColumnIndex("xm")); ? ? ? ? ? ? ? ? String xh = cursor.getString(cursor.getColumnIndex("xh")); ? ? ? ? ? ? ? ? String bj = cursor.getString(cursor.getColumnIndex("bj")); ? ? ? ? ? ? ? ? String zy = cursor.getString(cursor.getColumnIndex("zy")); ? ? ? ? ? ? ? ? String cj = cursor.getString(cursor.getColumnIndex("cj")); ? ? ? ? ? ? ? ? Intent intent = new Intent(Manage.this,NewStudent.class); ? ? ? ? ? ? ? ? intent.putExtra("id",id); ? ? ? ? ? ? ? ? intent.putExtra("xm",xm); ? ? ? ? ? ? ? ? intent.putExtra("xh",xh); ? ? ? ? ? ? ? ? intent.putExtra("bj",bj); ? ? ? ? ? ? ? ? intent.putExtra("zy",zy); ? ? ? ? ? ? ? ? intent.putExtra("cj",cj); ? ? ? ? ? ? ? ? startActivityForResult(intent,2); ? ? ? ? ? ? } ? ? ? ? }); ? ? ? ? listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long idi) { ? ? ? ? ? ? ? ? Cursor cursor = dbHelper.queryAll("Student"); ? ? ? ? ? ? ? ? cursor.move(position+1); ? ? ? ? ? ? ? ? String id = cursor.getString(cursor.getColumnIndex("_id"));//getColumnIndex("_id")得到這一列 ? ? ? ? ? ? ? ? String xm = cursor.getString(cursor.getColumnIndex("xm")); ? ? ? ? ? ? ? ? builder.setTitle("刪除確認").setMessage("是否確認刪除學生"+xm); ? ? ? ? ? ? ? ? builder.setPositiveButton("確定", new DialogInterface.OnClickListener() { ? ? ? ? ? ? ? ? ? ? @Override ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int which) { ? ? ? ? ? ? ? ? ? ? ? ? dbHelper.delStudent(id); ? ? ? ? ? ? ? ? ? ? ? ? renderListView(); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? }); ? ? ? ? ? ? ? ? builder.show(); ? ? ? ? ? ? ? ? return true; ? ? ? ? ? ? } ? ? ? ? }); ? ? } ? ? private void renderListView() { ? ? ? ? Cursor cursor = dbHelper.queryAll("Student"); ? ? ? ? String from[] = new String[]{"_id", "xm", "xh","bj","zy","cj"}; ? ? ? ? int to[] = new int[]{R.id.id,R.id.xm, R.id.xh, R.id.bj, R.id.zy, R.id.cj}; ? ? ? ? SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.listview, cursor, from, to, 0); ? ? ? ? ListView listView = findViewById(R.id.list); ? ? ? ? listView.setAdapter(adapter); ? ? } ? ? @Override ? ? protected ?void onActivityResult(int reqCode,int resultCode,Intent intent){ ? ? ? ? super.onActivityResult(reqCode,resultCode,intent); ? ? ? ? if (resultCode==RESULT_OK){ ? ? ? ? ? ? String xm = intent.getStringExtra("xm"); ? ? ? ? ? ? String xh = intent.getStringExtra("xh"); ? ? ? ? ? ? String bj = intent.getStringExtra("bj"); ? ? ? ? ? ? String zy = intent.getStringExtra("zy"); ? ? ? ? ? ? String cj = intent.getStringExtra("cj"); ? ? ? ? ? ? dbHelper.getWritableDatabase(); ? ? ? ? ? ? ContentValues values = new ContentValues(); ? ? ? ? ? ? values.put("xm",xm); ? ? ? ? ? ? values.put("xh",xh); ? ? ? ? ? ? values.put("bj",bj); ? ? ? ? ? ? values.put("zy",zy); ? ? ? ? ? ? values.put("cj",cj); ? ? ? ? ? ? if (reqCode==1) ? ? ? ? ? ? ? ? dbHelper.insert("Student",values); ? ? ? ? ? ? else if (reqCode==2){ ? ? ? ? ? ? ? ? String id = intent.getStringExtra("id"); ? ? ? ? ? ? ? ? dbHelper.updateStudent(id,values); ? ? ? ? ? ? } ? ? ? ? ? ? renderListView(); ? ? ? ? } ? ? } }
activity_manage.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:app="http://schemas.android.com/apk/res-auto" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? android:orientation="vertical" ? ? tools:context=".Manage"> ? ? ? <ListView ? ? ? ? android:id="@+id/list" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" /> ? ? ? <Button ? ? ? ? android:id="@+id/new_list" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:text="添加新學生" /> ? </LinearLayout>
對應的listview的布局文件,listview.xml
因為使用的是數據庫來儲存信息和調用,并沒有重寫對于listview的Adapter,而是使用Android自帶的SimpleCursorAdapter類方法,用游標來儲存,查看數據
<?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:orientation="vertical"> ? ? <LinearLayout ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:orientation="horizontal"> ? ? ? ? <TextView ? ? ? ? ? ? android:layout_width="0dp" ? ? ? ? ? ? android:layout_height="50dp" ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? android:text="id"/> ? ? ? ? ? <TextView ? ? ? ? ? ? android:layout_width="0dp" ? ? ? ? ? ? android:layout_height="50dp" ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? android:text="姓名" /> ? ? ? ? ? <TextView ? ? ? ? ? ? android:layout_width="0dp" ? ? ? ? ? ? android:layout_height="50dp" ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? android:text="學號" /> ? ? ? ? ? <TextView ? ? ? ? ? ? android:layout_width="0dp" ? ? ? ? ? ? android:layout_height="50dp" ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? android:text="班級" /> ? ? ? ? ? <TextView ? ? ? ? ? ? android:layout_width="0dp" ? ? ? ? ? ? android:layout_height="50dp" ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? android:text="專業" /> ? ? ? ? ? <TextView ? ? ? ? ? ? android:layout_width="0dp" ? ? ? ? ? ? android:layout_height="50dp" ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? android:text="成績" /> ? ? ? ? </LinearLayout> ? ? ? <LinearLayout ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:orientation="horizontal"> ? ? ? ? <TextView ? ? ? ? ? ? android:id="@+id/id" ? ? ? ? ? ? android:layout_width="0dp" ? ? ? ? ? ? android:layout_height="50dp" ? ? ? ? ? ? android:layout_weight="1" /> ? ? ? ? ? <TextView ? ? ? ? ? ? android:id="@+id/xm" ? ? ? ? ? ? android:layout_width="0dp" ? ? ? ? ? ? android:layout_height="50dp" ? ? ? ? ? ? android:layout_weight="1" /> ? ? ? ? ? <TextView ? ? ? ? ? ? android:id="@+id/xh" ? ? ? ? ? ? android:layout_width="0dp" ? ? ? ? ? ? android:layout_height="50dp" ? ? ? ? ? ? android:layout_weight="1" /> ? ? ? ? ? <TextView ? ? ? ? ? ? android:id="@+id/bj" ? ? ? ? ? ? android:layout_width="0dp" ? ? ? ? ? ? android:layout_height="50dp" ? ? ? ? ? ? android:layout_weight="1" /> ? ? ? ? ? <TextView ? ? ? ? ? ? android:id="@+id/zy" ? ? ? ? ? ? android:layout_width="0dp" ? ? ? ? ? ? android:layout_height="50dp" ? ? ? ? ? ? android:layout_weight="1" /> ? ? ? ? ? <TextView ? ? ? ? ? ? android:id="@+id/cj" ? ? ? ? ? ? android:layout_width="0dp" ? ? ? ? ? ? android:layout_height="50dp" ? ? ? ? ? ? android:layout_weight="1" /> ? ? </LinearLayout> ? </LinearLayout>
對于新添加學生操作,使用NewStudent.class來完成
package com.example.myapp; ? import androidx.appcompat.app.AppCompatActivity; ? import android.content.Intent; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; ? public class NewStudent extends AppCompatActivity { ? ? DbHelper dbHelper; ? ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.activity_new_student); ? ? ? ? dbHelper=new DbHelper(NewStudent.this,"MyDataBase",3); ? ? ? ? Intent priIntent = getIntent(); ? ? ? ? ? EditText et_xm = findViewById(R.id.et_xm); ? ? ? ? EditText et_xh = findViewById(R.id.et_xh); ? ? ? ? EditText et_bj = findViewById(R.id.et_bj); ? ? ? ? EditText et_zy = findViewById(R.id.et_zy); ? ? ? ? EditText et_cj = findViewById(R.id.et_cj); ? ? ? ? String priId = priIntent.getStringExtra("id"); ? ? ? ? String prixm = priIntent.getStringExtra("xm"); ? ? ? ? String prixh = priIntent.getStringExtra("xh"); ? ? ? ? String pribj = priIntent.getStringExtra("bj"); ? ? ? ? String prizy = priIntent.getStringExtra("zy"); ? ? ? ? String pricj = priIntent.getStringExtra("cj"); ? ? ? ? et_xm.setText(prixm); ? ? ? ? et_xh.setText(prixh); ? ? ? ? et_bj.setText(pribj); ? ? ? ? et_zy.setText(prizy); ? ? ? ? et_cj.setText(pricj); ? ? ? ? ? Button btn_confirm = findViewById(R.id.btn_confirm); ? ? ? ? ? btn_confirm.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View v) { ? ? ? ? ? ? ? ? EditText et_xm = findViewById(R.id.et_xm); ? ? ? ? ? ? ? ? EditText et_xh = findViewById(R.id.et_xh); ? ? ? ? ? ? ? ? EditText et_bj = findViewById(R.id.et_bj); ? ? ? ? ? ? ? ? EditText et_zy = findViewById(R.id.et_zy); ? ? ? ? ? ? ? ? EditText et_cj = findViewById(R.id.et_cj); ? ? ? ? ? ? ? ? ? String xm = et_xm.getText().toString(); ? ? ? ? ? ? ? ? String xh = et_xh.getText().toString(); ? ? ? ? ? ? ? ? String bj = et_bj.getText().toString(); ? ? ? ? ? ? ? ? String zy = et_zy.getText().toString(); ? ? ? ? ? ? ? ? String cj = et_cj.getText().toString(); ? ? ? ? ? ? ? ? if (TextUtils.isEmpty(xm)||TextUtils.isEmpty(xh)){ ? ? ? ? ? ? ? ? ? ? Toast.makeText(NewStudent.this,"學號或者姓名不可為空",Toast.LENGTH_SHORT).show(); ? ? ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? Intent intent = new Intent(); ? ? ? ? ? ? ? ? intent.putExtra("_id",priId); ? ? ? ? ? ? ? ? intent.putExtra("xm",xm); ? ? ? ? ? ? ? ? intent.putExtra("xh",xh); ? ? ? ? ? ? ? ? intent.putExtra("bj",bj); ? ? ? ? ? ? ? ? intent.putExtra("zy",zy); ? ? ? ? ? ? ? ? intent.putExtra("cj",cj); ? ? ? ? ? ? ? ? setResult(RESULT_OK,intent); ? ? ? ? ? ? ? ? finish(); ? ? ? ? ? ? } ? ? ? ? }); ? ? } }
activity_new_student.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:app="http://schemas.android.com/apk/res-auto" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? android:layout_width="match_parent" ? ? android:orientation="vertical" ? ? android:layout_height="match_parent" ? ? tools:context=".NewStudent"> ? ? <EditText ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:id="@+id/et_xm" ? ? ? ? android:hint="請輸入姓名" ? ? ? ? /> ? ? <EditText ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:id="@+id/et_xh" ? ? ? ? android:hint="請輸入學號" ? ? ? ? /> ? ? <EditText ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:id="@+id/et_bj" ? ? ? ? android:hint="請輸入班級" ? ? ? ? /> ? ? <EditText ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:id="@+id/et_zy" ? ? ? ? android:hint="請輸入專業" ? ? ? ? /> ? ? <EditText ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:id="@+id/et_cj" ? ? ? ? android:hint="請輸入成績" ? ? ? ? /> ? ? ? <Button ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:id="@+id/btn_confirm" ? ? ? ? android:text="確定" ? ? ? ? /> ? </LinearLayout>
運行效果圖:
登錄頁面:
注冊界面:
管理界面:
對listview進行操作:單擊
長按
對listview的添加:
原文鏈接:https://blog.csdn.net/m0_55995292/article/details/117170301
相關推薦
- 2022-07-06 Qt之使用GraphicsView框架實現思維導圖的示例_C 語言
- 2022-04-24 .Net?Core讀取文件時中文亂碼問題的解決方法分享_實用技巧
- 2023-07-14 react封裝一個router6
- 2022-07-16 Linux中啟動Docker容器報錯:Error response from daemon: dri
- 2022-08-04 python設置Pyplot的動態rc參數、繪圖的填充_python
- 2022-08-05 記一次使用EasyExcel出現Convert excel format exception.You
- 2024-02-17 序列轉序模型及其pytorch實現
- 2022-03-13 在Linux系統中安裝Docker的過程_docker
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支