Advertisement
Guest User

Untitled

a guest
Jul 4th, 2014
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.63 KB | None | 0 0
  1. DataBaseHelper.java
  2.  
  3. package com.yourapp.yourapp;
  4.  
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.OutputStream;
  9. import android.content.ContentValues;
  10. import android.content.Context;
  11. import android.database.Cursor;
  12. import android.database.SQLException;
  13. import android.database.sqlite.SQLiteDatabase;
  14. import android.database.sqlite.SQLiteException;
  15. import android.database.sqlite.SQLiteOpenHelper;
  16.  
  17. public class DataBaseHelper extends SQLiteOpenHelper{
  18.  
  19. //The Android's default system path of your application database.
  20. private static String DB_PATH = "/data/data/com.yourapp.yourapp/databases/";
  21.  
  22. private static final String TAG = "DBAdapter";
  23.  
  24. private static final String DATABASE_NAME = "yourdb";
  25. private static final String DATABASE_TABLE = "yourtable";
  26. private static final int DATABASE_VERSION = 1;
  27.  
  28.  
  29. private SQLiteDatabase myDataBase;
  30.  
  31. private final Context myContext;
  32.  
  33. /**
  34. * Constructor
  35. * Takes and keeps a reference of the passed context in order to access to the application assets and resources.
  36. * @param context
  37. */
  38. public DataBaseHelper(Context context) {
  39.  
  40. super(context, DATABASE_NAME, null, 1);
  41. this.myContext = context;
  42. }
  43.  
  44. /**
  45. * Creates a empty database on the system and rewrites it with your own database.
  46. * */
  47. public void createDataBase() throws IOException{
  48.  
  49. boolean dbExist = checkDataBase();
  50. SQLiteDatabase db_Read = null;
  51.  
  52. if(dbExist){
  53. //do nothing - database already exist
  54. }else{
  55.  
  56. //By calling this method and empty database will be created into the default system path
  57. //of your application so we are gonna be able to overwrite that database with our database.
  58. // this.getReadableDatabase();
  59. db_Read = this.getReadableDatabase();
  60. db_Read.close();
  61.  
  62. try {
  63.  
  64. copyDataBase();
  65.  
  66. } catch (IOException e) {
  67.  
  68. throw new Error("Error copying database");
  69.  
  70. }
  71. }
  72.  
  73. }
  74.  
  75. /**
  76. * Check if the database already exist to avoid re-copying the file each time you open the application.
  77. * @return true if it exists, false if it doesn't
  78. */
  79. private boolean checkDataBase(){
  80.  
  81. SQLiteDatabase checkDB = null;
  82.  
  83. try{
  84. String myPath = DB_PATH + DATABASE_NAME;
  85. checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS|SQLiteDatabase.OPEN_READONLY);
  86.  
  87. }catch(SQLiteException e){
  88.  
  89. //database does't exist yet.
  90.  
  91. }
  92.  
  93. if(checkDB != null){
  94.  
  95. checkDB.close();
  96.  
  97. }
  98.  
  99. return checkDB != null ? true : false;
  100. }
  101.  
  102. /**
  103. * Copies your database from your local assets-folder to the just created empty database in the
  104. * system folder, from where it can be accessed and handled.
  105. * This is done by transferring bytestream.
  106. * */
  107. private void copyDataBase() throws IOException{
  108.  
  109. //Open your local db as the input stream
  110. InputStream myInput = myContext.getAssets().open(DATABASE_NAME);
  111.  
  112. // Path to the just created empty db
  113. String outFileName = DB_PATH + DATABASE_NAME;
  114.  
  115. //Open the empty db as the output stream
  116. OutputStream myOutput = new FileOutputStream(outFileName);
  117.  
  118. //transfer bytes from the inputfile to the outputfile
  119. byte[] buffer = new byte[1024];
  120. int length;
  121. while ((length = myInput.read(buffer))>0){
  122. myOutput.write(buffer, 0, length);
  123. }
  124.  
  125. //Close the streams
  126. myOutput.flush();
  127. myOutput.close();
  128. myInput.close();
  129.  
  130. }
  131.  
  132. public void openDataBase() throws SQLException{
  133.  
  134. //Open the database
  135. String myPath = DB_PATH + DATABASE_NAME;
  136. myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
  137.  
  138. }
  139.  
  140. public synchronized void close() {
  141.  
  142. if(myDataBase != null)
  143. myDataBase.close();
  144.  
  145. super.close();
  146.  
  147. }
  148.  
  149. public void onCreate(SQLiteDatabase db) {
  150.  
  151. }
  152.  
  153. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  154.  
  155. }
  156.  
  157.  
  158. public Cursor getAllRows()
  159. {
  160. return myDataBase.rawQuery("SELECT * FROM tablename", null);
  161. }
  162.  
  163. public Cursor getRowsByCategory(int x)
  164. {
  165. return myDataBase.rawQuery("SELECT * FROM tablename where categ_id=" + x , null);
  166. }
  167.  
  168. }
  169.  
  170.  
  171.  
  172. //-----------------------
  173.  
  174. YourActivity
  175.  
  176. //declarations:
  177. private Cursor c;
  178. private ArrayList<yourListItem> myList;
  179.  
  180. //inside onCreate()
  181.  
  182.  
  183.  
  184. final DataBaseHelper db = new DataBaseHelper(context);
  185. try {db.createDataBase();} catch (IOException e) {e.printStackTrace();}
  186. db.openDataBase();
  187.  
  188. c=db.getAllRows(); //or db.getRowsByCategory(yourint);
  189.  
  190. myList = new ArrayList<yourListItem>();
  191. c.moveToFIrst();
  192. do { myList.add(new yourListItem(c.getString(0), c.getString(2), c.getString(3)));
  193. } while (c.moveToNext());
  194.  
  195. mAdapter1 = new yourAdapter(this, getApplicationContext(), yourListItem, myList);
  196. myList.setAdapter(mAdapter1);
  197.  
  198.  
  199.  
  200. //--------------
  201.  
  202. yourListItem.java
  203.  
  204. public class yourListItem {
  205.  
  206. private String title;
  207. private String txt1;
  208. private String txt2;
  209.  
  210. public yourListItem(){}
  211.  
  212. public yourListItem(String title, String txt1, String txt2){
  213. this.title = title;
  214. this.txt1 = txt1;
  215. this.txt2 = txt2;
  216. }
  217.  
  218. public String getTitle(){
  219. return this.title;
  220. }
  221.  
  222. public String getTxt1(){
  223. return this.txt1;
  224. }
  225.  
  226. public void setTitle(String title){
  227. this.title = title;
  228. }
  229.  
  230. public void setTxt1(String txt1){
  231. this.txt1 = txt1;
  232. }
  233. //similarly set and get methods for txt2
  234. }
  235.  
  236.  
  237. //-----------------------
  238.  
  239. yourAdapter.java
  240.  
  241. import java.io.IOException;
  242. import java.util.ArrayList;
  243.  
  244. import android.app.Activity;
  245. import android.app.AlertDialog;
  246. import android.content.Context;
  247. import android.content.DialogInterface;
  248. import android.content.Intent;
  249. import android.graphics.Typeface;
  250. import android.view.LayoutInflater;
  251. import android.view.View;
  252. import android.view.View.OnClickListener;
  253. import android.view.ViewGroup;
  254. import android.widget.BaseAdapter;
  255. import android.widget.Button;
  256. import android.widget.TextView;
  257.  
  258. public class yourAdapter extends BaseAdapter {
  259.  
  260. private Context context;
  261. private Activity a;
  262. private ArrayList<yourListItem> newListItems;
  263.  
  264. public yourAdapter(Activity a, Context context, ArrayList<yourListItem> newListItems){
  265. this.context = context;
  266. this.a = (Activity)a;
  267. this.newListItems = newListItems;
  268. }
  269.  
  270. @Override
  271. public int getCount() {
  272. return newListItems.size();
  273. }
  274.  
  275. @Override
  276. public Object getItem(int position) {
  277. return newListItems.get(position);
  278. }
  279.  
  280. @Override
  281. public long getItemId(int position) {
  282. return position;
  283. }
  284.  
  285. @Override
  286. public View getView(int position, View convertView, ViewGroup parent) {
  287. if (convertView == null) {
  288. LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
  289. convertView = mInflater.inflate(R.layout.list_item_new, null);
  290. }
  291.  
  292.  
  293. TextView txt1 = (TextView) convertView.findViewById(R.id.txt1);
  294. TextView txtTitle = (TextView) convertView.findViewById(R.id.list_new_title);
  295. TextView txt2 = (TextView) convertView.findViewById(R.id.txt2);
  296.  
  297. txt1.setText(newListItems.get(position).getTxt1());
  298. txtTitle.setText(newListItems.get(position).getTitle());
  299. txt2.setText(newListItems.get(position).getTxt2());
  300.  
  301. return convertView;
  302. }
  303.  
  304.  
  305. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement