前段时间准备系统学习一下Android的知识点,结果学了部分之后天天加班,搁置了有两个月了,中间有些知识点也没有发文记录。昨天学习了数据库的操作,最基础的增删改查功能,由于昨天跟着视频敲的时候有些困,最后的布局没有完善。记录一下所有的代码:
activity_main.xml内容:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="60dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20dp"
android:text="账号:">
</TextView>
<EditText
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textSize="20dp"
android:hint="请输入"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20dp"
android:text="密码:">
</TextView>
<EditText
android:id="@+id/psw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textSize="20dp"
android:hint="请输入"/>
</LinearLayout>
<Button
android:id="@+id/add"
android:layout_marginTop="40dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="添加"/>
<Button
android:id="@+id/del"
android:layout_marginTop="40dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="删除"/>
<Button
android:id="@+id/change"
android:layout_marginTop="40dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="修改"/>
<Button
android:id="@+id/seeAll"
android:layout_marginTop="40dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="查询"/>
<ScrollView
android:layout_width="match_parent"
android:layout_marginTop="40dp"
android:layout_height="150dp">
<TextView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</ScrollView>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java内容:
package com.jasmyn.database;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
EditText name,psw;
Button add,del,change,seeAll;
TextView content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = findViewById(R.id.name);
psw = findViewById(R.id.psw);
add = findViewById(R.id.add);
del = findViewById(R.id.del);
change = findViewById(R.id.change);
seeAll = findViewById(R.id.seeAll);
content = findViewById(R.id.content);
final DB db = new DB(this);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String n = name.getText().toString();
String p = psw.getText().toString();
if (db.add(n,p)) {
Toast.makeText(MainActivity.this,"添加成功",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this,"添加失败",Toast.LENGTH_SHORT).show();
}
}
});
del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String n = name.getText().toString();
String p = psw.getText().toString();
if (db.del(n)) {
Toast.makeText(MainActivity.this,"删除成功",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this,"删除失败",Toast.LENGTH_SHORT).show();
}
}
});
change.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String n = name.getText().toString();
String p = psw.getText().toString();
if (db.change(n,p)) {
Toast.makeText(MainActivity.this,"修改成功",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this,"修改失败",Toast.LENGTH_SHORT).show();
}
}
});
seeAll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ArrayList a = db.getAll();
for (Object object:
a) {
String n = content.getText().toString();
String str =n + ((User)object).toString()+"\n";
content.setText(str);
}
}
});
}
}
DB.java内容:
package com.jasmyn.database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import androidx.annotation.Nullable;
import java.util.ArrayList;
public class DB extends SQLiteOpenHelper {
public DB(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
private SQLiteDatabase dp;
public DB(Context context) {
super(context,"TEST1.dp",null,1);
dp = this.getWritableDatabase();
}
public boolean add(String name,String psw) {
ContentValues values = new ContentValues();
values.put("name",name);
values.put("psw",psw);
long i = dp.insert("users",null,values);
if (i > 0) {
Log.d("","插入成功");
return true;
}
return false;
}
public boolean del(String name) {
long i = dp.delete("users","name=?",new String[]{name});
if (i > 0) {
Log.d("","删除成功");
return true;
}
return false;
}
public boolean change(String name,String NewPsw) {
ContentValues values = new ContentValues();
values.put("psw",NewPsw);
long i = dp.update("users",values,"name=?",new String[]{name});
if (i > 0) {
Log.d("","修改成功");
return true;
}
return false;
}
public ArrayList getAll() {
ArrayList arrayList = new ArrayList();
Cursor cursor = dp.query("users",null,null,null,null,null,null);
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex("name"));
String psw = cursor.getString(cursor.getColumnIndex("psw"));
User u = new User(name,psw);
arrayList.add(u);
}
return arrayList;
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table users(name text primary key,psw text not null)";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
User.java内容:
package com.jasmyn.database;
public class User {
String name;
String psw;
public User(String name, String psw) {
this.name = name;
this.psw = psw;
}
@Override
public String toString() {
return name + "," + psw;
}
}
后面实例写完了再继续更新。