Android studio写的一个简单的数据库增删改查demo。便于初步理解数据库操作。
1.实现效果
增
删
改
查
2.项目目录
3.具体Activity与Layout实现
1> studentdemo包,主要是Activity
AddActivity.java
package com.example.rex.studentdemo;
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 com.example.rex.student.Student;
import com.example.rex.student.StudentDao;
public class AddActivity extends AppCompatActivity {
private Button resetButton, okButton;
private EditText nameEditText, ageEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
initView();
onButtonClick();
}
private void initView()
{
resetButton = (Button) findViewById(R.id.add_reset_btn);
okButton = (Button) findViewById(R.id.add_ok_btn);
nameEditText = (EditText) findViewById(R.id.edit_text_name);
ageEditText = (EditText) findViewById(R.id.edit_text_age);
}
private void onButtonClick()
{
resetButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//有可能出问题,没有出问题...
nameEditText.setText("");
ageEditText.setText("");
}
});
okButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(nameEditText.getText() != null && ageEditText.getText() != null)
{
String name = nameEditText.getText() + "";
int age = Integer.parseInt(ageEditText.getText() + "");
Student student = new Student(name, age);
StudentDao.insertData(AddActivity.this, student);
}
Intent i = new Intent(AddActivity.this, MainActivity.class);
startActivity(i);
}
});
}
}
对应布局文件activity_add.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名:"/>
<EditText
android:id="@+id/edit_text_name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="年龄:"/>
<EditText
android:id="@+id/edit_text_age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLength="2"
android:inputType="number"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/add_reset_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="重置"/>
<Button
android:id="@+id/add_ok_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="保存"/>
</LinearLayout>
</LinearLayout>
MainActivity.java
package com.example.rex.studentdemo;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import com.example.rex.student.Student;
import com.example.rex.student.StudentAdapter;
import com.example.rex.student.StudentDao;
import com.example.rex.utils.DbManager;
import com.example.rex.utils.MySqliteHelper;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private MySqliteHelper helper;
private Button searchButton;
private Button addButton;
private ListView listView;
private List<Student> list = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
helper = DbManager.getHelperInstance(this);
helper.getWritableDatabase();
initView();
onButtonClick();
showListView();
onItemClick();
}
//初始化按钮、ListView 控件
private void initView()
{
searchButton = (Button) findViewById(R.id.search_btn);
addButton = (Button) findViewById(R.id.add_btn);
listView = (ListView) findViewById(R.id.list_view);
}
/**
* 将ListView对象显示出来
*/
private void showListView()
{
list = StudentDao.selectAll(MainActivity.this);
StudentAdapter adapter = new StudentAdapter(MainActivity.this, R.layout.student_item, list);
listView.setAdapter(adapter);
}
private void onItemClick(