AndriodStudio利用ListView和數(shù)據(jù)庫實(shí)現(xiàn)簡單學(xué)生管理
本文實(shí)例為大家分享了AndriodStudio利用ListView和數(shù)據(jù)庫實(shí)現(xiàn)簡單學(xué)生管理的具體代碼,供大家參考,具體內(nèi)容如下
數(shù)據(jù)庫的創(chuàng)建
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)";
//創(chuàng)建兩張表,一張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);
//完成創(chuàng)建
? ? }
// 對表進(jìn)行數(shù)據(jù)的插入方法
? ? public void insert(String tableName, ContentValues values){
? ? ? ? SQLiteDatabase db = getReadableDatabase();
? ? ? ? db.insert(tableName,null,values);
? ? ? ? Toast.makeText(context,"成功插入數(shù)據(jù)!",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;
? ? }
//對表中的姓名和學(xué)號(hào)進(jìn)行單獨(dú)的查詢
? ? 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;
? ? ? ? }
? ? }
//刪除表中數(shù)據(jù)的方法
? ? public void delStudent(String id){
? ? ? ? SQLiteDatabase db = getWritableDatabase();
? ? ? ? db.delete("Student","_id=?",new String[]{id});
? ? }
//對表進(jìn)行更新
? ? public void ?updateStudent(String id,ContentValues values){
? ? ? ? SQLiteDatabase db = getWritableDatabase();
? ? ? ? db.update("Student",values,"_id=?",new String[]{id});
? ? }
}創(chuàng)建了兩張表格,一張用來儲(chǔ)存學(xué)生信息,Student表:id(主鍵),姓名,學(xué)號(hào),班級(jí),專業(yè),成績。
一張register表:id(主鍵),姓名,學(xué)號(hào)。用來儲(chǔ)存登錄信息
其余類和其布局文件
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,"姓名或者學(xué)號(hào)不可為空",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="學(xué)號(hào)"/> ? ? <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,"姓名或者學(xué)號(hào)不能為空",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="輸入學(xué)號(hào)"/> ? ? <Button ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:text="確認(rèn)注冊" ? ? ? ? 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("刪除確認(rèn)").setMessage("是否確認(rèn)刪除學(xué)生"+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="添加新學(xué)生" /> ? </LinearLayout>
對應(yīng)的listview的布局文件,listview.xml
因?yàn)槭褂玫氖菙?shù)據(jù)庫來儲(chǔ)存信息和調(diào)用,并沒有重寫對于listview的Adapter,而是使用Android自帶的SimpleCursorAdapter類方法,用游標(biāo)來儲(chǔ)存,查看數(shù)據(jù)
<?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="學(xué)號(hào)" /> ? ? ? ? ? <TextView ? ? ? ? ? ? android:layout_width="0dp" ? ? ? ? ? ? android:layout_height="50dp" ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? android:text="班級(jí)" /> ? ? ? ? ? <TextView ? ? ? ? ? ? android:layout_width="0dp" ? ? ? ? ? ? android:layout_height="50dp" ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? android:text="專業(yè)" /> ? ? ? ? ? <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>
對于新添加學(xué)生操作,使用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,"學(xué)號(hào)或者姓名不可為空",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="請輸入學(xué)號(hào)" ? ? ? ? /> ? ? <EditText ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:id="@+id/et_bj" ? ? ? ? android:hint="請輸入班級(jí)" ? ? ? ? /> ? ? <EditText ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:id="@+id/et_zy" ? ? ? ? android:hint="請輸入專業(yè)" ? ? ? ? /> ? ? <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>
運(yùn)行效果圖:
登錄頁面:

注冊界面:

管理界面:

對listview進(jìn)行操作:單擊

長按

對listview的添加:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android 控件(button)對齊方法實(shí)現(xiàn)詳解
horizontal是讓所有的子元素按水平方向從左到右排列,vertical是讓所有的子元素按豎直方向從上到下排列,下面為大家介紹下控件(button)的對齊方法2013-06-06
Android實(shí)現(xiàn)屏蔽微信拉黑和刪除聯(lián)系人功能示例
本篇文章主要介紹了Android實(shí)現(xiàn)屏蔽微信拉黑和刪除聯(lián)系人功能示例,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-02-02
Android RecycleView和線型布局制作聊天布局
大家好,本篇文章主要講的是Android RecycleView和線型布局制作聊天布局,感興趣的同學(xué)趕緊來看一看吧,對你有幫助的話記得收藏一下2022-01-01
Android編程設(shè)計(jì)模式之單例模式實(shí)例詳解
這篇文章主要介紹了Android編程設(shè)計(jì)模式之單例模式,結(jié)合實(shí)例形式詳細(xì)分析了Android開發(fā)設(shè)計(jì)模式中單例模式的概念、功能、實(shí)現(xiàn)、使用方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-12-12
Kotlin ViewModelProvider.Factory的使用實(shí)例詳解
這篇文章主要介紹了Kotlin ViewModelProvider.Factory的使用,在我們使用 ViewModel 的時(shí)候,我們會(huì)發(fā)現(xiàn),有的時(shí)候我們需要用到 ViewModelFactory,有的時(shí)候不需要2023-02-02
Android 自定義View時(shí)使用TypedArray配置樣式屬性詳細(xì)介紹
這篇文章主要介紹了Android 自定義View時(shí)使用TypedArray配置樣式屬性詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2016-11-11
Android 本地廣播和強(qiáng)制下線功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android 本地廣播和強(qiáng)制下線功能的實(shí)現(xiàn)代碼,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
Android開發(fā)之圖形圖像與動(dòng)畫(二)Animation實(shí)現(xiàn)圖像的漸變/縮放/位移/旋轉(zhuǎn)
Android 平臺(tái)提供了兩類動(dòng)畫,一類是Tween動(dòng)畫,就是對場景里的對象不斷的進(jìn)行圖像變化來產(chǎn)生動(dòng)畫效果;旋轉(zhuǎn)、平移、放縮和漸變等等,感興趣的朋友可以了解下啊,希望本文對你有所幫助2013-01-01
關(guān)于Fragment?already?added問題的解決方案
這篇文章主要介紹了關(guān)于Fragment?already?added問題的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10

