Advertisement
Guest User

Untitled

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