-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathAndroidDatabaseManager.java
1290 lines (1068 loc) · 49.9 KB
/
AndroidDatabaseManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//add your package name here example: package com.example.dbm;
//all required import files
import java.util.ArrayList;
import java.util.LinkedList;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;
public class AndroidDatabaseManager extends Activity implements OnItemClickListener {
//a static class to save cursor,table values etc which is used by functions to share data in the program.
static class indexInfo
{
public static int index = 10;
public static int numberofpages = 0;
public static int currentpage = 0;
public static String table_name="";
public static Cursor maincursor;
public static int cursorpostion=0;
public static ArrayList<String> value_string;
public static ArrayList<String> tableheadernames;
public static ArrayList<String> emptytablecolumnnames;
public static boolean isEmpty;
public static boolean isCustomQuery;
}
// all global variables
//in the below line Change the text 'yourCustomSqlHelper' with your custom sqlitehelper class name.
//Do not change the variable name dbm
yourCustomSqlLiteHelperclass dbm;
TableLayout tableLayout;
TableRow.LayoutParams tableRowParams;
HorizontalScrollView hsv;
ScrollView mainscrollview;
LinearLayout mainLayout;
TextView tvmessage;
Button previous;
Button next;
Spinner select_table;
TextView tv;
indexInfo info = new indexInfo();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//in the below line Change the text 'yourCustomSqlHelper' with your custom sqlitehelper class name
dbm = new yourCustomSqlLiteHelperclass(AndroidDatabaseManager.this);
mainscrollview = new ScrollView(AndroidDatabaseManager.this);
//the main linear layout to which all tables spinners etc will be added.In this activity every element is created dynamically to avoid using xml file
mainLayout = new LinearLayout(AndroidDatabaseManager.this);
mainLayout.setOrientation(LinearLayout.VERTICAL);
mainLayout.setBackgroundColor(Color.WHITE);
mainLayout.setScrollContainer(true);
mainscrollview.addView(mainLayout);
//all required layouts are created dynamically and added to the main scrollview
setContentView(mainscrollview);
//the first row of layout which has a text view and spinner
final LinearLayout firstrow = new LinearLayout(AndroidDatabaseManager.this);
firstrow.setPadding(0,10,0,20);
LinearLayout.LayoutParams firstrowlp = new LinearLayout.LayoutParams(0, 150);
firstrowlp.weight = 1;
TextView maintext = new TextView(AndroidDatabaseManager.this);
maintext.setText("Select Table");
maintext.setTextSize(22);
maintext.setLayoutParams(firstrowlp);
select_table=new Spinner(AndroidDatabaseManager.this);
select_table.setLayoutParams(firstrowlp);
firstrow.addView(maintext);
firstrow.addView(select_table);
mainLayout.addView(firstrow);
ArrayList<Cursor> alc ;
//the horizontal scroll view for table if the table content doesnot fit into screen
hsv = new HorizontalScrollView(AndroidDatabaseManager.this);
//the main table layout where the content of the sql tables will be displayed when user selects a table
tableLayout = new TableLayout(AndroidDatabaseManager.this);
tableLayout.setHorizontalScrollBarEnabled(true);
hsv.addView(tableLayout);
//the second row of the layout which shows number of records in the table selected by user
final LinearLayout secondrow = new LinearLayout(AndroidDatabaseManager.this);
secondrow.setPadding(0,20,0,10);
LinearLayout.LayoutParams secondrowlp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
secondrowlp.weight = 1;
TextView secondrowtext = new TextView(AndroidDatabaseManager.this);
secondrowtext.setText("No. Of Records : ");
secondrowtext.setTextSize(20);
secondrowtext.setLayoutParams(secondrowlp);
tv =new TextView(AndroidDatabaseManager.this);
tv.setTextSize(20);
tv.setLayoutParams(secondrowlp);
secondrow.addView(secondrowtext);
secondrow.addView(tv);
mainLayout.addView(secondrow);
//A button which generates a text view from which user can write custome queries
final EditText customquerytext = new EditText(this);
customquerytext.setVisibility(View.GONE);
customquerytext.setHint("Enter Your Query here and Click on Submit Query Button .Results will be displayed below");
mainLayout.addView(customquerytext);
final Button submitQuery = new Button(AndroidDatabaseManager.this);
submitQuery.setVisibility(View.GONE);
submitQuery.setText("Submit Query");
submitQuery.setBackgroundColor(Color.parseColor("#BAE7F6"));
mainLayout.addView(submitQuery);
final TextView help = new TextView(AndroidDatabaseManager.this);
help.setText("Click on the row below to update values or delete the tuple");
help.setPadding(0,5,0,5);
// the spinner which gives user a option to add new row , drop or delete table
final Spinner spinnertable =new Spinner(AndroidDatabaseManager.this);
mainLayout.addView(spinnertable);
mainLayout.addView(help);
hsv.setPadding(0,10,0,10);
hsv.setScrollbarFadingEnabled(false);
hsv.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_INSET);
mainLayout.addView(hsv);
//the third layout which has buttons for the pagination of content from database
final LinearLayout thirdrow = new LinearLayout(AndroidDatabaseManager.this);
previous = new Button(AndroidDatabaseManager.this);
previous.setText("Previous");
previous.setBackgroundColor(Color.parseColor("#BAE7F6"));
previous.setLayoutParams(secondrowlp);
next = new Button(AndroidDatabaseManager.this);
next.setText("Next");
next.setBackgroundColor(Color.parseColor("#BAE7F6"));
next.setLayoutParams(secondrowlp);
TextView tvblank = new TextView(this);
tvblank.setLayoutParams(secondrowlp);
thirdrow.setPadding(0,10,0,10);
thirdrow.addView(previous);
thirdrow.addView(tvblank);
thirdrow.addView(next);
mainLayout.addView(thirdrow);
//the text view at the bottom of the screen which displays error or success messages after a query is executed
tvmessage =new TextView(AndroidDatabaseManager.this);
tvmessage.setText("Error Messages will be displayed here");
String Query = "SELECT name _id FROM sqlite_master WHERE type ='table'";
tvmessage.setTextSize(18);
mainLayout.addView(tvmessage);
final Button customQuery = new Button(AndroidDatabaseManager.this);
customQuery.setText("Custom Query");
customQuery.setBackgroundColor(Color.parseColor("#BAE7F6"));
mainLayout.addView(customQuery);
customQuery.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//set drop down to custom Query
indexInfo.isCustomQuery=true;
secondrow.setVisibility(View.GONE);
spinnertable.setVisibility(View.GONE);
help.setVisibility(View.GONE);
customquerytext.setVisibility(View.VISIBLE);
submitQuery.setVisibility(View.VISIBLE);
select_table.setSelection(0);
customQuery.setVisibility(View.GONE);
}
});
//when user enter a custom query in text view and clicks on submit query button
//display results in tablelayout
submitQuery.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
tableLayout.removeAllViews();
customQuery.setVisibility(View.GONE);
ArrayList<Cursor> alc2;
String Query10=customquerytext.getText().toString();
Log.d("query",Query10);
//pass the query to getdata method and get results
alc2 = dbm.getData(Query10);
final Cursor c4=alc2.get(0);
Cursor Message2 =alc2.get(1);
Message2.moveToLast();
//if the query returns results display the results in table layout
if(Message2.getString(0).equalsIgnoreCase("Success"))
{
tvmessage.setBackgroundColor(Color.parseColor("#2ecc71"));
if(c4!=null){
tvmessage.setText("Queru Executed successfully.Number of rows returned :"+c4.getCount());
if(c4.getCount()>0)
{
indexInfo.maincursor=c4;
refreshTable(1);
}
}else{
tvmessage.setText("Queru Executed successfully");
refreshTable(1);
}
}
else
{
//if there is any error we displayed the error message at the bottom of the screen
tvmessage.setBackgroundColor(Color.parseColor("#e74c3c"));
tvmessage.setText("Error:"+Message2.getString(0));
}
}
});
//layout parameters for each row in the table
tableRowParams = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
tableRowParams.setMargins(0, 0, 2, 0);
// a query which returns a cursor with the list of tables in the database.We use this cursor to populate spinner in the first row
alc = dbm.getData(Query);
//the first cursor has reults of the query
final Cursor c=alc.get(0);
//the second cursor has error messages
Cursor Message =alc.get(1);
Message.moveToLast();
String msg = Message.getString(0);
Log.d("Message from sql = ",msg);
ArrayList<String> tablenames = new ArrayList<String>();
if(c!=null)
{
c.moveToFirst();
tablenames.add("click here");
do{
//add names of the table to tablenames array list
tablenames.add(c.getString(0));
}while(c.moveToNext());
}
//an array adapter with above created arraylist
ArrayAdapter<String> tablenamesadapter = new ArrayAdapter<String>(AndroidDatabaseManager.this,
android.R.layout.simple_spinner_item, tablenames) {
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
v.setBackgroundColor(Color.WHITE);
TextView adap =(TextView)v;
adap.setTextSize(20);
return adap;
}
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View v =super.getDropDownView(position, convertView, parent);
v.setBackgroundColor(Color.WHITE);
return v;
}
};
tablenamesadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
if(tablenamesadapter!=null)
{
//set the adpater to select_table spinner
select_table.setAdapter(tablenamesadapter);
}
// when a table names is selecte display the table contents
select_table.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
if(pos==0&&!indexInfo.isCustomQuery)
{
secondrow.setVisibility(View.GONE);
hsv.setVisibility(View.GONE);
thirdrow.setVisibility(View.GONE);
spinnertable.setVisibility(View.GONE);
help.setVisibility(View.GONE);
tvmessage.setVisibility(View.GONE);
customquerytext.setVisibility(View.GONE);
submitQuery.setVisibility(View.GONE);
customQuery.setVisibility(View.GONE);
}
if(pos!=0){
secondrow.setVisibility(View.VISIBLE);
spinnertable.setVisibility(View.VISIBLE);
help.setVisibility(View.VISIBLE);
customquerytext.setVisibility(View.GONE);
submitQuery.setVisibility(View.GONE);
customQuery.setVisibility(View.VISIBLE);
hsv.setVisibility(View.VISIBLE);
tvmessage.setVisibility(View.VISIBLE);
thirdrow.setVisibility(View.VISIBLE);
c.moveToPosition(pos-1);
indexInfo.cursorpostion=pos-1;
//displaying the content of the table which is selected in the select_table spinner
Log.d("selected table name is",""+c.getString(0));
indexInfo.table_name=c.getString(0);
tvmessage.setText("Error Messages will be displayed here");
tvmessage.setBackgroundColor(Color.WHITE);
//removes any data if present in the table layout
tableLayout.removeAllViews();
ArrayList<String> spinnertablevalues = new ArrayList<String>();
spinnertablevalues.add("Click here to change this table");
spinnertablevalues.add("Add row to this table");
spinnertablevalues.add("Delete this table");
spinnertablevalues.add("Drop this table");
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, spinnertablevalues);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
// a array adapter which add values to the spinner which helps in user making changes to the table
ArrayAdapter<String> adapter = new ArrayAdapter<String>(AndroidDatabaseManager.this,
android.R.layout.simple_spinner_item, spinnertablevalues) {
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
v.setBackgroundColor(Color.WHITE);
TextView adap =(TextView)v;
adap.setTextSize(20);
return adap;
}
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View v =super.getDropDownView(position, convertView, parent);
v.setBackgroundColor(Color.WHITE);
return v;
}
};
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnertable.setAdapter(adapter);
String Query2 ="select * from "+c.getString(0);
Log.d("",""+Query2);
//getting contents of the table which user selected from the select_table spinner
ArrayList<Cursor> alc2=dbm.getData(Query2);
final Cursor c2=alc2.get(0);
//saving cursor to the static indexinfo class which can be resued by the other functions
indexInfo.maincursor=c2;
// if the cursor returned form the database is not null we display the data in table layout
if(c2!=null)
{
int counts = c2.getCount();
indexInfo.isEmpty=false;
Log.d("counts",""+counts);
tv.setText(""+counts);
//the spinnertable has the 3 items to drop , delete , add row to the table selected by the user
//here we handle the 3 operations.
spinnertable.setOnItemSelectedListener((new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
((TextView)parentView.getChildAt(0)).setTextColor(Color.rgb(0,0,0));
//when user selects to drop the table the below code in if block will be executed
if(spinnertable.getSelectedItem().toString().equals("Drop this table"))
{
// an alert dialog to confirm user selection
runOnUiThread(new Runnable() {
@Override
public void run() {
if(!isFinishing()){
new AlertDialog.Builder(AndroidDatabaseManager.this)
.setTitle("Are you sure ?")
.setMessage("Pressing yes will remove "+indexInfo.table_name+" table from database")
.setPositiveButton("yes",
new DialogInterface.OnClickListener() {
// when user confirms by clicking on yes we drop the table by executing drop table query
public void onClick(DialogInterface dialog, int which) {
String Query6 = "Drop table "+indexInfo.table_name;
ArrayList<Cursor> aldropt=dbm.getData(Query6);
Cursor tempc=aldropt.get(1);
tempc.moveToLast();
Log.d("Drop table Mesage",tempc.getString(0));
if(tempc.getString(0).equalsIgnoreCase("Success"))
{
tvmessage.setBackgroundColor(Color.parseColor("#2ecc71"));
tvmessage.setText(indexInfo.table_name+"Dropped successfully");
refreshactivity();
}
else
{
//if there is any error we displayd the error message at the bottom of the screen
tvmessage.setBackgroundColor(Color.parseColor("#e74c3c"));
tvmessage.setText("Error:"+tempc.getString(0));
spinnertable.setSelection(0);
}
}})
.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
spinnertable.setSelection(0);
}
})
.create().show();
}
}
});
}
//when user selects to drop the table the below code in if block will be executed
if(spinnertable.getSelectedItem().toString().equals("Delete this table"))
{ // an alert dialog to confirm user selection
runOnUiThread(new Runnable() {
@Override
public void run() {
if(!isFinishing()){
new AlertDialog.Builder(AndroidDatabaseManager.this)
.setTitle("Are you sure?")
.setMessage("Clicking on yes will delete all the contents of "+indexInfo.table_name+" table from database")
.setPositiveButton("yes",
new DialogInterface.OnClickListener() {
// when user confirms by clicking on yes we drop the table by executing delete table query
public void onClick(DialogInterface dialog, int which) {
String Query7 = "Delete from "+indexInfo.table_name;
Log.d("delete table query",Query7);
ArrayList<Cursor> aldeletet=dbm.getData(Query7);
Cursor tempc=aldeletet.get(1);
tempc.moveToLast();
Log.d("Delete table Mesage",tempc.getString(0));
if(tempc.getString(0).equalsIgnoreCase("Success"))
{
tvmessage.setBackgroundColor(Color.parseColor("#2ecc71"));
tvmessage.setText(indexInfo.table_name+" table content deleted successfully");
indexInfo.isEmpty=true;
refreshTable(0);
}
else
{
tvmessage.setBackgroundColor(Color.parseColor("#e74c3c"));
tvmessage.setText("Error:"+tempc.getString(0));
spinnertable.setSelection(0);
}
}})
.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
spinnertable.setSelection(0);
}
})
.create().show();
}
}
});
}
//when user selects to add row to the table the below code in if block will be executed
if(spinnertable.getSelectedItem().toString().equals("Add row to this table"))
{
//we create a layout which has textviews with column names of the table and edittexts where
//user can enter value which will be inserted into the datbase.
final LinkedList<TextView> addnewrownames = new LinkedList<TextView>();
final LinkedList<EditText> addnewrowvalues = new LinkedList<EditText>();
final ScrollView addrowsv =new ScrollView(AndroidDatabaseManager.this);
Cursor c4 = indexInfo.maincursor;
if(indexInfo.isEmpty)
{
getcolumnnames();
for(int i=0;i<indexInfo.emptytablecolumnnames.size();i++)
{
String cname = indexInfo.emptytablecolumnnames.get(i);
TextView tv = new TextView(getApplicationContext());
tv.setText(cname);
addnewrownames.add(tv);
}
for(int i=0;i<addnewrownames.size();i++)
{
EditText et = new EditText(getApplicationContext());
addnewrowvalues.add(et);
}
}
else{
for(int i=0;i<c4.getColumnCount();i++)
{
String cname = c4.getColumnName(i);
TextView tv = new TextView(getApplicationContext());
tv.setText(cname);
addnewrownames.add(tv);
}
for(int i=0;i<addnewrownames.size();i++)
{
EditText et = new EditText(getApplicationContext());
addnewrowvalues.add(et);
}
}
final RelativeLayout addnewlayout = new RelativeLayout(AndroidDatabaseManager.this);
RelativeLayout.LayoutParams addnewparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
addnewparams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
for(int i=0;i<addnewrownames.size();i++)
{
TextView tv =addnewrownames.get(i);
EditText et=addnewrowvalues.get(i);
int t = i+400;
int k = i+500;
int lid = i+600;
tv.setId(t);
tv.setTextColor(Color.parseColor("#000000"));
et.setBackgroundColor(Color.parseColor("#F2F2F2"));
et.setTextColor(Color.parseColor("#000000"));
et.setId(k);
final LinearLayout ll = new LinearLayout(AndroidDatabaseManager.this);
LinearLayout.LayoutParams tvl = new LinearLayout.LayoutParams(0, 100);
tvl.weight = 1;
ll.addView(tv,tvl);
ll.addView(et,tvl);
ll.setId(lid);
Log.d("Edit Text Value",""+et.getText().toString());
RelativeLayout.LayoutParams rll = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
rll.addRule(RelativeLayout.BELOW,ll.getId()-1 );
rll.setMargins(0, 20, 0, 0);
addnewlayout.addView(ll, rll);
}
addnewlayout.setBackgroundColor(Color.WHITE);
addrowsv.addView(addnewlayout);
Log.d("Button Clicked", "");
//the above form layout which we have created above will be displayed in an alert dialog
runOnUiThread(new Runnable() {
@Override
public void run() {
if(!isFinishing()){
new AlertDialog.Builder(AndroidDatabaseManager.this)
.setTitle("values")
.setCancelable(false)
.setView(addrowsv)
.setPositiveButton("Add",
new DialogInterface.OnClickListener() {
// after entering values if user clicks on add we take the values and run a insert query
public void onClick(DialogInterface dialog, int which) {
indexInfo.index = 10;
//tableLayout.removeAllViews();
//trigger select table listener to be triggerd
String Query4 ="Insert into "+indexInfo.table_name+" (";
for(int i=0 ; i<addnewrownames.size();i++)
{
TextView tv = addnewrownames.get(i);
tv.getText().toString();
if(i==addnewrownames.size()-1)
{
Query4=Query4+tv.getText().toString();
}
else
{
Query4=Query4+tv.getText().toString()+", ";
}
}
Query4=Query4+" ) VALUES ( ";
for(int i=0 ; i<addnewrownames.size();i++)
{
EditText et = addnewrowvalues.get(i);
et.getText().toString();
if(i==addnewrownames.size()-1)
{
Query4=Query4+"'"+et.getText().toString()+"' ) ";
}
else
{
Query4=Query4+"'"+et.getText().toString()+"' , ";
}
}
//this is the insert query which has been generated
Log.d("Insert Query",Query4);
ArrayList<Cursor> altc=dbm.getData(Query4);
Cursor tempc=altc.get(1);
tempc.moveToLast();
Log.d("Add New Row",tempc.getString(0));
if(tempc.getString(0).equalsIgnoreCase("Success"))
{
tvmessage.setBackgroundColor(Color.parseColor("#2ecc71"));
tvmessage.setText("New Row added succesfully to "+indexInfo.table_name);
refreshTable(0);
}
else
{
tvmessage.setBackgroundColor(Color.parseColor("#e74c3c"));
tvmessage.setText("Error:"+tempc.getString(0));
spinnertable.setSelection(0);
}
}
})
.setNegativeButton("close",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
spinnertable.setSelection(0);
}
})
.create().show();
}
}
});
}
}
public void onNothingSelected(AdapterView<?> arg0) { }
}));
//display the first row of the table with column names of the table selected by the user
TableRow tableheader = new TableRow(getApplicationContext());
tableheader.setBackgroundColor(Color.BLACK);
tableheader.setPadding(0, 2, 0, 2);
for(int k=0;k<c2.getColumnCount();k++)
{
LinearLayout cell = new LinearLayout(AndroidDatabaseManager.this);
cell.setBackgroundColor(Color.WHITE);
cell.setLayoutParams(tableRowParams);
final TextView tableheadercolums = new TextView(getApplicationContext());
// tableheadercolums.setBackgroundDrawable(gd);
tableheadercolums.setPadding(0, 0, 4, 3);
tableheadercolums.setText(""+c2.getColumnName(k));
tableheadercolums.setTextColor(Color.parseColor("#000000"));
//columsView.setLayoutParams(tableRowParams);
cell.addView(tableheadercolums);
tableheader.addView(cell);
}
tableLayout.addView(tableheader);
c2.moveToFirst();
//after displaying columnnames in the first row we display data in the remaining columns
//the below paginatetbale function will display the first 10 tuples of the tables
//the remaining tuples can be viewed by clicking on the next button
paginatetable(c2.getCount());
}
else{
//if the cursor returned from the database is empty we show that table is empty
help.setVisibility(View.GONE);
tableLayout.removeAllViews();
getcolumnnames();
TableRow tableheader2 = new TableRow(getApplicationContext());
tableheader2.setBackgroundColor(Color.BLACK);
tableheader2.setPadding(0, 2, 0, 2);
LinearLayout cell = new LinearLayout(AndroidDatabaseManager.this);
cell.setBackgroundColor(Color.WHITE);
cell.setLayoutParams(tableRowParams);
final TextView tableheadercolums = new TextView(getApplicationContext());
tableheadercolums.setPadding(0, 0, 4, 3);
tableheadercolums.setText(" Table Is Empty ");
tableheadercolums.setTextSize(30);
tableheadercolums.setTextColor(Color.RED);
cell.addView(tableheadercolums);
tableheader2.addView(cell);
tableLayout.addView(tableheader2);
tv.setText(""+0);
}
}}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
//get columnnames of the empty tables and save them in a array list
public void getcolumnnames()
{
ArrayList<Cursor> alc3=dbm.getData("PRAGMA table_info("+indexInfo.table_name+")");
Cursor c5=alc3.get(0);
indexInfo.isEmpty=true;
if(c5!=null)
{
indexInfo.isEmpty=true;
ArrayList<String> emptytablecolumnnames= new ArrayList<String>();
c5.moveToFirst();
do
{
emptytablecolumnnames.add(c5.getString(1));
}while(c5.moveToNext());
indexInfo.emptytablecolumnnames=emptytablecolumnnames;
}
}
//displays alert dialog from which use can update or delete a row
public void updateDeletePopup(int row)
{
Cursor c2=indexInfo.maincursor;
// a spinner which gives options to update or delete the row which user has selected
ArrayList<String> spinnerArray = new ArrayList<String>();
spinnerArray.add("Click Here to Change this row");
spinnerArray.add("Update this row");
spinnerArray.add("Delete this row");
//create a layout with text values which has the column names and
//edit texts which has the values of the row which user has selected
final ArrayList<String> value_string = indexInfo.value_string;
final LinkedList<TextView> columnames = new LinkedList<TextView>();
final LinkedList<EditText> columvalues = new LinkedList<EditText>();
for(int i=0;i<c2.getColumnCount();i++)
{
String cname = c2.getColumnName(i);
TextView tv = new TextView(getApplicationContext());
tv.setText(cname);
columnames.add(tv);
}
for(int i=0;i<columnames.size();i++)
{
String cv =value_string.get(i);
EditText et = new EditText(getApplicationContext());
value_string.add(cv);
et.setText(cv);
columvalues.add(et);
}
int lastrid = 0;
// all text views , edit texts are added to this relative layout lp
final RelativeLayout lp = new RelativeLayout(AndroidDatabaseManager.this);
lp.setBackgroundColor(Color.WHITE);
RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lay.addRule(RelativeLayout.ALIGN_PARENT_TOP);
final ScrollView updaterowsv =new ScrollView(AndroidDatabaseManager.this);
LinearLayout lcrud = new LinearLayout(AndroidDatabaseManager.this);
LinearLayout.LayoutParams paramcrudtext = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
paramcrudtext.setMargins(0, 20, 0, 0);
//spinner which displays update , delete options
final Spinner crud_dropdown = new Spinner(getApplicationContext());
ArrayAdapter<String> crudadapter = new ArrayAdapter<String>(AndroidDatabaseManager.this,
android.R.layout.simple_spinner_item, spinnerArray) {
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
v.setBackgroundColor(Color.WHITE);
TextView adap =(TextView)v;
adap.setTextSize(20);
return adap;
}
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View v =super.getDropDownView(position, convertView, parent);
v.setBackgroundColor(Color.WHITE);
return v;
}
};
crudadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
crud_dropdown.setAdapter(crudadapter);
lcrud.setId(299);
lcrud.addView(crud_dropdown,paramcrudtext);
RelativeLayout.LayoutParams rlcrudparam = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
rlcrudparam.addRule(RelativeLayout.BELOW,lastrid);
lp.addView(lcrud, rlcrudparam);
for(int i=0;i<columnames.size();i++)
{
TextView tv =columnames.get(i);
EditText et=columvalues.get(i);
int t = i+100;
int k = i+200;
int lid = i+300;
tv.setId(t);
tv.setTextColor(Color.parseColor("#000000"));
et.setBackgroundColor(Color.parseColor("#F2F2F2"));
et.setTextColor(Color.parseColor("#000000"));
et.setId(k);
Log.d("text View Value",""+tv.getText().toString());
final LinearLayout ll = new LinearLayout(AndroidDatabaseManager.this);
ll.setBackgroundColor(Color.parseColor("#FFFFFF"));
ll.setId(lid);
LinearLayout.LayoutParams lpp = new LinearLayout.LayoutParams(0, 100);
lpp.weight = 1;
tv.setLayoutParams(lpp);
et.setLayoutParams(lpp);
ll.addView(tv);
ll.addView(et);
Log.d("Edit Text Value",""+et.getText().toString());
RelativeLayout.LayoutParams rll = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
rll.addRule(RelativeLayout.BELOW,ll.getId()-1 );
rll.setMargins(0, 20, 0, 0);
lastrid=ll.getId();
lp.addView(ll, rll);
}
updaterowsv.addView(lp);
//after the layout has been created display it in a alert dialog
runOnUiThread(new Runnable() {
@Override
public void run() {
if(!isFinishing()){
new AlertDialog.Builder(AndroidDatabaseManager.this)
.setTitle("values")
.setView(updaterowsv)
.setCancelable(false)
.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
//this code will be executed when user changes values of edit text or spinner and clicks on ok button
public void onClick(DialogInterface dialog, int which) {
//get spinner value
String spinner_value = crud_dropdown.getSelectedItem().toString();
//it he spinner value is update this row get the values from
//edit text fields generate a update query and execute it
if(spinner_value.equalsIgnoreCase("Update this row"))
{
indexInfo.index = 10;
String Query3="UPDATE "+indexInfo.table_name+" SET ";
for(int i=0;i<columnames.size();i++)
{
TextView tvc = columnames.get(i);
EditText etc = columvalues.get(i);
if(!etc.getText().toString().equals("null"))
{
Query3=Query3+tvc.getText().toString()+" = ";
if(i==columnames.size()-1)
{
Query3=Query3+"'"+etc.getText().toString()+"'";
}
else{
Query3=Query3+"'"+etc.getText().toString()+"' , ";
}
}
}
Query3=Query3+" where ";
for(int i=0;i<columnames.size();i++)
{
TextView tvc = columnames.get(i);
if(!value_string.get(i).equals("null"))
{
Query3=Query3+tvc.getText().toString()+" = ";
if(i==columnames.size()-1)
{
Query3=Query3+"'"+value_string.get(i)+"' ";
}
else
{
Query3=Query3+"'"+value_string.get(i)+"' and ";
}
}
}
Log.d("Update Query",Query3);
//dbm.getData(Query3);
ArrayList<Cursor> aluc=dbm.getData(Query3);
Cursor tempc=aluc.get(1);
tempc.moveToLast();
Log.d("Update Mesage",tempc.getString(0));
if(tempc.getString(0).equalsIgnoreCase("Success"))
{
tvmessage.setBackgroundColor(Color.parseColor("#2ecc71"));
tvmessage.setText(indexInfo.table_name+" table Updated Successfully");
refreshTable(0);
}
else
{
tvmessage.setBackgroundColor(Color.parseColor("#e74c3c"));
tvmessage.setText("Error:"+tempc.getString(0));
}
}
//it he spinner value is delete this row get the values from
//edit text fields generate a delete query and execute it
if(spinner_value.equalsIgnoreCase("Delete this row"))
{
indexInfo.index = 10;
String Query5="DELETE FROM "+indexInfo.table_name+" WHERE ";
for(int i=0;i<columnames.size();i++)
{
TextView tvc = columnames.get(i);
if(!value_string.get(i).equals("null"))
{
Query5=Query5+tvc.getText().toString()+" = ";
if(i==columnames.size()-1)
{
Query5=Query5+"'"+value_string.get(i)+"' ";
}
else
{
Query5=Query5+"'"+value_string.get(i)+"' and ";
}
}
}
Log.d("Delete Query",Query5);
dbm.getData(Query5);