安卓的简单的课程可以增删改查和数据库
时间: 2024-05-03 15:18:01 浏览: 90
QML SQLite数据库编程
安卓的课程可以分为以下几个部分:
1. UI设计:使用XML语言进行布局和设计,包括按钮、文本框、列表等控件的添加和使用。
2. 增删改查:使用Java语言编写相关代码,实现数据的增加、删除、修改和查询等功能。
3. 数据库:使用SQLite数据库进行数据的存储和管理,包括表的创建、数据的插入和查询等操作。
下面是一个简单的增删改查的例子:
1. 布局文件:
```xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="姓名"/>
<EditText
android:id="@+id/et_age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="年龄"/>
<Button
android:id="@+id/btn_save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="保存"/>
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
```
2. Java代码:
```java
public class MainActivity extends AppCompatActivity {
private EditText etName, etAge;
private Button btnSave;
private ListView listView;
private ArrayList<Person> persons;
private PersonAdapter adapter;
private DBHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etName = findViewById(R.id.et_name);
etAge = findViewById(R.id.et_age);
btnSave = findViewById(R.id.btn_save);
listView = findViewById(R.id.list_view);
dbHelper = new DBHelper(this);
persons = dbHelper.getAllPersons();
adapter = new PersonAdapter(this, persons);
listView.setAdapter(adapter);
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = etName.getText().toString();
int age = Integer.parseInt(etAge.getText().toString());
Person person = new Person(name, age);
dbHelper.addPerson(person);
persons.add(person);
adapter.notifyDataSetChanged();
}
});
}
class PersonAdapter extends ArrayAdapter<Person> {
public PersonAdapter(Context context, ArrayList<Person> persons) {
super(context, 0, persons);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item_person, parent, false);
}
TextView tvName = convertView.findViewById(R.id.tv_name);
TextView tvAge = convertView.findViewById(R.id.tv_age);
Person person = getItem(position);
tvName.setText(person.getName());
tvAge.setText(String.valueOf(person.getAge()));
return convertView;
}
}
}
```
3. 数据库操作:
```java
public class DBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "mydb.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "persons";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_NAME = "name";
private static final String COLUMN_AGE = "age";
private static final String SQL_CREATE_TABLE = "CREATE TABLE " + TABLE_NAME +
" (" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
COLUMN_NAME + " TEXT," +
COLUMN_AGE + " INTEGER)";
private static final String SQL_DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(SQL_DROP_TABLE);
onCreate(db);
}
public void addPerson(Person person) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, person.getName());
values.put(COLUMN_AGE, person.getAge());
db.insert(TABLE_NAME, null, values);
db.close();
}
public ArrayList<Person> getAllPersons() {
ArrayList<Person> persons = new ArrayList<>();
SQLiteDatabase db = getReadableDatabase();
String[] columns = {COLUMN_ID, COLUMN_NAME, COLUMN_AGE};
Cursor cursor = db.query(TABLE_NAME, columns, null, null, null, null, null);
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex(COLUMN_ID));
String name = cursor.getString(cursor.getColumnIndex(COLUMN_NAME));
int age = cursor.getInt(cursor.getColumnIndex(COLUMN_AGE));
Person person = new Person(id, name, age);
persons.add(person);
}
cursor.close();
db.close();
return persons;
}
}
```
其中,Person类的定义如下:
```java
public class Person {
private int id;
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public Person(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
```
list_item_person.xml的定义如下:
```xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"/>
<TextView
android:id="@+id/tv_age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"/>
</LinearLayout>
```
以上代码实现了一个简单的增删改查功能,包括UI设计、Java代码和数据库操作。具体实现细节可以参考代码中的注释。
阅读全文