activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<include layout="@layout/content_main" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/activity_main">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_name"
android:hint="请填写姓名"
android:textColor="@color/colorAccent"
android:textSize="25sp"
android:drawablePadding="10dp"
android:drawableLeft="@drawable/name"
/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_id"
android:hint="请填写学号"
android:textSize="25sp"
android:drawablePadding="10dp"
android:textColor="@color/colorAccent"
android:drawableLeft="@drawable/number"
android:numeric="integer"
/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_age"
android:hint="请填写年龄"
android:textSize="25sp"
android:drawablePadding="10dp"
android:textColor="@color/colorAccent"
android:drawableLeft="@drawable/age"
android:numeric="integer"
/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_class"
android:hint="请填写课程"
android:textSize="25sp"
android:drawablePadding="10dp"
android:textColor="@color/colorAccent"
android:drawableLeft="@drawable/myclass"
/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.button.MaterialButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="保存"
android:textSize="20sp"
android:id="@+id/btn_save"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:textAppearance="?android:attr/textAppearance"
android:theme="@style/Theme.MaterialComponents.Light"
app:backgroundTint="@color/colorAccent"
app:cornerRadius="6dp"
android:elevation="20dp"
/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lv_list"
android:layout_marginTop="5dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
/>
</LinearLayout>
item_layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="20dp"
android:layout_height="wrap_content"
android:id="@+id/item_num"
android:textSize="25sp"
/>
<TextView
android:layout_width="90dp"
android:layout_height="wrap_content"
android:id="@+id/item_name"
android:textSize="25sp"
android:textColor="@color/colorAccent"
/>
<TextView
android:layout_width="40dp"
android:layout_height="wrap_content"
android:id="@+id/item_age"
android:textSize="20sp"
android:textColor="@color/colorPrimary"
/>
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="@+id/item_number"
android:textSize="20sp"
android:textColor="@color/colorPrimary"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/item_class"
android:textSize="20sp"
android:textColor="@color/colorAccent"
/>
</LinearLayout>
menu_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.sgxy_ict.threadsqlite.MainActivity">
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never" />
</menu>
MainActivity.java
package com.sgxy_ict.threadsqlite;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
EditText ET_Name,ET_Age,ET_Number,ET_Class;
MaterialButton BTN_Save;
ListView LV;
MySQLiteDB mySQLiteDB;
SQLiteDatabase sqlDB;
Cursor cur;
int cur_position;
String Str_Name,Str_Age,Str_Number,Str_Class;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
find_id();
Key_things();
mySQLiteDB = new MySQLiteDB(MainActivity.this,"mydb",null,1);
sqlDB = mySQLiteDB.getWritableDatabase();
showSQL();
}
private void Key_things() {
BTN_Save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Str_Name = ET_Name.getText().toString();
Str_Age = ET_Age.getText().toString();
Str_Number = ET_Number.getText().toString();
Str_Class = ET_Class.getText().toString();
sqlDB.execSQL("insert into people (name,age,number,class_name) values (?,?,?,?)",
new String[]{Str_Name,Str_Age,Str_Number,Str_Class});
Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
showSQL();
}
});
}
private void find_id() {
ET_Name = findViewById(R.id.et_name);
ET_Age = findViewById(R.id.et_age);
ET_Number = findViewById(R.id.et_id);
ET_Class = findViewById(R.id.et_class);
BTN_Save = findViewById(R.id.btn_save);
LV = findViewById(R.id.lv_list);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void showSQL() {
cur = sqlDB.query("people",new String[]{"id","name","age","number","class_name"},"",null,null,null,null);
ArrayList<HashMap<String,Object>> mylist = new ArrayList<HashMap<String, Object>>();
while (cur.moveToNext()){
HashMap<String,Object> hm = new HashMap<String, Object>();
hm.put("K_id",cur.getInt(0));
hm.put("K_name",cur.getString(1));
hm.put("K_age",cur.getInt(2));
hm.put("K_number",cur.getInt(3));
hm.put("K_class",cur.getString(4));
mylist.add(hm);
}
SimpleAdapter sqladapter = new SimpleAdapter(
MainActivity.this,
mylist,
R.layout.item_layout,
new String[]{"K_id","K_name","K_age","K_number","K_class"},
new int[]{R.id.item_num,R.id.item_name,R.id.item_age,R.id.item_number,R.id.item_class});
LV.setAdapter(sqladapter);
}
}
MySQLiteDB.java
package com.sgxy_ict.threadsqlite;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
public class MySQLiteDB extends SQLiteOpenHelper {
public MySQLiteDB(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
String SQL_Create_Table ="create table people (id integer primary key,name text,age integer,number integer,class_name text)";
db.execSQL(SQL_Create_Table);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}