Попробуйте, откройте Android Studio, создайте новую пустую активность (Файл> Создать> Новый проект> Пустая активность)
Я приведу пример активности добавления, отображения и удаления с использованием sqlite в качестве базы данных
Создать DbHelper.java
package com.example.myapplication;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.HashMap;
public class DbHelper extends SQLiteOpenHelper {
ArrayList<HashMap<String, String>> YourList = new ArrayList<>();
public static final String DATABASE_NAME = "User_Database.db";
public static final String CONTACTS_TABLE_NAME = "User_info";
public static final String COLUMN_ID = "id";
public static final String COLUMN_FIRSTNAME = "first_name";
public static final String COLUMN_SURNAME = "surname";
private HashMap hp;
public DbHelper(Context context){
super(context ,DATABASE_NAME,null,1);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(
"create table User_info(id INTEGER PRIMARY KEY AUTOINCREMENT,first_name VARCHAR,surname VARCHAR)"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS User_info");
onCreate(db);
}
//Insert Data to SQLite database
public boolean insertData(String first_name, String surname) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("first_name", first_name);
contentValues.put("surname", surname);
db.insert("User_info", null, contentValues);
return true;
}
//delete all data in SQLite database
public Cursor deleteData() {
SQLiteDatabase db = this.getReadableDatabase();
db.execSQL("delete FROM User_info");
return null;
}
//counting the number of items in your database
public int numberOfRows(){
SQLiteDatabase db = this.getReadableDatabase();
int numRows = (int) DatabaseUtils.queryNumEntries(db, CONTACTS_TABLE_NAME);
return numRows;
}
//Get all item in your database
public ArrayList<HashMap<String, String>> getAllDatabaseData() {
//hp = new HashMap();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from User_info", null );
res.moveToFirst();
while(res.isAfterLast() == false){
HashMap<String, String> array_list = new HashMap<>();
array_list.put("id",res.getString(res.getColumnIndex(COLUMN_ID)));
array_list.put("first_name",res.getString(res.getColumnIndex(COLUMN_FIRSTNAME)));
array_list.put("surname",res.getString(res.getColumnIndex(COLUMN_SURNAME)));
YourList.add(array_list);
res.moveToNext();
}
return YourList;
}
}
Поместить его в MainActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
EditText etfirstname, etsurname;
ListView dataLV;
Button btnSubmit, btnDelete;
ArrayList<HashMap<String, String >>DataList;
Context context = this;
SimpleAdapter Adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etfirstname = findViewById(R.id.et_firstname);
etsurname = findViewById(R.id.et_surname);
dataLV = findViewById(R.id.data_listview);
btnSubmit = findViewById(R.id.Submit);
btnDelete = findViewById(R.id.Delete);
DataList = new ArrayList<>();
DataList = new DbHelper(context).getAllDatabaseData();
updateListView();
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String first_name = etfirstname.getText().toString();
String surname = etsurname.getText().toString();
new DbHelper(MainActivity.this).insertData(first_name, surname); //calling the method [inserData] in DbHleper.java Class
DataList = new DbHelper(context).getAllDatabaseData();
updateListView();
}
});
btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new DbHelper(MainActivity.this).deleteData(); //calling the method [deleteData] in DbHleper.java Class
DataList = new DbHelper(context).getAllDatabaseData();
updateListView();
}
});
}
void updateListView(){
Adapter = new SimpleAdapter(MainActivity.this, DataList,
R.layout.listview_data, new String[]{"first_name", "surname"},
new int[]{R.id.firstname, R.id.surname});
dataLV.setAdapter(Adapter);
}
}
Надеть activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp"
android:gravity="center_horizontal"
tools:context=".MainActivity">
<EditText
android:id="@+id/et_firstname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="First Name"
android:padding="10dp"/>
<EditText
android:id="@+id/et_surname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Surname"
android:padding="10dp"/>
<Button
android:id="@+id/Submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit"/>
<Button
android:id="@+id/Delete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Delete"/>
<View
android:layout_width="match_parent"
android:layout_height="10dp" />
<ListView
android:id="@+id/data_listview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
, затем создайте listview_data.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/firstname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"/>
<View
android:layout_width="20dp"
android:layout_height="10dp" />
<TextView
android:id="@+id/surname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp" />
</LinearLayout>