用安卓写一个课程表具有增删改查和连接数据库的具体代码
时间: 2024-05-16 22:16:18 浏览: 97
以下是用安卓编写一个简单的课程表,实现增删改查和连接数据库的代码:
1. 首先,在 layout 文件夹下创建一个 activity_main.xml 文件,用于显示主界面的布局:
```xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 显示课程列表的 ListView -->
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- 添加课程按钮 -->
<Button
android:id="@+id/button_add"
android:text="添加课程"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
```
2. 在 MainActivity 类中,定义一个 DatabaseHelper 类,用于连接数据库并实现增删改查操作:
```java
public class MainActivity extends AppCompatActivity {
// 数据库名称和版本
private static final String DATABASE_NAME = "schedule.db";
private static final int DATABASE_VERSION = 1;
// 表名和列名
private static final String TABLE_NAME = "schedule";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_NAME = "name";
private static final String COLUMN_DAY = "day";
private static final String COLUMN_TIME = "time";
// 定义数据库帮助类
private static class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// 创建表格
String sql = "CREATE TABLE " + TABLE_NAME + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY," +
COLUMN_NAME + " TEXT," +
COLUMN_DAY + " TEXT," +
COLUMN_TIME + " TEXT" + ")";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// 升级数据库版本,删除旧表格并重新创建新表格
String sql = "DROP TABLE IF EXISTS " + TABLE_NAME;
db.execSQL(sql);
onCreate(db);
}
}
private SQLiteDatabase db;
private Cursor cursor;
private SimpleCursorAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 连接数据库
DatabaseHelper dbHelper = new DatabaseHelper(this);
db = dbHelper.getWritableDatabase();
// 查询数据并显示在 ListView 中
cursor = db.query(TABLE_NAME, null, null, null, null, null, null);
String[] from = {COLUMN_NAME, COLUMN_DAY, COLUMN_TIME};
int[] to = {R.id.text_name, R.id.text_day, R.id.text_time};
adapter = new SimpleCursorAdapter(this, R.layout.list_item, cursor, from, to, 0);
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
// 添加课程按钮点击事件
Button buttonAdd = (Button) findViewById(R.id.button_add);
buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 显示添加课程对话框
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog_add, null);
builder.setView(view);
final EditText editTextName = (EditText) view.findViewById(R.id.editText_name);
final EditText editTextDay = (EditText) view.findViewById(R.id.editText_day);
final EditText editTextTime = (EditText) view.findViewById(R.id.editText_time);
builder.setPositiveButton("添加", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 将新课程插入到数据库中
String name = editTextName.getText().toString();
String day = editTextDay.getText().toString();
String time = editTextTime.getText().toString();
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, name);
values.put(COLUMN_DAY, day);
values.put(COLUMN_TIME, time);
db.insert(TABLE_NAME, null, values);
// 刷新 ListView 中的数据
cursor = db.query(TABLE_NAME, null, null, null, null, null, null);
adapter.changeCursor(cursor);
}
});
builder.setNegativeButton("取消", null);
builder.show();
}
});
// ListView 中的课程项长按事件,显示删除对话框
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("确认删除?");
builder.setPositiveButton("删除", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 删除课程
cursor.moveToPosition(position);
int rowId = cursor.getInt(cursor.getColumnIndex(COLUMN_ID));
db.delete(TABLE_NAME, COLUMN_ID + "=?", new String[] {String.valueOf(rowId)});
// 刷新 ListView 中的数据
cursor = db.query(TABLE_NAME, null, null, null, null, null, null);
adapter.changeCursor(cursor);
}
});
builder.setNegativeButton("取消", null);
builder.show();
return true;
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
// 关闭数据库和游标
db.close();
cursor.close();
}
}
```
3. 在 layout 文件夹下创建一个 list_item.xml 文件,用于显示 ListView 中的每个课程项的布局:
```xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<TextView
android:id="@+id/text_name"
android:textSize="20sp"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text_day"
android:textSize="16sp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text_time"
android:textSize="16sp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
```
4. 在 layout 文件夹下创建一个 dialog_add.xml 文件,用于显示添加课程对话框的布局:
```xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<EditText
android:id="@+id/editText_name"
android:hint="课程名称"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/editText_day"
android:hint="星期几"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/editText_time"
android:hint="上课时间"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
```
5. 在 AndroidManifest.xml 文件中添加数据库权限:
```xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
```
以上就是用安卓编写一个简单的课程表,实现增删改查和连接数据库的代码。注意,这只是一个简单的示例,实际应用中需要更加完善和安全的数据库操作方式。
阅读全文