summaryrefslogtreecommitdiff
path: root/src/plugins/android.codeutils/resources/databaseDeploy/DatabaseListActivity.txt
blob: 17a017a43328cca4f89307b33db4294c5156dfc8 (plain)
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
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package #package_name#;

import android.app.Activity; 
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle; 
import android.view.Gravity;
import android.widget.TableLayout; 
import android.widget.TableRow; 
import android.widget.TextView;
import android.widget.Toast;

import #ManifestPackageName#.R;

public class #class_name# extends Activity {
	/* Called when the activity is first created. */

	private final int ONE = 1;
	private TableLayout tbl = null;
	private TableRow row = null;

	private android.widget.TableRow.LayoutParams rowParams = 
		new android.widget.TableRow.LayoutParams( 
				android.widget.TableRow.LayoutParams.FILL_PARENT, 
				android.widget.TableRow.LayoutParams.WRAP_CONTENT);

	private android.widget.TableRow.LayoutParams tableParams =
		new android.widget.TableRow.LayoutParams( 
				android.widget.TableRow.LayoutParams.FILL_PARENT, 
				android.widget.TableRow.LayoutParams.WRAP_CONTENT);

	//set database location
	private String DB_PATH = "/data/data/#package_name#/databases/";
	private String DB_NAME = "#dbName#";

	//column names
	#constColumnsNames#

	//table name
	private final String SQL_TABLE_NAME = "#tableNameLowerCase#";
	private boolean DISTINCT_ROWS = true;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.#layout_name#database_list/database_layout.xml#);

		tbl = (TableLayout) findViewById(R.id.myTableLayout);
		tbl.setBackgroundColor(Color.BLACK);
		
		

		SQLiteDatabase checkDB = null;

		try
		{
			String myPath = DB_PATH + DB_NAME;
			checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
			loadTable(checkDB);
		}
		catch(Exception e)
		{
			Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
		}

		if(checkDB != null)
		{
			checkDB.close();
		}
	}

	public void loadTable(SQLiteDatabase sqliteDatabase) {  
		Cursor cursor = sqliteDatabase.query(DISTINCT_ROWS, SQL_TABLE_NAME,  
				new String[] { #columsNames# } /*columns*/, COL_1 + "=" + COL_1 /*selection*/, 
				null /*selection args*/, null /*group by*/,  
				null /*having*/, null /*order by*/, null /*limit*/);  

		if (cursor != null) {  
			this.startManagingCursor(cursor);

			String[] columns = cursor.getColumnNames();
			createHeader(columns);

			while(cursor.move(ONE))
			{
				#columnGetValues#				

				TableRow row = new TableRow(getApplicationContext());
				row.setLayoutParams(rowParams);

				if(cursor.getPosition() % 2 == 1) {
					row.setBackgroundColor(Color.LTGRAY);
				}
				else {
					row.setBackgroundColor(Color.WHITE);
				}
				
				#columnAddRows#	

				tbl.addView(row, tableParams);
			}
		}  
	}

	//adds new value to row
	private void addToRow(Object obj, TableRow row)
	{
		TextView txt = new TextView(this);
		if(obj != null)
			txt.setText(obj.toString());
		else
			txt.setText("");
		txt.setLayoutParams(rowParams);
		txt.setTextColor(Color.BLACK);
		txt.setGravity(Gravity.RIGHT);
		txt.setPadding(5, 1, 1, 1);
		row.addView(txt);
	}
	
	//creates table header
	private void createHeader(String[] names)
	{
		row = new TableRow(getApplicationContext());
		row.setLayoutParams(rowParams); 
		row.setBackgroundColor(Color.GRAY);

		for(String column : names)
		{
			TextView txt = new TextView(this);
			txt.setText(column.toUpperCase());
			txt.setLayoutParams(rowParams);
			txt.setWidth(80);
			txt.setTextColor(Color.BLACK);
			txt.setTypeface(Typeface.DEFAULT_BOLD);
			txt.setGravity(Gravity.CENTER);
			//adds new field to row
			row.addView(txt);
		}
		
		//adds row to table
		tbl.addView(row, tableParams);
	}
	
	/**
	 * 
	 */	
	private boolean createDatabase() {		
		#sqlOpenHelperName# helper = new #sqlOpenHelperName#(getApplicationContext());
		try {
			helper.createDataBase();
		} catch (IOException ioe) {
			throw new Error("Unable to create database");
		}
		try {
			helper.openDataBase();
		} catch (SQLException sqle) {
			throw sqle;
		}
		return false;
	}
}