· 8 years ago · Apr 12, 2018, 05:44 AM
1--------- Manifest.xml
2
3<?xml version="1.0" encoding="utf-8"?>
4<manifest xmlns:android="http://schemas.android.com/apk/res/android"
5 xmlns:tools="http://schemas.android.com/tools"
6 package="com.example.vladel.myapplication">
7
8 <uses-sdk
9 android:minSdkVersion="8"
10 android:targetSdkVersion="8" />
11
12 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
13 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
14 <uses-permission android:name="android.permission.READ_PHONE_STATE" />
15 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
16
17 <application
18 android:allowBackup="true"
19 android:icon="@mipmap/ic_launcher"
20 android:label="@string/app_name"
21 android:roundIcon="@mipmap/ic_launcher_round"
22 android:supportsRtl="true"
23 android:theme="@style/AppTheme">
24 <activity android:name=".MainActivity"
25
26 android:windowSoftInputMode="adjustResize">
27
28 <intent-filter>
29 <action android:name="android.intent.action.MAIN" />
30
31 <category android:name="android.intent.category.LAUNCHER" />
32 </intent-filter>
33 </activity>
34 <activity android:name=".Ex_3_anctivity"></activity>
35 </application>
36
37</manifest>
38
39----
40
41package com.example.vladel.myapplication;
42
43import android.Manifest;
44import android.content.Context;
45import android.content.Intent;
46import android.content.pm.PackageManager;
47import android.database.Cursor;
48import android.location.Location;
49import android.location.LocationListener;
50import android.location.LocationManager;
51import android.support.v4.app.ActivityCompat;
52import android.support.v7.app.AppCompatActivity;
53import android.os.Bundle;
54import android.util.Log;
55import android.view.View;
56import android.widget.Button;
57import android.widget.TextView;
58import android.widget.Toast;
59
60
61public class MainActivity extends AppCompatActivity {
62
63
64 TextView msg;
65 Button btn, ex3Btn;
66 private LocationManager locManager;
67 private LocationListener locListener;
68 private Location mobileLocation;
69 DBAdapter db;
70
71 ///////////////////////////////////////////////// exercitiu 1
72// @Override
73// protected void onCreate(Bundle savedInstanceState) {
74// super.onCreate(savedInstanceState);
75// setContentView(R.layout.activity_main);
76//
77// msg= (TextView) findViewById(R.id.textField);
78// btn= (Button) findViewById(R.id.button);
79// locManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
80//
81// locListener = new LocationListener() {
82//
83// @Override
84// public void onStatusChanged(String provider, int status,Bundle extras)
85// {}
86// @Override
87// public void onProviderEnabled(String provider) {}
88//
89// @Override
90// public void onProviderDisabled(String provider) {}
91//
92// @Override
93// public void onLocationChanged(Location location) {
94// mobileLocation = location;
95// }
96// };
97//
98// locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,locListener);
99//
100// btn.setOnClickListener(new View.OnClickListener() {
101// @Override
102// public void onClick(View v) {
103// if ( locManager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ){
104// msg.setText("GPS is ON");
105// if (mobileLocation != null) {
106// String longitude = "Longitude: " + mobileLocation.getLongitude();
107// String latitude = "Latitude: " + mobileLocation.getLatitude();
108// String bearing = "Bearing:" + mobileLocation.getBearing();
109// String provider = "Provider:" + mobileLocation.getProvider();
110// String accuracy = "accuracy:" + mobileLocation.getAccuracy();
111//
112// String elapsedRTN = "elapsedRTN:" + mobileLocation.getElapsedRealtimeNanos();
113// String speed = "speed:" + mobileLocation.getSpeed();
114// String altitude = "altitude:" + mobileLocation.getAltitude();
115// String time = "time:" + mobileLocation.getTime();
116// msg.setText("GPS is ON \n"+longitude+"\n"+latitude
117// +"\n"+bearing
118// +"\n"+provider
119// +"\n"+accuracy
120// +"\n"+elapsedRTN
121// +"\n"+speed
122// +"\n"+altitude
123// +"\n"+time
124// );
125//
126// }
127// }
128// else
129// msg.setText("GPS is OFF");
130// }
131// });
132//
133
134 ///////////////////////////////////////////////// exercitiu 2 - se modifica in timp - cred
135 @Override
136 protected void onCreate(Bundle savedInstanceState) {
137
138 super.onCreate(savedInstanceState);
139 setContentView(R.layout.activity_main);
140
141
142 btn = (Button) findViewById(R.id.button);
143 msg = (TextView) findViewById(R.id.tv_data);
144 ex3Btn = (Button) findViewById(R.id.ex3);
145
146 try {
147 // Start db connection
148 db = new DBAdapter(this);
149 msg.setText("DB CREATED");
150 } catch (Exception e) {
151 Log.e("DB ERROR", e.toString());
152
153 e.printStackTrace();
154 }
155
156 locManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
157
158 locListener = new LocationListener() {
159
160 @Override
161 public void onStatusChanged(String provider, int status, Bundle extras) {
162 }
163
164 @Override
165 public void onProviderEnabled(String provider) {
166 }
167
168 @Override
169 public void onProviderDisabled(String provider) {
170 }
171
172 @Override
173 public void onLocationChanged(Location location) {
174
175 ///// decomentati asta
176
177// try {
178// // Start db connection
179// db.open();
180// db.insertContact(String.valueOf(location.getLongitude()), String.valueOf(location.getLatitude()));
181// db.close();
182//// msg.setText("econfirm 1");
183// } catch (Exception e) {
184// Log.e("DB ERROR", e.toString());
185// msg.setText("error 1");
186// e.printStackTrace();
187// }
188
189
190 }
191 };
192
193
194 locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
195
196
197 //////////////////// asta merge in exercitiu 3
198 ex3Btn.setOnClickListener(new View.OnClickListener() {
199 @Override
200 public void onClick(View v) {
201 Intent ex_3_activ = new Intent(MainActivity.this, Ex_3_anctivity.class);
202 startActivity(ex_3_activ);
203 }
204 });
205
206 btn.setOnClickListener(new View.OnClickListener() {
207 @Override
208 public void onClick(View v) {
209
210
211 try {
212 // Start db connection
213 db.open();
214// msg.setText("confirm 3");
215 String data = db.getData();
216 db.close();
217 if(data != "") {
218 msg.setText(data);
219// msg.setText("stuff");
220 }
221 else
222 msg.setText("empty DB");
223 } catch (Exception e) {
224 Log.e("DB ERROR", e.toString());
225 msg.setText("error 2");
226 e.printStackTrace();
227 }
228
229
230
231 }
232 });
233
234
235
236 }
237}
238
239------ DB - Adapter
240
241package com.example.vladel.myapplication;
242
243
244import android.content.ContentValues;
245import android.content.Context;
246import android.database.Cursor;
247import android.database.SQLException;
248import android.database.sqlite.SQLiteDatabase;
249import android.database.sqlite.SQLiteOpenHelper;
250import android.util.Log;
251
252public class DBAdapter {
253
254 static final String KEY_ROWID = "id";
255 static final String NAME = "longitude";
256 static final String EMAIL = "latitude";
257 static final String LOG_TAG = "DBAdapter";
258 static final String DATABASE_NAME = "MyDB";
259 static final String DATABASE_TABLE = "GPS_data";
260 static final int DATABASE_VERSION = 1;
261 static final String DATABASE_CREATE = "CREATE TABLE GPS_data (id INTEGER PRIMARY KEY AUTOINCREMENT, "
262 + "longitude TEXT NOT NULL, latitude TEXT NOT NULL);";
263
264 final Context context;
265 DatabaseHelper DBHelper;
266 SQLiteDatabase db;
267
268 public DBAdapter(Context ctx) {
269 this.context = ctx;
270 DBHelper = new DatabaseHelper(context);
271 }
272
273
274 //Inner nested class (not anonymous)
275 private static class DatabaseHelper extends SQLiteOpenHelper {
276 DatabaseHelper(Context context) {
277 super(context, DATABASE_NAME, null, DATABASE_VERSION);
278 }
279 @Override
280 public void onCreate(SQLiteDatabase db) {
281 try {
282 db.execSQL(DATABASE_CREATE);
283 } catch (SQLException e) {
284 e.printStackTrace();
285 }
286 }
287 @Override
288 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
289 {
290 Log.w(LOG_TAG, "Upgrading DB from vs." + oldVersion + " to "
291 + newVersion + ", old data will be destroyed");
292 db.execSQL("DROP TABLE IF EXISTS contacts");
293 onCreate(db);
294 }
295 }// end inner class
296 // Open the db
297 public DBAdapter open() throws SQLException {
298 db = DBHelper.getWritableDatabase();
299 return this;
300 }
301 // Close the db
302 public void close() {
303 DBHelper.close();
304 }
305 // Insert a record to the contacts table
306 public long insertContact(String name, String email) {
307 ContentValues initialValues = new ContentValues();
308 initialValues.put(NAME, name);
309 initialValues.put(EMAIL, email);
310 return db.insert(DATABASE_TABLE, null, initialValues);
311 }
312 // Delete a record from the table
313 public boolean deleteContact(long rowId) {
314 return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
315 }
316 // Retrieve all the contacts through a Cursor
317 public Cursor getAllContacts() {
318 return db.query(DATABASE_TABLE, new String[] { KEY_ROWID, NAME,
319 EMAIL }, null, null, null, null, null);
320 }
321 // Return a String with all the data
322 public String getData() {
323 String[] columns = new String[] { KEY_ROWID, NAME, EMAIL };
324 Cursor c = db.query(DATABASE_TABLE, columns, null, null, null, null,null);
325 String result = "";
326 int iRow = c.getColumnIndex(KEY_ROWID);
327 int iName = c.getColumnIndex(NAME);
328 int iEmail = c.getColumnIndex(EMAIL);
329 for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
330 result = result + " " + c.getString(iRow) + " "
331 + c.getString(iName) + " " + c.getString(iEmail) + "\n";
332 }//end for
333 return result;
334 }
335 // Get a particular contact
336 public Cursor getContact(long rowId) throws SQLException {
337 Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {
338 KEY_ROWID, NAME, EMAIL }, KEY_ROWID + "=" + rowId,
339 null, null, null, null, null);
340 if (mCursor != null) {
341 mCursor.moveToFirst();
342 }
343 return mCursor;
344 }
345 // Update a contact
346 public boolean updateContact(long rowId, String name, String email) {
347 ContentValues args = new ContentValues();
348 args.put(NAME, name);
349 args.put(EMAIL, email);
350 return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) >
351 0;
352 }
353}// end DBAdapter
354
355
356---- DB - Adapter Ex3
357
358package com.example.vladel.myapplication;
359
360import android.content.ContentValues;
361import android.content.Context;
362import android.database.Cursor;
363import android.database.SQLException;
364import android.database.sqlite.SQLiteDatabase;
365import android.database.sqlite.SQLiteOpenHelper;
366import android.util.Log;
367
368
369public class DBAdapter_ex_3 {
370
371 static final String KEY_ROWID = "id";
372 static final String NAME = "name";
373 static final String CARD_ID = "card_ID";
374 static final String LOG_TAG = "DBAdapter";
375 static final String DATABASE_NAME = "MyDB_2";
376 static final String DATABASE_TABLE = "Ex_3";
377 static final int DATABASE_VERSION = 1;
378 static final String DATABASE_CREATE = "CREATE TABLE Ex_3 (id INTEGER PRIMARY KEY AUTOINCREMENT, "
379 + "name TEXT NOT NULL, card_ID TEXT NOT NULL);";
380
381 final Context context;
382 DatabaseHelper DBHelper;
383 SQLiteDatabase db;
384
385 public DBAdapter_ex_3(Context ctx) {
386 this.context = ctx;
387 DBHelper = new DatabaseHelper(context);
388 }
389
390
391 //Inner nested class (not anonymous)
392 private static class DatabaseHelper extends SQLiteOpenHelper {
393 DatabaseHelper(Context context) {
394 super(context, DATABASE_NAME, null, DATABASE_VERSION);
395 }
396 @Override
397 public void onCreate(SQLiteDatabase db) {
398 try {
399 db.execSQL(DATABASE_CREATE);
400 } catch (SQLException e) {
401 e.printStackTrace();
402 }
403 }
404 @Override
405 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
406 {
407 Log.w(LOG_TAG, "Upgrading DB from vs." + oldVersion + " to "
408 + newVersion + ", old data will be destroyed");
409 db.execSQL("DROP TABLE IF EXISTS contacts");
410 onCreate(db);
411 }
412 }// end inner class
413 // Open the db
414 public DBAdapter_ex_3 open() throws SQLException {
415 db = DBHelper.getWritableDatabase();
416 return this;
417 }
418 // Close the db
419 public void close() {
420 DBHelper.close();
421 }
422 // Insert a record to the contacts table
423 public long insertContact(String name, String card_id) {
424 ContentValues initialValues = new ContentValues();
425 initialValues.put(NAME, name);
426 initialValues.put(CARD_ID, card_id);
427 return db.insert(DATABASE_TABLE, null, initialValues);
428 }
429 // Delete a record from the table
430 public boolean deleteContact(long rowId) {
431 return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
432 }
433 // Retrieve all the contacts through a Cursor
434 public Cursor getAllContacts() {
435 return db.query(DATABASE_TABLE, new String[] { KEY_ROWID, NAME,
436 CARD_ID }, null, null, null, null, null);
437 }
438 // Return a String with all the data
439 public String getData() {
440 String[] columns = new String[] { KEY_ROWID, NAME, CARD_ID };
441 Cursor c = db.query(DATABASE_TABLE, columns, null, null, null, null,null);
442 String result = "";
443 int iRow = c.getColumnIndex(KEY_ROWID);
444 int iName = c.getColumnIndex(NAME);
445 int icard_id = c.getColumnIndex(CARD_ID);
446 for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
447 result = result + " " + c.getString(iRow) + " "
448 + c.getString(iName) + " " + c.getString(icard_id) + "\n";
449 }//end for
450 return result;
451 }
452 // Get a particular contact
453 public Cursor getContact(long rowId) throws SQLException {
454 Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {
455 KEY_ROWID, NAME, CARD_ID }, KEY_ROWID + "=" + rowId,
456 null, null, null, null, null);
457 if (mCursor != null) {
458 mCursor.moveToFirst();
459 }
460 return mCursor;
461 }
462 // check particular data by ID only
463 public boolean checkData(String card_id){
464
465 Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {
466 KEY_ROWID, NAME, CARD_ID }, CARD_ID + "="+ "'" + card_id+ "'",
467 null, null, null, null, null);
468
469 if (mCursor.moveToFirst() ) {
470 return true;
471 }
472
473 return false;
474
475 }
476
477 public boolean deleteContact(String card_id, String name) {
478
479 if(card_id.equals("")){
480
481 return db.delete(DATABASE_TABLE, NAME + "=" + "'" + name + "'", null) > 0;
482
483 }
484 else{
485
486 if(name.equals("")){
487 return db.delete(DATABASE_TABLE, CARD_ID + "=" + "'" + card_id+ "'", null) > 0;
488
489 }
490 else{
491
492 return db.delete(DATABASE_TABLE, CARD_ID + "=" + "'" + card_id + "'"+ " AND " + NAME + "="+ "'" + name+ "'" , null) > 0;
493 }
494
495 }
496
497 }
498
499 public boolean updateContact(String name, String card_id) {
500 ContentValues args = new ContentValues();
501 args.put(NAME, name);
502 args.put(CARD_ID, card_id);
503 return db.update(DATABASE_TABLE, args, NAME + "=" + "'"+ name+ "'", null) >0;
504 }
505
506 // Update a contact
507 public boolean updateContact(long rowId, String name, String card_id) {
508 ContentValues args = new ContentValues();
509 args.put(NAME, name);
510 args.put(CARD_ID, card_id);
511 return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) >
512 0;
513 }
514}// end DBAdapter
515
516---- Exercitiul 3 - Activity
517
518
519package com.example.Vladel.myapplication;
520
521import android.support.v7.app.AppCompatActivity;
522import android.os.Bundle;
523import android.util.Log;
524import android.view.View;
525import android.widget.Button;
526import android.widget.EditText;
527import android.widget.TextView;
528import android.widget.Toast;
529
530public class Ex_3_anctivity extends AppCompatActivity {
531
532 DBAdapter_ex_3 db;
533
534 Button addBtn,delBtn,editBtn,shAllBtn;
535 EditText Name,ID;
536 TextView scrView;
537 @Override
538 protected void onCreate(Bundle savedInstanceState) {
539
540 super.onCreate(savedInstanceState);
541 setContentView(R.layout.activity_ex_3_anctivity);
542
543 try {
544 // Start db connection
545 db = new DBAdapter_ex_3(this);
546
547 } catch (Exception e) {
548 Log.e("DB ERROR", e.toString());
549
550 e.printStackTrace();
551 }
552 Name = (EditText)findViewById(R.id.Name);
553 ID = (EditText)findViewById(R.id.ID);
554
555
556 scrView=findViewById(R.id.rows);
557
558 addBtn=(Button) findViewById(R.id.ADD);
559 delBtn=(Button) findViewById(R.id.Delete);
560 editBtn=(Button) findViewById(R.id.Edit);
561 shAllBtn=(Button) findViewById(R.id.ShowAll);
562
563 addBtn.setOnClickListener(new View.OnClickListener() {
564 @Override
565 public void onClick(View view) {
566 if (String.valueOf(Name.getText()).equals("") || String.valueOf(ID.getText()).equals("") ){
567 Toast.makeText(getApplicationContext(),"empty fields",Toast.LENGTH_LONG).show();
568 }
569 else {
570 try {
571 // Start db connection
572 db.open();
573 if (db.checkData(String.valueOf(ID.getText())) ){
574 Toast.makeText(getApplicationContext(),"Already exists",Toast.LENGTH_LONG).show();
575 }
576 else{
577 db.insertContact(String.valueOf(Name.getText()), String.valueOf(ID.getText()));
578 }
579 db.close();
580 } catch (Exception e) {
581 Log.e("DB ERROR", e.toString());
582 e.printStackTrace();
583 }
584 }
585
586
587 }
588 });
589
590
591 delBtn.setOnClickListener(new View.OnClickListener() {
592 @Override
593 public void onClick(View view) {
594 if (String.valueOf(Name.getText()).equals("") && String.valueOf(ID.getText()).equals("") ){
595 Toast.makeText(getApplicationContext(),"empty fields",Toast.LENGTH_LONG).show();
596 }
597 else {
598 try {
599 // Start db connection
600 db.open();
601 if (db.deleteContact( String.valueOf(ID.getText()),String.valueOf(Name.getText()) ) )
602 Toast.makeText(getApplicationContext(),"Delete Succesfull",Toast.LENGTH_LONG).show();
603 else
604 Toast.makeText(getApplicationContext(),"Delete Failed",Toast.LENGTH_LONG).show();
605 db.close();
606 } catch (Exception e) {
607 Log.e("DB ERROR", e.toString());
608 e.printStackTrace();
609 }
610 }
611
612
613 }
614 });
615
616 editBtn.setOnClickListener(new View.OnClickListener() {
617 @Override
618 public void onClick(View view) {
619 if (String.valueOf(Name.getText()).equals("") && String.valueOf(ID.getText()).equals("") ){
620 Toast.makeText(getApplicationContext(),"empty fields",Toast.LENGTH_LONG).show();
621 }
622 else {
623 try {
624 // Start db connection
625 db.open();
626 if (db.updateContact( String.valueOf(Name.getText()), String.valueOf(ID.getText()) ) )
627 Toast.makeText(getApplicationContext(),"Update Succesfull",Toast.LENGTH_LONG).show();
628 else
629 Toast.makeText(getApplicationContext(),"Update Failed",Toast.LENGTH_LONG).show();
630 db.close();
631 } catch (Exception e) {
632 Log.e("DB ERROR", e.toString());
633 e.printStackTrace();
634 }
635 }
636
637
638 }
639 });
640
641 shAllBtn.setOnClickListener(new View.OnClickListener() {
642 @Override
643 public void onClick(View view) {
644
645 try {
646 // Start db connection
647 db.open();
648 scrView.setText(db.getData());
649 db.close();
650 } catch (Exception e) {
651 Log.e("DB ERROR", e.toString());
652 e.printStackTrace();
653 }
654
655 }
656 });
657
658
659 }
660}