<?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:id="@+id/main"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="@+id/tasks_list_view"
android:layout_width="match_parent"
android:layout_height="600dp"/>
<LinearLayout
android:background="#ababab"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/task_text"
android:hint="Введите название задачи"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:layout_margin="5dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textSize="20sp"
android:text="Выполнено"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Switch
android:layout_marginLeft="5dp"
android:id="@+id/is_done_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<Button
android:id="@+id/add_task_btn"
android:text="Добавить"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
<?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/item_task_id"
android:text="fasdfasdf"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/item_task_text"
android:text="sadfsadf"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Switch
android:id="@+id/item_switch"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
package com.example.dbappexample;
public class Task {
private int id;
private String text;
private boolean isDone;
public Task(int id, String text, boolean isDone) {
this.id = id;
this.text = text;
this.isDone = isDone;
}
public int getId() {
return id;
}
public String getText() {
return text;
}
public boolean isDone() {
return isDone;
}
}
package com.example.dbappexample;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
public class TasksDB {
private final String DB_NAME = "db";
private final int DB_VERSION = 1;
private final String TABLE_NAME = "tasks";
private final String COLUMN_ID = "id";
private final String COLUMN_TEXT = "text";
private final String COLUMN_IS_DONE = "is_done";
private SQLiteDatabase dataBase;
public TasksDB(Context context){
OpenHelper openHelper = new OpenHelper(context);
dataBase = openHelper.getWritableDatabase();
}
public void add(String text) {
ContentValues contentValues = new ContentValues();
contentValues.put(COLUMN_TEXT, text);
contentValues.put(COLUMN_IS_DONE, false);
dataBase.insert(TABLE_NAME, null, contentValues);
}
public ArrayList<Task> getTasks() {
// SELECT * FROM tasks
Cursor cursor = dataBase.query(TABLE_NAME, null, null, null, null, null, null);
ArrayList<Task> tasks = new ArrayList<>();
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_ID));
String text = cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_TEXT));
int isDone = cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_IS_DONE));
tasks.add(new Task(id, text, isDone != 0));
}
return tasks;
}
private class OpenHelper extends SQLiteOpenHelper {
OpenHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + TABLE_NAME + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_TEXT + " STRING, " +
COLUMN_IS_DONE + " BOOLEAN" +
")";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String query = "DROP TABLE " + TABLE_NAME;
db.execSQL(query);
onCreate(db);
}
}
}
package com.example.dbappexample;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Switch;
import android.widget.TextView;
import java.util.ArrayList;
public class TaskAdapter extends ArrayAdapter<Task> {
public TaskAdapter(Context context, ArrayList<Task> tasks) {
super(context, R.layout.task_list_item, tasks);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null){
convertView = LayoutInflater.from(getContext()).inflate(
R.layout.task_list_item,
null
);
}
TextView textViewId = convertView.findViewById(R.id.item_task_id);
TextView textViewText = convertView.findViewById(R.id.item_task_text);
Switch itemSwitch = convertView.findViewById(R.id.item_switch);
Task task = getItem(position);
textViewId.setText(String.valueOf(task.getId()));
textViewText.setText(task.getText());
itemSwitch.setChecked(task.isDone());
return convertView;
}
}
package com.example.dbappexample;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Switch;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
ListView listView;
Button addButton;
EditText text;
Switch isDoneSwitch;
TasksDB tasksDB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tasksDB = new TasksDB(getApplicationContext());
listView = findViewById(R.id.tasks_list_view);
addButton = findViewById(R.id.add_task_btn);
text = findViewById(R.id.task_text);
isDoneSwitch = findViewById(R.id.is_done_switch);
TaskAdapter taskAdapter = new TaskAdapter(
getApplicationContext(),
tasksDB.getTasks()
);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tasksDB.add(text.getText().toString());
taskAdapter.clear();
taskAdapter.addAll(tasksDB.getTasks());
taskAdapter.notifyDataSetInvalidated();
}
});
listView.setAdapter(taskAdapter);
}
}