-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMainActivity.java
More file actions
107 lines (88 loc) · 3.54 KB
/
MainActivity.java
File metadata and controls
107 lines (88 loc) · 3.54 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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();
// to do wont be added if text field is empty
if (text.length() !=0) {
boolean isSuccess = myDB.insertData(text);
if (isSuccess) {
Toast.makeText(this, "Todo added successfully!", Toast.LENGTH_SHORT).show();
etTodo.setText("");
loadTodos();
} else {
Toast.makeText(this, "Something went wrong!", Toast.LENGTH_SHORT).show();
}
}
else{
Toast.makeText(this, "Please enter text", Toast.LENGTH_SHORT).show();
}
}
@SuppressLint({"NotifyDataSetChanged", "SetTextI18n"})
private void loadTodos() {
Cursor cursor = myDB.getAllData();
if(cursor!=null){
if (cursor.moveToFirst()) {
String todoText;
int todoId;
// Solved id+1 bug which was repeating the inserted items
todoList.clear();
while (cursor.moveToNext()) {
todoId = cursor.getInt(cursor.getColumnIndex(DatabaseHelper.TODO_ID));
todoText = cursor.getString(cursor.getColumnIndex(DatabaseHelper.TODO_TEXT));
todoList.add(new TodoData(todoId, todoText));
}
}
cursor.close();
todoAdapter.notifyDataSetChanged();
if(todoList.isEmpty()){
TextView nulltext = (TextView)findViewById(R.id.nullText);
nulltext.setText("seems like you dont have any TODO yet please add Some.");
}
else {
TextView nulltext = (TextView) findViewById(R.id.nullText);
nulltext.setText("");
}
}
}
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());
// added delete function
myDB.deleteRecord(itemIdToDelete);
loadTodos();
Toast.makeText(this, "This item should be deleted "+itemIdToDelete, Toast.LENGTH_SHORT).show();
}
}