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

这篇博客展示了如何使用Android Studio创建一个简单的SQLite数据库,实现学生信息的增删改查功能。通过AddActivity、MainActivity、SearchActivity等类进行交互,使用ListView展示数据,并通过Student、StudentAdapter和StudentDao等类进行数据处理。文章提供了参考资源和源码下载链接。
最低0.47元/天 解锁文章
11万+





