-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMainActivity.java
More file actions
93 lines (83 loc) · 3.15 KB
/
MainActivity.java
File metadata and controls
93 lines (83 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package com.example.todoappjava;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.annotation.SuppressLint;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.example.todoappjava.db.DatabaseHelper;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements TodoAdapter.OnItemClickListener {
private RecyclerView rvTodos;
private EditText etTodo;
private Button btnAddTodo;
private final ArrayList<TodoData> todoList = new ArrayList<>();
private final TodoAdapter todoAdapter = new TodoAdapter(todoList, this);
private final DatabaseHelper myDB = new DatabaseHelper(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
rvTodos.setLayoutManager(new LinearLayoutManager(this));
rvTodos.setAdapter(todoAdapter);
loadTodos();
btnAddTodo.setOnClickListener(v -> addTodo());
}
private void addTodo() {
String text = etTodo.getText().toString();
if(text.isEmpty())
{
// Bug 1 fixed
Toast.makeText(MainActivity.this, "Empty TODO can't be saved", Toast.LENGTH_SHORT).show();
}
else{
boolean isSuccess = myDB.insertData(text);
//App Not Crashing
Toast.makeText(MainActivity.this, "Todo added successfully!", Toast.LENGTH_SHORT).show();
loadTodos();
//Solved bug 2
etTodo.setText("");
}
}
private TextView tt;
@SuppressLint("NotifyDataSetChanged")
private void loadTodos() {
Cursor cursor = myDB.getAllData();
if(cursor!=null){
tt=findViewById(R.id.textView);
if (cursor.moveToFirst()) {
tt.setText("");
String todoText;
int todoId;
// bug 3 Fixed
do {
todoId = cursor.getInt(cursor.getColumnIndex(DatabaseHelper.TODO_ID));
todoText = cursor.getString(cursor.getColumnIndex(DatabaseHelper.TODO_TEXT));
} while (cursor.moveToNext());
todoList.add(new TodoData(todoId, todoText));
}
else{
// bug 7 fixed
tt.setText("TODO LIST Empty !! \n ADD A Task to see the LIST .");
}
cursor.close();
todoAdapter.notifyDataSetChanged();
}
}
private void initViews() {
rvTodos = findViewById(R.id.rvTodos);
etTodo = findViewById(R.id.etTodo);
btnAddTodo = findViewById(R.id.btnAddTodo);
}
@Override
public void onDeleteClick(int position) {
String itemIdToDelete = String.valueOf(todoList.get(position).getId());
Toast.makeText(this, "This item should be deleted "+itemIdToDelete, Toast.LENGTH_SHORT).show();
loadTodos();
}
}