5月15日SQL保存姓名信息
完成下列内容:
(1)将学生信息存入数据库 (2)显示所有学生信息列表 (3)删除数据库表中的第一条信息
首先我们应该有两个界面,一个界面中有输入框,按钮,和ListView显示,另一个则是这个Listview的item显示
主界面
<LinearLayout
android:layout_weight="0.1"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="0dp">
<EditText
android:id="@+id/writer"
android:layout_width="384dp"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="0.1"
android:orientation="horizontal">
<Button
android:id="@+id/add"
android:layout_width="196dp"
android:layout_height="wrap_content"
android:text="@string/add"
android:onClick="onClick"/>
<Button
android:id="@+id/delte"
android:layout_width="196dp"
android:layout_height="wrap_content"
android:text="@string/delte"
android:onClick="onClick"/>
</LinearLayout>
<LinearLayout
android:layout_weight="0.8"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="0dp">
<ListView
android:choiceMode="singleChoice"
android:listSelector="#ffaa00"
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_weight="0.8">
</ListView>
</LinearLayout>
ListView的item界面来
-<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="20sp"
android:text="@string/name"
android:id="@+id/name"/>
</LinearLayout>
效果为:
java代码中实现的功能,将在代码中以注解的形式表现
步骤一:打开或创建数据库
public class DatabaseHelpter extends SQLiteOpenHelper {
//创建表的SQL语句
private String sql = "create table student(_id integer primary key autoincrement,"
+ "name text not null)";
public DatabaseHelpter(Context context ) {
super(context, "studentInfo", null,1);
}
@Override
public void onCreate(SQLiteDatabase db) {
//执行语句
db.execSQL(sql);
}
//升级数据库表
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists student ");
onCreate(db);
}
}
步骤二:将相关信息进行封装
public class Student implements Serializable {
private int _id;
private String name;
public Student() {
}
public Student(String name) {
this.name = name;
}
public int get_id() {
return _id;
}
public void set_id(int _id) {
this._id = _id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
步骤三:加载Listview的布局并完成给每个控件赋值
public class CursorAD extends CursorAdapter {
public CursorAD(Context context, Cursor cursor) {
super(context, cursor, 0);
}
//加载ListView的item的布局
@Override
public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
return LayoutInflater.from(context).inflate(R.layout.item,viewGroup,false);
}
//给item的每个组件赋值
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView name = (TextView) view.findViewById(R.id.name);
name.setText(cursor.getString(cursor.getColumnIndex("name")));
}
}
*步骤四:插入数据
public class StudentDAO {
//对数据库进行增删改查
private Context context;
private DatabaseHelpter helper;
private SQLiteDatabase db;
public StudentDAO(Context context) {
helper = new DatabaseHelpter(context);
}
public void insert(Student student) {
//打开数据库
db = helper.getWritableDatabase();
//生成数据集合
ContentValues values = new ContentValues();
values.put("name", student.getName());
//执行语句
db.insert("student", null, values);
}
//增加数据时的selectAll()方法,执行时可以查询数据
public Cursor selectAll() {
db =helper.getReadableDatabase();
Cursor cursor= db.query("student",null,null,null,null,null,null);
return cursor;
}
//删除时的delete()方法,这两个不用刻意写,顺其自然的调用即可
public void delete(int id) {
db = helper.getWritableDatabase();
db.delete("student","_id=?",new String[]{String.valueOf(id)});
}
}
mainActivity实现功能
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ListView listview;
private Button add;
private Button delte;
private StudentDAO studentDAO;
private Cursor cursor;
private CursorAD adapter;
private EditText writer;
private Student student;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
writer = (EditText) findViewById(R.id.writer);
listview = (ListView) findViewById(R.id.list);
add = (Button) findViewById(R.id.add);
add.setOnClickListener(this);
delte = (Button) findViewById(R.id.delte);
delte.setOnClickListener(this);
studentDAO = new StudentDAO(this);
cursor = studentDAO.selectAll();
if (cursor != null) {
adapter = new CursorAD(this, cursor);
listview.setAdapter(adapter);
}
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//拿数据 强制类型转换
Cursor cursor = (Cursor) adapterView.getItemAtPosition(i);
if(cursor != null){
cursor.moveToFirst();
student = new Student();
student.set_id(cursor.getInt(cursor.getColumnIndex("_id")));
}
}
});
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.add:
String name = writer.getText().toString();
if (TextUtils.isEmpty(name)) {
Toast.makeText(this, "姓名不能为空", Toast.LENGTH_LONG).show();
return;
}
//组装对象
student = new Student(name);
//将这些数据存储到数据库
studentDAO=new StudentDAO(this);
//添加数据
studentDAO.insert(student);
//将列表上的数据进行更新
adapter.changeCursor(cursor);
listview.setAdapter(adapter);
//显示
Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
break;
case R.id.delte:
if(student != null){
studentDAO.delete(student.get_id());
studentDAO=new StudentDAO(this);
cursor = studentDAO.selectAll();
adapter = new CursorAD(this, cursor);
adapter.changeCursor(cursor);
listview.setAdapter(adapter);
Toast.makeText(this,"删除成功",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(this,"请选择第一条记录",Toast.LENGTH_SHORT).show();
break;
}
}
}
}
效果为:
作业中的题目比上课中老师讲的相对要简单一点,如果我们能真正的搞清楚老师上课讲的东西,这些对我们来说并不算什么,但是现实中我觉得自己还没有到那个程度,需要下更大的努力在这方面,我们每一个人
也需要好好的投入到这里,融入他,每当做作业的时候,才知道Android其实还不认识我,而我也不了解他。