· 8 years ago · Feb 02, 2018, 07:48 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
64while (cursor.moveToNext()) {
65 int index = cursor.getColumnIndex(TaskEntry.COL_TASK_TITLE);
66 tasklist.add(cursor.getString(index));
67
68 }
69if (mAdapter == null) {
70mAdapter = new ArrayAdapter<>(this, R.layout.item_todo, R.id.task_title,
71tasklist);
72mTaskListView.setAdapter(mAdapter);
73
74} else{
75 mAdapter.clear();
76 mAdapter.addAll(tasklist);
77 mAdapter.notifyDataSetChanged();
78 }