5.16 SQL

作业内容:实现数据加入数据库,显示加入的内容,删除第一条信息
一,界面布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.administrator.sql_liuhao.MainActivity">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:id="@+id/ed_"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    <Button
          android:id="@+id/bu_zeng"
          android:text="增加一条新纪录"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_weight="1"/>
    <Button
            android:id="@+id/bu_shan"
            android:text="删除第一条记录"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
    </LinearLayout>
    <ListView
        android:id="@+id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </ListView>
</LinearLayout>

ListView还需要一个xml来显示文本,只需要写一个TextView即可id为name

二,新建CursorAdapter.java

public class CursorAdapter extends CursorAdapter {
public CursorAdapter(Context context, Cursor cursor) {
    super(context, cursor, 0);
}
@Override
    public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
    return LayoutInflater.from(context).inflate(R.layout.activity_main, viewGroup, false);
}
@Override
    public void bindView(View view, Context context, Cursor cursor) {
    TextView name = (TextView) view.findViewById(R.id.ed_);
    name.setText(cursor.getString(cursor.getColumnIndex("name")));
}
}

三,新建Student.java

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;
    }
}

四,新建DBHelper.java

public class DBHelper extends SQLiteOpenHelper {
private String sql="create table student(_id integer primary key autoincrement,"
        +"name text not null ,classmate text not null,age integer)";

public DBHelper(Context context) {
    super(context,"studentInfo",null,1);
}
@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(sql);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("drop table if exists stdent");
    onCreate(db);
}
}

五,新建StudentDAO.java

public class StudentDAO {
private Context context;
private DBHelper helper;
private SQLiteDatabase db;

public StudentDAO(Context context) {
    helper = new DBHelper(context);
}
public void insert(Student student){
    db = helper.getWritableDatabase();
    // 第一种方法
    // 2. 生成数据集合
    ContentValues values = new ContentValues();
    values.put("name", student.getName());
    // 3. 执行语句
    db.insert("student", null, values);
}
ublic Cursor selectAll() {
    db = helper.getReadableDatabase();
    Cursor cursor = db.query("student",null,null,null,null,null,null);
    return cursor;
}
public void delete(int id){
    db = helper.getWritableDatabase();
    //根据数据的id号来删除数据
    db.delete("student","_id=?",new String[]{String.valueOf(id)});
}
}

六,MainActivity.java住代码

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText ed_;
private Button bu_zengj;
private Button bu_shan;
private ListView list;
private Student student;
private CursorAdapter adapter;
private StudentDAO studentDao;
private Cursor cursor;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    studentList = (ListView) findViewById(R.id.list);
    ed_=(EditText)findViewById(R.id.ed_);
    bu_zeng = (Button) findViewById(R.id.bu_zeng);
    bu_zeng.setOnClickListener(this);
    bu_shan = (Button) findViewById(R.id.bu_shan);
    bu_shan.setOnClickListener(this);

    studentDao = new StudentDAO(this);
    if(cursor != null) {
        adapter = new CursorAdapter(this, cursor);
        studentList.setAdapter(adapter);
    }

    // ListView的item点击事件
    studentList.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.moveToPosition(i);
                student = new Student();
                student.setName(cursor.getString(cursor.getColumnIndex("name")));
            }
        }
    });
}
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.bu_zeng:
            String name = ed_.getText().toString();
            //对必填项做非空判断
            if (TextUtils.isEmpty(name)) {
                Toast.makeText(MainActivity.this, "姓名不能为空",Toast.LENGTH_SHORT).show();
                return;
            }
            //将这些数据存储到数据库
            //组装name对象
            Student student = new Student(name);
            StudentDAO studentDAO = new StudentDAO(this);
            studentDAO.insert(student);
            Cursor cursor = studentDao.selectAll();
            adapter.changeCursor(cursor);
            Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
            break;
        case R.id.bu_shan:
            if (studentList!= null) {
                cursor.moveToFirst();
                student = new Student();
                student.set_id(cursor.getInt(cursor.getColumnIndex("_id")));
                studentDao.delete(student.get_id());
                studentDao = new StudentDAO(this);
                cursor = studentDao.selectAll();
                adapter = new MyCursorAdapter(this, cursor);
                studentList.setAdapter(adapter);
                Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
            }
            Toast.makeText(MainActivity.this, "记录为空", Toast.LENGTH_SHORT).show();
            break;
    }
}
}

posted on 2017-05-16 22:35  王启明  阅读(117)  评论(0编辑  收藏  举报