· 7 years ago · Sep 08, 2018, 09:58 AM
1<?xml version="1.0" encoding="utf-8"?>
2<menu xmlns:android="http://schemas.android.com/apk/res/android"
3xmlns:app="http://schemas.android.com/apk/res-auto">
4
5<item
6 android:id="@+id/action_add_task"
7 android:icon="@drawable/add_item"
8 android:title="@string/add_task"
9 app:showAsAction="always"
10 />
11</menu>
12
13<?xml version="1.0" encoding="utf-8"?>
14<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
15xmlns:app="http://schemas.android.com/apk/res-auto"
16android:layout_width="match_parent"
17android:layout_height="match_parent"
18android:layout_gravity="center_vertical">
19
20<TextView
21 android:id="@+id/task_title"
22 android:layout_width="wrap_content"
23 android:layout_height="wrap_content"
24 android:layout_alignParentLeft="true"
25 android:layout_alignParentStart="true"
26 android:text="@string/hi"
27 android:textSize="20sp" />
28
29
30
31
32 <Button
33 android:id="@+id/task_delete"
34 android:layout_width="wrap_content"
35 android:layout_height="wrap_content"
36 android:layout_alignParentEnd="true"
37 android:layout_alignParentRight="true"
38 android:onClick="deleteTask"
39 android:text="@string/done" />
40
41</RelativeLayout>
42
43<?xml version="1.0" encoding="utf-8"?>
44<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
45xmlns:app="http://schemas.android.com/apk/res-auto"
46xmlns:tools="http://schemas.android.com/tools"
47android:layout_width="match_parent"
48android:layout_height="match_parent"
49tools:context=".MainActivity">
50
51<ListView
52 android:id="@+id/list_todo"
53 android:layout_width="wrap_content"
54 android:layout_height="wrap_content">
55
56</ListView>
57
58<Button
59
60android:id="@+id/btnAdd"
61android:layout_width="65dp"
62android:layout_height="65dp"
63android:layout_alignParentBottom="true"
64android:layout_alignParentRight="true"
65android:layout_margin="20dp"
66android:background="@drawable/circle"
67/>
68
69</RelativeLayout>
70
71import android.content.ContentValues;
72import android.content.DialogInterface;
73import android.database.Cursor;
74import android.database.sqlite.SQLiteDatabase;
75
76import android.support.v7.app.AlertDialog;
77import android.support.v7.app.AppCompatActivity;
78import android.os.Bundle;
79import android.view.Menu;
80
81import android.view.MenuItem;
82import android.view.View;
83import android.widget.ArrayAdapter;
84import android.widget.Button;
85import android.widget.EditText;
86import android.widget.ListView;
87import android.widget.TextView;
88
89
90
91import java.util.ArrayList;
92
93public class MainActivity extends AppCompatActivity {
94
95private static final String TAG = "MainActivity";
96private TaskHelper mHelper;
97private ListView mTaskListView;
98private ArrayAdapter<String> mAdapter;
99Button btnAdd;
100
101@Override
102protected void onCreate(Bundle savedInstanceState) {
103 super.onCreate(savedInstanceState);
104 setContentView(R.layout.activity_main);
105
106 mHelper = new TaskHelper(this);
107 mTaskListView = (ListView) findViewById(R.id.list_todo);
108 btnAdd = (Button) findViewById(R.id.btnAdd);
109
110 updateUI();
111}
112
113
114@Override
115public boolean onCreateOptionsMenu(Menu menu) {
116 getMenuInflater().inflate(R.menu.main_menu, menu);
117 return super.onCreateOptionsMenu(menu);
118
119}
120
121@Override
122public boolean onOptionsItemSelected(MenuItem item) {
123 switch (item.getItemId()) {
124 case R.id.action_add_task:
125 final EditText taskEditText = new EditText(this);
126 AlertDialog dialog = new AlertDialog.Builder(this)
127 .setTitle("New Task")
128 .setMessage("Add a new task")
129 .setView(taskEditText)
130 .setPositiveButton("Add", new `
131 DialogInterface.OnClickListener() {`
132 @Override
133 public void onClick(DialogInterface dialog, int `
134 which) {`
135 String task =
136 String.valueOf(taskEditText.getText());
137 SQLiteDatabase db = mHelper.getWritableDatabase();
138 ContentValues values = new ContentValues();
139 values.put(Task.TaskEntry.COL_TASK_TITLE, task);
140 db.insertWithOnConflict(Task.TaskEntry.TABLE,
141 null, values, SQLiteDatabase.CONFLICT_REPLACE);
142 db.close();
143 updateUI();
144
145 }
146 })
147
148 .setNegativeButton("Cancel", null)
149 .create();
150 dialog.show();
151 return true;
152
153 default:
154 return super.onOptionsItemSelected(item);
155
156 }
157 }
158
159
160 private void updateUI() {
161
162 ArrayList<String> taskList = new ArrayList<>();
163 SQLiteDatabase db = mHelper.getReadableDatabase();
164 Cursor cursor = db.query(Task.TaskEntry.TABLE,
165 new String[] {Task.TaskEntry._ID,
166 Task.TaskEntry.COL_TASK_TITLE}, null, null, null, null, null);
167 while (cursor.moveToNext()) {
168 int index = cursor.getColumnIndex(Task.TaskEntry.COL_TASK_TITLE);
169 taskList.add(cursor.getString(index));
170 }
171
172 if (mAdapter == null) {
173 mAdapter = new ArrayAdapter<>(this, R.layout.item_todo,
174 R.id.task_title, taskList);
175 mTaskListView.setAdapter(mAdapter);
176 } else {
177 mAdapter.clear();
178 mAdapter.addAll(taskList);
179 mAdapter.notifyDataSetChanged();
180
181 }
182
183 cursor.close();
184 db.close();
185}
186
187public void deleteTask(View view) {
188 View parent = (View) view.getParent();
189 TextView taskTextView = (TextView) parent.findViewById(R.id.task_title);
190 String task = String.valueOf(taskTextView.getText());
191 SQLiteDatabase db = mHelper.getWritableDatabase();
192 db.delete(Task.TaskEntry.TABLE, Task.TaskEntry.COL_TASK_TITLE + " = ?",
193new String[]{task});
194 db.close();
195 updateUI();
196}
197
198
199}
200
201package com.kentforth.list5;
202
203import android.provider.BaseColumns;
204
205public class Task {
206
207 public static final String DB_NAME = "ToDoDB";
208 public static final int DB_VERSION = 1;
209
210 public class TaskEntry implements BaseColumns {
211 public static final String TABLE = "tasks";
212 public static final String COL_TASK_TITLE = "title";
213 }
214
215 }
216
217package com.kentforth.list5;
218
219import android.content.Context;
220import android.database.sqlite.SQLiteDatabase;
221import android.database.sqlite.SQLiteOpenHelper;
222
223public class TaskHelper extends SQLiteOpenHelper{
224
225public TaskHelper(Context context) {
226 super(context, Task.DB_NAME, null, Task.DB_VERSION);
227}
228
229@Override
230public void onCreate(SQLiteDatabase db) {
231String createTable = "CREATE TABLE " + Task.TaskEntry.TABLE + " ( " +
232 Task.TaskEntry._ID + " INTEGER PRIMARY
233KEY AUTOINCREMENT, " +
234 Task.TaskEntry.COL_TASK_TITLE + " TEXT
235NOT NULL);";
236 db.execSQL(createTable);
237}
238
239@Override
240public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
241 db.execSQL("DROP TABLE IF EXISTS" + Task.TaskEntry.TABLE);
242 onCreate(db);
243 }
244
245}
246
247@Override
248 protected void onCreate(Bundle savedInstanceState) {
249 super.onCreate(savedInstanceState);
250 setContentView(R.layout.activity_main);
251
252 mHelper = new TaskHelper(this);
253 mTaskListView = (ListView) findViewById(R.id.list_todo);
254 btnAdd = (Button) findViewById(R.id.btnAdd);
255 btnAdd.setOnClickListener(new View.OnClickListener() {
256 @Override
257 public void onClick(View v) {
258 addTask();
259 }
260 });
261 updateUI();
262}
263
264private void addTask(){
265final EditText taskEditText = new EditText(this);
266 AlertDialog dialog = new AlertDialog.Builder(this)...
267 // и так далее
268 .create();
269 dialog.show();
270}