1.建数据库,数据库适配器,写增删改查方法:
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
public class MySQLiteOpenHelper extends SQLiteOpenHelper {
public MySQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.d(“msg”,“创建数据库”);
String createStr = “create table student (id integer primary key autoincrement not null,” +
“name varchar not null,” +
“tell varchar not null,” +
“address varchar not null)”;
db.execSQL(createStr);
//插入学生信息
public void insert(String tableNm, Student student) {
SQLiteDatabase sql = getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", student.getName());
values.put("tell", student.getTell());
values.put("address", student.getAddress() );
sql.insert(tableNm, null, values);
}
public void delete(String tableNm, int id) {
SQLiteDatabase sql = getWritableDatabase();
String where = "id=?";
sql.delete(tableNm, where, new String[]{id + ""});
}
public List selectAll(String tableNm) {
List students = new ArrayList<>();
SQLiteDatabase database = getReadableDatabase();
String selectstr = “select * from " + tableNm;
Cursor cursor = database.rawQuery(selectstr, null);
// Cursor cursor=database.query(tableNm,new String[]{“id”,“name”,“age”,“sex”},null,null,null,null,null);
cursor.moveToFirst();
do {
int id = cursor.getInt(cursor.getColumnIndex(“id”));
String name = cursor.getString(cursor.getColumnIndex(“name”));
String tell = cursor.getString(cursor.getColumnIndex(“tell”));
String address = cursor.getString(cursor.getColumnIndex(“address”));
students.add(new Student(id, name, tell, address));
}
while (cursor.moveToNext());
return students;
}
public void update(String tableNm,ContentValues values,int id)
{
SQLiteDatabase database = getWritableDatabase();
// String where = “id = ?”;
// database.update(tableNm,values,where,new String[]{id+”"});
// UPDATE newTable SET id = 0, name = ‘’, age = 0, sex = ‘’ WHERE id = ;
String sql = “update “+tableNm+” set name=?,tell=?,address=? where id = ?”;
database.execSQL(sql,new String[]{values.get(“name”).toString(),values.get(“tell”).toString(),values.get(“address”).toString(),id+""})
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
2.写参数,建bean类
public class Student {
private int id;
private String name;
private String tell;
private String address;
public Student() {
}
public Student(String name, String tell, String address) {
this.name = name;
this.tell = tell;
this.address = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTell() {
return tell;
}
public void setTell(String tell) {
this.tell = tell;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", tell='" + tell + '\'' +
", address='" + address + '\'' +
'}';
}
public Student(int id, String name, String tell, String address) {
this.id = id;
this.name = name;
this.tell = tell;
this.address = address;
}
}
3. mainactivity中的代码
import android.content.ContentValues;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
import java.util.List;
public class MainActivity extends AppCompatActivity {
ListView lv;
private MySQLiteOpenHelper helper;
static String student_table = “student”;
Button ad_del,ad_xiu,main_add;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//数据库表名 ,版本号
helper = new MySQLiteOpenHelper(getApplicationContext(), “studentdb”, null, 1);
lv=findViewById(R.id.lv);
ad_xiu=findViewById(R.id.ad_xiu);
ad_del=findViewById(R.id.ad_del);
main_add=findViewById(R.id.main_add);
//查询全部并显示到listview中
List SJ=helper.selectAll(student_table);
lv.setAdapter(new lvAdapter(SJ));
//点击跳转
main_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,ADDActivity.class);
startActivity(intent);
}
});
}
//listview适配器
class lvAdapter extends BaseAdapter {
private List students;
//传参类型
public lvAdapter(List students) {
this.students = students;
}
@Override
public int getCount() {
return students.size();
}
@Override
public Object getItem(int position) {
return students.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
//把查询数据填到listview 的item中
view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.lv_layout, null);
Student student = students.get(position);
TextView txt = view.findViewById(R.id.ad_name);
txt.setText(student.getName());
txt = view.findViewById(R.id.ad_tell);
txt.setText(student.getTell());
txt = view.findViewById(R.id.ad_adress);
txt.setText(student.getAddress() + "");
} else {
view = convertView;
}
return view;
}
}
}
*4.main布局:
5.跳转界面:
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;
import android.widget.RadioButton;
import android.widget.TextView;
public class ADDActivity extends AppCompatActivity {
EditText add_name,add_tel,add_adress;
private MySQLiteOpenHelper helper;
static String student_table = “student”;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
helper = new MySQLiteOpenHelper(getApplicationContext(), “studentdb”, null, 1);
add_name=findViewById(R.id.add_name);
add_tel=findViewById(R.id.add_tel);
add_adress=findViewById(R.id.add_address);
Button btn=findViewById(R.id.add_qd);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = add_name.getText().toString();
String tel=add_tel.getText().toString();
String address=add_adress.getText().toString();
helper.insert(“student”, new Student(name, tel, address));
Intent intent=new Intent(ADDActivity.this,MainActivity.class);
startActivity(intent);
}
});
}
}
6.跳转界面布局:
<?xml version="1.0" encoding="utf-8"?>
listview布局
<?xml version="1.0" encoding="utf-8"?><TextView
android:id="@+id/ad_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="邱国忠"
android:textSize="25dp"
/>
<TextView
android:id="@+id/ad_tell"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/ad_name"
android:text="15770836531"
android:textSize="22dp"
android:layout_margin="2dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="学校"
android:layout_toRightOf="@id/ad_tell"
android:layout_marginRight="3dp"
android:layout_marginTop="5dp"
/>
<TextView
android:id="@+id/ad_adress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="红红火火恍恍惚惚哈哈"
android:textSize="20dp"
android:layout_below="@id/ad_name"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="设为默认"
android:layout_below="@id/ad_adress"/>
<Button
android:id="@+id/ad_xiu"
android:layout_width="55dp"
android:layout_height="35dp"
android:text="编辑"
android:layout_toLeftOf="@id/ad_del"
android:layout_below="@id/ad_adress"
/>
<Button
android:id="@+id/ad_del"
android:layout_width="55dp"
android:layout_height="35dp"
android:text="删除"
android:layout_alignParentRight="true"
android:layout_below="@id/ad_adress"
/>