Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- DataBaseHelper.java
- package com.yourapp.yourapp;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.SQLException;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteException;
- import android.database.sqlite.SQLiteOpenHelper;
- public class DataBaseHelper extends SQLiteOpenHelper{
- //The Android's default system path of your application database.
- private static String DB_PATH = "/data/data/com.yourapp.yourapp/databases/";
- private static final String TAG = "DBAdapter";
- private static final String DATABASE_NAME = "yourdb";
- private static final String DATABASE_TABLE = "yourtable";
- private static final int DATABASE_VERSION = 1;
- private SQLiteDatabase myDataBase;
- private final Context myContext;
- /**
- * Constructor
- * Takes and keeps a reference of the passed context in order to access to the application assets and resources.
- * @param context
- */
- public DataBaseHelper(Context context) {
- super(context, DATABASE_NAME, null, 1);
- this.myContext = context;
- }
- /**
- * Creates a empty database on the system and rewrites it with your own database.
- * */
- public void createDataBase() throws IOException{
- boolean dbExist = checkDataBase();
- SQLiteDatabase db_Read = null;
- if(dbExist){
- //do nothing - database already exist
- }else{
- //By calling this method and empty database will be created into the default system path
- //of your application so we are gonna be able to overwrite that database with our database.
- // this.getReadableDatabase();
- db_Read = this.getReadableDatabase();
- db_Read.close();
- try {
- copyDataBase();
- } catch (IOException e) {
- throw new Error("Error copying database");
- }
- }
- }
- /**
- * Check if the database already exist to avoid re-copying the file each time you open the application.
- * @return true if it exists, false if it doesn't
- */
- private boolean checkDataBase(){
- SQLiteDatabase checkDB = null;
- try{
- String myPath = DB_PATH + DATABASE_NAME;
- checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS|SQLiteDatabase.OPEN_READONLY);
- }catch(SQLiteException e){
- //database does't exist yet.
- }
- if(checkDB != null){
- checkDB.close();
- }
- return checkDB != null ? true : false;
- }
- /**
- * Copies your database from your local assets-folder to the just created empty database in the
- * system folder, from where it can be accessed and handled.
- * This is done by transferring bytestream.
- * */
- private void copyDataBase() throws IOException{
- //Open your local db as the input stream
- InputStream myInput = myContext.getAssets().open(DATABASE_NAME);
- // Path to the just created empty db
- String outFileName = DB_PATH + DATABASE_NAME;
- //Open the empty db as the output stream
- OutputStream myOutput = new FileOutputStream(outFileName);
- //transfer bytes from the inputfile to the outputfile
- byte[] buffer = new byte[1024];
- int length;
- while ((length = myInput.read(buffer))>0){
- myOutput.write(buffer, 0, length);
- }
- //Close the streams
- myOutput.flush();
- myOutput.close();
- myInput.close();
- }
- public void openDataBase() throws SQLException{
- //Open the database
- String myPath = DB_PATH + DATABASE_NAME;
- myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
- }
- public synchronized void close() {
- if(myDataBase != null)
- myDataBase.close();
- super.close();
- }
- public void onCreate(SQLiteDatabase db) {
- }
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- }
- public Cursor getAllRows()
- {
- return myDataBase.rawQuery("SELECT * FROM tablename", null);
- }
- public Cursor getRowsByCategory(int x)
- {
- return myDataBase.rawQuery("SELECT * FROM tablename where categ_id=" + x , null);
- }
- }
- //-----------------------
- YourActivity
- //declarations:
- private Cursor c;
- private ArrayList<yourListItem> myList;
- //inside onCreate()
- final DataBaseHelper db = new DataBaseHelper(context);
- try {db.createDataBase();} catch (IOException e) {e.printStackTrace();}
- db.openDataBase();
- c=db.getAllRows(); //or db.getRowsByCategory(yourint);
- myList = new ArrayList<yourListItem>();
- c.moveToFIrst();
- do { myList.add(new yourListItem(c.getString(0), c.getString(2), c.getString(3)));
- } while (c.moveToNext());
- mAdapter1 = new yourAdapter(this, getApplicationContext(), yourListItem, myList);
- myList.setAdapter(mAdapter1);
- //--------------
- yourListItem.java
- public class yourListItem {
- private String title;
- private String txt1;
- private String txt2;
- public yourListItem(){}
- public yourListItem(String title, String txt1, String txt2){
- this.title = title;
- this.txt1 = txt1;
- this.txt2 = txt2;
- }
- public String getTitle(){
- return this.title;
- }
- public String getTxt1(){
- return this.txt1;
- }
- public void setTitle(String title){
- this.title = title;
- }
- public void setTxt1(String txt1){
- this.txt1 = txt1;
- }
- //similarly set and get methods for txt2
- }
- //-----------------------
- yourAdapter.java
- import java.io.IOException;
- import java.util.ArrayList;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.graphics.Typeface;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.Button;
- import android.widget.TextView;
- public class yourAdapter extends BaseAdapter {
- private Context context;
- private Activity a;
- private ArrayList<yourListItem> newListItems;
- public yourAdapter(Activity a, Context context, ArrayList<yourListItem> newListItems){
- this.context = context;
- this.a = (Activity)a;
- this.newListItems = newListItems;
- }
- @Override
- public int getCount() {
- return newListItems.size();
- }
- @Override
- public Object getItem(int position) {
- return newListItems.get(position);
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- if (convertView == null) {
- LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
- convertView = mInflater.inflate(R.layout.list_item_new, null);
- }
- TextView txt1 = (TextView) convertView.findViewById(R.id.txt1);
- TextView txtTitle = (TextView) convertView.findViewById(R.id.list_new_title);
- TextView txt2 = (TextView) convertView.findViewById(R.id.txt2);
- txt1.setText(newListItems.get(position).getTxt1());
- txtTitle.setText(newListItems.get(position).getTitle());
- txt2.setText(newListItems.get(position).getTxt2());
- return convertView;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement