AddActivity.java
package com.example.administrator.myapplication;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Addactivity extends AppCompatActivity {
private DBAdapter db;
private Student student;
private Button bt_cancel;
private Button bt_ensure;
private EditText et_cl;
private EditText et_num;
private EditText et_na;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
bt_cancel = (Button)findViewById(R.id.bt_cancel);
bt_ensure =(Button) findViewById(R.id.bt_ensure);
et_cl = (EditText)findViewById(R.id.et_cl);
et_num = (EditText)findViewById(R.id.et_num);
et_na = (EditText)findViewById(R.id.et_na);
db = new DBAdapter(this);
db.open();
bt_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Addactivity.this,MainActivity.class);
startActivity(intent);
finish();
}
});
bt_ensure.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
String cl = et_cl.getText().toString();
String num = et_num.getText().toString();
String na = et_na.getText().toString();
student = new Student(cl,num,na);
long colunm= db.insert(student);
//db.close();
Intent intent = new Intent(Addactivity.this,MainActivity.class);
startActivity(intent);
db.close();
finish();
}
});
}
}
Student.java
package com.example.administrator.myapplication;
public class Student {
public int ID = -1;
public String Name;
public String Number;
public String Clas;
public Student(String Class, String Number, String Name) {
this.Clas = Class;
this.Number = Number;
this.Name = Name;
}
public Student() {
super();
}
public String getClas() {
return Clas;
}
public String getName() {
return Name;
}
public String getNumber() {
return Number;
}
public int getID() {
return ID;
}
@Override
public String toString() {
String result = "";
result += this.ID + " ";
result += this.Clas + " ";
result += this.Number + " ";
result += this.Name + " ";
return result;
}
}
MainActivity.java
package com.example.administrator.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private DBAdapter db;
private Button bt_add;
private ListView listview;
private SimpleAdapter simpleAdapter;
Map item = new HashMap();
private List
DBAdapter .java
package com.example.administrator.myapplication;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
public class DBAdapter {
private static final String DB_NAME = "student.db";
private static final String DB_TABLE = "studentinfo";
private static final int DB_VERSION = 1;
public static final String KEY_ID = "_id";
public static final String KEY_NAME = "name";
public static final String KEY_CLASS = "class";
public static final String KEY_NUMBER = "number";
private SQLiteDatabase db;
private final Context context;
private DBOpenHelper dbOpenHelper;
public DBAdapter(Context _context) {
context = _context;
}
/** Close the database */
public void close() {
if (db != null){
db.close();
db = null;
}
}
/** Open the database */
public void open() throws SQLiteException {
dbOpenHelper = new DBOpenHelper(context, DB_NAME, null, DB_VERSION);
try {
db = dbOpenHelper.getWritableDatabase();
}
catch (SQLiteException ex) {
db = dbOpenHelper.getReadableDatabase();
}
}
public long insert(Student student) {
ContentValues newValues = new ContentValues();
newValues.put(KEY_NAME, student.Name);
newValues.put(KEY_CLASS, student.Clas);
newValues.put(KEY_NUMBER, student.Number);
return db.insert(DB_TABLE, null, newValues);
}
public Student[] queryAllData() {
Cursor results = db.query(DB_TABLE, new String[] { KEY_ID, KEY_NAME, KEY_CLASS, KEY_NUMBER}, null, null, null, null, null);
return ConvertToStudent(results);
}
public Student[] queryOneData(long id) {
Cursor results = db.query(DB_TABLE, new String[] { KEY_ID, KEY_NAME, KEY_CLASS, KEY_NUMBER}, KEY_ID + "=" + id, null, null, null, null);
return ConvertToStudent(results);
}
private Student[] ConvertToStudent(Cursor cursor){
int resultCounts = cursor.getCount();
if (resultCounts == 0 || !cursor.moveToFirst()){
return null;
}
Student[] students = new Student[resultCounts];
for (int i = 0 ; i
students[i] = new Student();
students[i].ID = cursor.getInt(0);
students[i].Name = cursor.getString(cursor.getColumnIndex(KEY_NAME));
students[i].Clas = cursor.getString(cursor.getColumnIndex(KEY_CLASS));
students[i].Number = cursor.getString(cursor.getColumnIndex(KEY_NUMBER));
cursor.moveToNext();
}
return students;
}
public long deleteAllData() {
return db.delete(DB_TABLE, null, null);
}
public long deleteOneData(int id) {
return db.delete(DB_TABLE, KEY_ID + "=" + id, null);
}
public long updateOneData(long id , Student student){
ContentValues updateValues = new ContentValues();
updateValues.put(KEY_NAME, student.Name);
updateValues.put(KEY_CLASS, student.Clas);
updateValues.put(KEY_NUMBER, student.Number);
return db.update(DB_TABLE, updateValues, KEY_ID + "=" + id, null);
}
private static class DBOpenHelper extends SQLiteOpenHelper {
public DBOpenHelper(Context context, String name, CursorFactory factory, int version) {
super(context, name, factory, version);
}
private static final String DB_CREATE = "create table " + DB_TABLE + " (" + KEY_ID + " integer primary key autoincrement, " + KEY_NAME+ " text not null, " + KEY_CLASS+ " text not null," + KEY_NUMBER + " text not null);";
@Override
public void onCreate(SQLiteDatabase _db) {
_db.execSQL(DB_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion) {
_db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE);
onCreate(_db);
}
}
}