· 8 years ago · Feb 02, 2018, 07:52 AM
1public void editTask(View view){
2
3 final EditText taskEdit = new EditText(this);
4 AlertDialog dialog = new AlertDialog.Builder(this).setTitle("Редактирование заметки").setMessage("Введите новый текÑÑ‚ заметки ")
5 .setView(taskEdit).setPositiveButton(" Изменить ", new DialogInterface.OnClickListener() {
6 @Override
7 public void onClick(DialogInterface dialog, int which) {
8 String task = String.valueOf(taskEdit.getText());
9 SQLiteDatabase db = mHelper.getWritableDatabase();
10 ContentValues newvalues = new ContentValues();
11 newvalues.put(com.example.alexandrkuchinsky.myapplication.db.task.TaskEntry.COL_TASK_TITLE,task);
12 String where = TaskEntry._ID + " = ?";
13
14 db.update(TaskEntry.TABLE, newvalues, where, null);
15
16 db.close();
17 updateUI();
18 }
19 })
20
21 .setNegativeButton(" Отмена ", null).create();
22 dialog.show();
23
24 }
25
26public class TaskHelper extends SQLiteOpenHelper {
27
28 public TaskHelper(Context context){
29 super(context, task.DB_name, null, task.Db_versiom);
30
31}
32 @Override
33 public void onCreate(SQLiteDatabase db) {
34 String createTable = "CREATE TABLE " + task.TaskEntry.TABLE + " ( " +
35 task.TaskEntry._ID + " INTEGER PRIMARY
36 KEY AUTOINCREMENT, " +
37 task.TaskEntry.COL_TASK_TITLE + " TEXT
38NOT NULL);";
39db.execSQL(createTable);
40 }
41
42@Override
43public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
44db.execSQL("DROP TABLE IF EXISTS " + task.TaskEntry.TABLE);
45onCreate(db);
46}
47
48
49}
50
51public class task {
52
53public static final String DB_name = "cas.example.com.todolist.db";
54public static final int Db_versiom = 1;
55public class TaskEntry implements BaseColumns{
56public static final String TABLE = "tasks";
57public static final String COL_TASK_TITLE = "title";
58
59}
60}
61
62private void updateUI(){
63 ArrayList<String> tasklist = new ArrayList<>();
64 SQLiteDatabase db = mHelper.getReadableDatabase();
65 Cursor cursor = db.query(TaskEntry.TABLE, new String[]{TaskEntry._ID,
66 TaskEntry.COL_TASK_TITLE}, null, null, null, null, null);
67
68 while (cursor.moveToNext()) {
69 int index = cursor.getColumnIndex(TaskEntry.COL_TASK_TITLE);
70 tasklist.add(cursor.getString(index));
71
72 }
73if (mAdapter == null) {
74mAdapter = new ArrayAdapter<>(this, R.layout.item_todo, R.id.task_title,
75tasklist);
76mTaskListView.setAdapter(mAdapter);
77
78} else{
79 mAdapter.clear();
80 mAdapter.addAll(tasklist);
81 mAdapter.notifyDataSetChanged();
82 }