· 10 years ago · Sep 22, 2016, 05:30 PM
1activity_main.xml
2====================
3<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
4 android:layout_width="match_parent"
5 android:layout_height="match_parent"
6 android:orientation="vertical">
7
8 <TextView
9 android:id="@+id/myTask"
10 android:layout_width="match_parent"
11 android:layout_height="wrap_content"
12 android:layout_marginTop="20dp"
13 android:text="ADD TASK (DURC)"
14 android:textSize="30sp" />
15
16 <EditText
17 android:id="@+id/taskName"
18 android:layout_width="match_parent"
19 android:layout_height="wrap_content"
20 android:hint="enter task...."
21 android:textSize="32sp" />
22
23 <Button
24 android:layout_width="wrap_content"
25 android:layout_height="wrap_content"
26 android:onClick="btnAdd"
27 android:text="Add" />
28
29 <Button
30 android:layout_width="wrap_content"
31 android:layout_height="wrap_content"
32 android:onClick="btnRet"
33 android:text="RETRIVE ALL TASKS" />
34
35 <Button
36 android:layout_width="wrap_content"
37 android:layout_height="wrap_content"
38 android:onClick="getCount"
39 android:text="GET COUNT" />
40
41 <Button
42 android:layout_width="wrap_content"
43 android:layout_height="wrap_content"
44 android:onClick="delMe"
45 android:text="DELETE ALL !!!!"/>
46</LinearLayout>
47
48MainActivity.java
49=====================
50package com.example.zeevm.mysql;
51
52import android.database.Cursor;
53import android.support.v7.app.AppCompatActivity;
54import android.os.Bundle;
55import android.view.View;
56import android.widget.EditText;
57import android.widget.TextView;
58import android.widget.Toast;
59
60import org.w3c.dom.Text;
61
62import java.util.ArrayList;
63import java.util.List;
64
65public class MainActivity extends AppCompatActivity {
66
67 utlSQL mySQL;
68 TextView myTask;
69 EditText taskName;
70
71 @Override
72 protected void onCreate(Bundle savedInstanceState) {
73 super.onCreate(savedInstanceState);
74 setContentView(R.layout.activity_main);
75 setPointer();
76 }
77
78 private void setPointer()
79 {
80 myTask=(TextView)findViewById(R.id.myTask);
81 taskName=(EditText)findViewById(R.id.taskName);
82 mySQL=new utlSQL(this,"zeev");
83 }
84
85 public void btnAdd(View view)
86 {
87 if (taskName.getText().toString().isEmpty()) return;
88
89 if (mySQL.newTask(taskName.getText().toString()))
90 {
91 Toast.makeText(MainActivity.this, "task addd", Toast.LENGTH_SHORT).show();
92 taskName.setText("");
93 }
94 else
95 {
96 Toast.makeText(MainActivity.this, "error in adding a new task", Toast.LENGTH_SHORT).show();
97 }
98 }
99
100 public void btnRet(View view)
101 {
102 Cursor res = mySQL.getTasks();
103 if (res.getCount()==0)
104 {
105 Toast.makeText(MainActivity.this, "WE DONT HAVE ANY TASKS", Toast.LENGTH_SHORT).show();
106 return;
107 }
108 List<String> myList=new ArrayList<>();
109 while (res.moveToNext())
110 {
111 myList.add(res.getString(1));
112 }
113 Toast.makeText(MainActivity.this, "tasks:"+myList.toString(), Toast.LENGTH_SHORT).show();
114 }
115
116 public void getCount(View view)
117 {
118 Cursor res = mySQL.getTasks();
119 Toast.makeText(MainActivity.this, "Total:"+res.getCount(), Toast.LENGTH_SHORT).show();
120 }
121
122 public void delMe(View view)
123 {
124 mySQL.deleteAll();
125 Cursor res = mySQL.getTasks();
126 Toast.makeText(MainActivity.this, "Total tasks:"+res.getCount(), Toast.LENGTH_SHORT).show();
127 }
128}
129
130
131utlSQL.java
132==================
133package com.example.zeevm.mysql;
134
135import android.content.ContentValues;
136import android.content.Context;
137import android.database.Cursor;
138import android.database.sqlite.SQLiteDatabase;
139import android.database.sqlite.SQLiteOpenHelper;
140import android.database.sqlite.SQLiteStatement;
141
142import java.util.Date;
143
144/**
145 * Created by zeevm on 9/22/2016.
146 */
147public class utlSQL extends SQLiteOpenHelper {
148
149 //name of DB file to use.....(must be static)
150 public final static String DB_NAME="tasks.db";
151 //name of TABLE
152 public String tableName;
153 //create a DB instance
154 SQLiteDatabase db;
155 Context context;
156
157 public utlSQL(Context context,String tableName)
158 {
159 //we must pass to SQLiteOpenHelper the context, db_name, null for cursor and version
160 super(context,DB_NAME,null,1);
161 //pointers to field
162 this.context=context;
163 this.tableName=tableName;
164 //get DB file for write and read;
165 db = this.getWritableDatabase();
166 }
167
168 @Override
169 public void onCreate(SQLiteDatabase sqLiteDatabase) {
170 sqLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS "+tableName+" (id INTEGER PRIMARY KEY AUTOINCREMENT,task TEXT, status INTEGER, date TEXT)");
171 }
172
173 @Override
174 public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
175 //HE TPOHЬ 3APA3A
176 //do not touch
177 //db.execSQL("DROP TABLE IF EXISTS "+tableName);
178 //super(db)
179 }
180
181 //method 1 to add a task
182 public boolean newTask(String taskName)
183 {
184 //create new instance of content values
185 ContentValues contentValues=new ContentValues();
186 contentValues.put("task",taskName);
187 contentValues.put("status",0); //0->unDone task 1->task was done...
188 contentValues.put("date", new Date().toString());
189 //insert into table our values
190 long res=db.insert(tableName,null,contentValues);
191
192 //return true if res>=0 else return false res=(-1)
193 return !(res<0);
194 }
195
196 //method 2 to add task (SQL style)
197 public boolean newTask2(String taskName)
198 {
199 //create sql string -> INSERT INTO [table name] (task,status,date) value ('task name',0,date);
200
201 //INSERT INTO zeev (task,status,date) VALUES('clean the house',0,'23/09/2016') ;
202 String sql = "INSERT INTO "+tableName+" (task,status,date) VALUES('"+taskName+"',0,'"+new Date().toString()+"')";
203 //compile our sql statment into ContentValues
204 SQLiteStatement statement = db.compileStatement(sql);
205 //we send the sql command,esult of (-1) is an error
206 long res=statement.executeInsert();
207 //return true if res>=0 else return false since res=(-1)
208 return !(res<0);
209 }
210
211 public Cursor getTasks()
212 {
213 Cursor res=db.rawQuery("SELECT * FROM "+tableName,null);
214 return res;
215 }
216
217
218 //method 1
219 public void update(int id, String taskName, int taskStatus, String myDate)
220 {
221 //create instance of content values
222 ContentValues contentValues = new ContentValues();
223 //put data inside out record
224 contentValues.put ("id",id);
225 contentValues.put ("task",taskName);
226 contentValues.put ("status",taskStatus);
227 contentValues.put ("date",myDate);
228 //save data
229 db.update(tableName,contentValues,"id = ?", new String[] {id+""});
230 }
231
232 //method 2 -> change the task name
233 public void update2(String original, String replaced)
234 {
235 db.execSQL("UPDATE "+tableName+" SET task='"+replaced+"' WHERE task='"+original+"'");
236 }
237
238 public void deleteAll()
239 {
240 db.execSQL("DELETE FROM "+tableName);
241 }
242
243}