Android :: Way To Refactor Current Code To Add Database Feature?

May 22, 2010

I am adding a local database as a cache to a remote web service in my android application to answer queries. I used ArrayAdapters before for list views to display the results from the web service. Now with a database cache, the result could be either a Cursor(from database) or a List(from web), which means the adapter can be CursorAdapter or ArrayAdapter too. Creating two adapters for one query doesn't seem to be a good idea. So I am wondering what would be the best way to refactor my current code to add this database feature?

Android :: Way to refactor current code to add database feature?


Android :: How To Import Cursor From Database Class Into Current One?

Jul 1, 2010

Here is my code for my db class:
package one.two;
import java.util.List;
import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import java.util.ArrayList;
public class DBAdapter extends ListActivity
{public String status = "status";
public String id = "id";
public String arrival = "arrival";
public String destination = "destination";
public String ferry = "ferry";
private static String DB_PATH = "/data/data/one.two/databases/";
private static final String DATABASE_NAME = "ferry.db";
private static final String DATABASE_TABLE = "port";
public static Context context;
public Cursor c;
public static SQLiteDatabase DbLib;
//overloaded non-null constructor
public DBAdapter(Context context) {
DbLib = context.openOrCreateDatabase(DATABASE_NAME,
SQLiteDatabase.CREATE_IF_NECESSARY,null);
System.out.println("OpenOrCreateDB Done");
} public class DatabaseHelper extends SQLiteOpenHelper {
Context context;
DatabaseHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}//end constructor DatabaseHelper
// ---closes the database---
public void close() {
DBHelper.close();
}//end close()
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion){
}//end onUpgrade()
@Override
public void onCreate(SQLiteDatabase db){
}//end onCreate()
}// end class DatabaseHelper
private static DatabaseHelper DBHelper;
private static final int DATABASE_VERSION = 1;
public static ArrayList<String> getAllTitles(){
ArrayList<String> port = new ArrayList<String>();
Cursor c=null;
c = DbLib.query("port"
new String[] { "status", "id", "arrival",
"destination", "ferry" }, null, null,
null, null, null);
try {
if (c!=null) { // start - when there is at least 1 record
System.out.println("Cursor is NOT NULL");
int i =0;
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
{// Debug Stm
System.out.println("Record No. "+i);
System.out.println(c.getString(0));
System.out.println(c.getString(1));
System.out.println(c.getString(2));
System.out.println(c.getString(3));
System.out.println(c.getString(4));
// Assign database cursor.records to arraylist
port.add(i,c.getString(0));
port.add(i,c.getString(1));
port.add(i,c.getString(2));
port.add(i,c.getString(3));
port.add(i,c.getString(4));
i = i + 1;
} } // end - where there is at least 1 record
} finally {
if (c!=null) {
c.close();
} } return port;
}//end getAllTitles()
public void open() {
//Open the database
String myPath = DB_PATH + DATABASE_NAME;
DbLib = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
} }//end class DBAdapter
My Arrival class
package one.two;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class Arrival extends ListActivity
{ private ListView listView;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState)
{ ArrayList<String> retList = new ArrayList<String>();
System.out.println("Start onCreate Function");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
System.out.println("In onCreate Function");
System.out.println("In of GetData");
DBAdapter db = new DBAdapter(this);
System.out.println("DB Open");
db.open();
System.out.println("DB Opened");
retList = getData();
System.out.println("Out of GetData");
// force count no. of records in table
// dump to check index
int cnt = 2;
int i=0;
for (i = 0; i<cnt; i++)
System.out.println(retList.toString());
listView = (ListView) findViewById(android.R.id.list);
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, android.R.id.list, null, null, null);
this.setListAdapter(mAdapter);
// db.close();
} @SuppressWarnings("static-access")
public static ArrayList<String> getData()
{ ArrayList<String> items = DBAdapter.getAllTitles();
// titles ???? redundant
//ArrayList<String> titles = new ArrayList<String>(items);
System.out.println("Return a LIST titles");
return items;
} }

View 1 Replies View Related

Android :: How To Refactor Class So I Can Unit Test It?

May 21, 2010

I am trying to unit test a class that does SAX parsing and creates an object.This class takes a string as a parameter representing the URL of a document on the internet, parses it and then creates an object based on the contents.I don't want to have the unit tests actually access the network, so I'd like to have a few test xml files to parse. However I can't figure out how to access them from my AndroidTestCases. I don't want to include the test files with the actual application, I want them in the test project (it's a separate project, as is the norm for Android tests from what I could gather - due to the need to have a custom AndroidManifest.xml, for one).One way would be to put the XML files in the test project's assets directory, I can read them using getContext() .getAssets().open(filename) into an InputStream in the test case, but my class expects a URL string. I'd rather not have to provide an InputStream to this class instead of the current URL string. I can test just the parsing by making two methods, one that takes a string and one an Inputstream, and test the second, but how can I then test the one that just takes a string?How should I design my class and or tests to circumvent this problem?

View 1 Replies View Related

Android :: Get Current SDK Version In Program Code?

May 19, 2009

Can I get current SDK version (like 1.0 or 1.5)in the program code?

View 4 Replies View Related

Android :: Not Being Able To Display Current Location On Map / Code Fr This?

Oct 14, 2010

I am new to android and not being able to display current location on map so can you provide me the code for displaying Location with manifest.xml and main.xml.

View 2 Replies View Related

Android :: Retrieve Current Version Code Of App's Manifest?

Jan 23, 2010

How do you retrieve the current version code of an app's manifest? I don't need to access another application, I'm talking about My app accessing its own version code.

View 5 Replies View Related

Android :: How To Obtain Current Local Area Code Of Handset?

Feb 16, 2010

I want to obtain the current Local Area Code of the Cell the handset is currently loged in to.
How do I get this information?

View 3 Replies View Related

Android :: Database - Error Code 5

May 19, 2009

Getting error code 5 and unable to insert the data in the database, when more than one thread are trying to populate the data in database.

I am using the following versions - android sdk1.1 and sqlite 3.0.

View 2 Replies View Related

Android :: How To Fix Error Code 5 : Database Is Locked

Oct 15, 2010

I'm creating a Thread and in that thread I open a database connection using the DBHelper, perform an update on a field in my database and close it. I create a separate one in the Thread because if the View is closed the global one for that view is closed, causing my Thread update to throw an error. The thread is in a View, that also has it's own instance of the DBHelper and opens the database onCreate closes it onPause opens it onResume closes it onDestroy The instance in my Thread is throwing this but not all the time :error code 5: database is locked Is there a known practice I should implement so I don't get this error?

View 9 Replies View Related

Android :: Error Code 5 : Database Is Locked / Way To Fix?

Aug 23, 2009

I have different background threads in my application, one doing periodical database updates, another responsible for doing long searches and so. Obviously some are just reading from database while others are writing as well. Once in a while I get "database is locked" exception. I need to know the safe way of reading/writing from/to database on different threads. As well I've found "isDbLockedByCurrentThread()" and "isDbLockedByOtherThreads()" methods on SQLiteDatabase. Should I use them every time I want to access the database? What if the database is locked? Should I pause the current thread and retry a few milliseconds later?

View 5 Replies View Related

Android :: Sample Code For Getting Current Weather Details From Weatherbug Using Java Droid?

Dec 24, 2009

I am new in android weatherbug technologies. Please can you tell me how to interact with weatherbug from android using weatherbug API. I need to get the current weather condition from weatherbug and display it on android screen.

View 2 Replies View Related

Android :: JNI - C++ Code Can Directly Read The App's Sqlite Database

Sep 22, 2010

I'm working on an Android app with a Java component and a C++ component via JNI. The C++ component is used on other platforms. It would make my life considerably easier if the C++ component could query the existing SQLite database (via the SQLite C API), without having to shell calls out to the Java side.

View 3 Replies View Related

Android :: Want To Get Code Of Content Provider Which Database Is Created

Oct 8, 2010

I want to get a code of content provider which a database is created. I am using the tool which located here tools/sqllite3.exe to check if the database is created.Please let me know the step by step procedure for this thing.

View 2 Replies View Related

Android : Media Database Direct Access From Code

Jul 20, 2010

Can I access android MediaProvider (Images & Videos) databases directly from my code rather then through a content provider ?

I'm planning to have my own Cursor implementation so inside it I want to access the media databases directly, possible or android does not allow it ?

View 1 Replies View Related

Android : Phone Code To Fetch Data From Database

Aug 23, 2010

ParsedNotificationDataSet result = new ParsedNotificationDataSet(); Code...

here is my code to fetch data from database. according to me it works properly but at the very first time it will generate the exception java.lang.NullPointerException. after that it will runs very correctly. Will you help me what modification i will do to resolve this.

View 2 Replies View Related

Android :: Unable To Open Database File - Uncaugh Exception Code

Mar 27, 2009

I am having problem using webview in my application. When ever the webviewclient and webview is trying to load, it will throw me and uncaugh exception. Code...

View 2 Replies View Related

HTC Desire :: New Database Unlock Code

Sep 26, 2010

I have my new desire for almost a week now. I am trying to unlock it (it's locked to telus). I have tryed many website (mobileincanada, cellunlock) but they still answer me that the didn't receive the codes for ''new'' Desires.I didnt ask a refund from cellunlock because he told me that if he doesnt have it, no one else will have it.so I'd better wait (1-3 buisness days) after 3 buisness days they still don't have it.I've spoken to an other admin from htccode dot com and they can 'give it a try'because they have acess to ''all' databases. I'm not into asking a refund from cellunlock and be stucked again with htccode.Can someone explain me the theory of 'new databases' Vs 'old databases'?How do these buisness work? How can they have to htc databases... why HTC give them access? Hopefully I will find someone here who's in my situation and will try to help out

View 1 Replies View Related

Android :: Sample Code To Store / Retrieve Images In SQLite Database In Droid?

Dec 16, 2009

I want to know how to store the images in SQLite database in android.and also how to retrieve it. anyone give the samplecode or any URL give me to develop my application.

View 3 Replies View Related

Android :: Blocking Current Activity / Screen Till Current Task Is Complete

May 13, 2010

The default behavior of an activity when BACK softkey is pressed is, GO BACK TO PREVIOUS ACTIVITY. If some the same activity is waiting for some response from server or some data updation is going on and then press BACK, I want to wait on the same screen till the current task is completely processed and then move out to the previous activity.

You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscribe@googlegroups.com For more options

View 12 Replies View Related

Android :: Connect To Remote Database Online Database

Nov 8, 2010

ive been looking for a week now i need some help connecting to a remote database...i want my app to get data out of the database and update the database.ive tried this http://www.helloandroid.com/tutorials/connecting-mysql-database but i dont understand it.

View 1 Replies View Related

Android :: Full Android Database Class For Existing SQLite Database?

Aug 23, 2010

I'm trying to deploy an application with an existing SQLite database.I've been reading though the examples that are posted but they are always missing some part of the class. I feel like I'm trying to bake muffins but no one told me to use baking powder.Can someone post a full database helper class for depoying an SQLite database on Android? Edit : Delete old code because it doesn't work.

View 2 Replies View Related

Android :: Synchronizing Sqlite Database On Android To A Sybase Database On Server

Apr 12, 2010

I'm currently developing a Field-Service application that stores data in the local sqlite database on an android device. At some point, usually after completing the data collection rounds, the local sqlite db is to be synchronized to a remote sybase db on the server.Any suggestions as to how this could be achieved or engineered as a solution? Or even better, are there alternatives to synchronizing data in such an application?

View 1 Replies View Related

Android :: Change Existing Apps Code Of G1 - Calendar And Camera Code

Feb 19, 2009

I wanna to add some code in Camera Apps code , so that it will invoke my Application and do the thing as per my desier. Is it possible to change the Application code? If any one already tried this then plz let me know, how to proceed and build if i change some code. How to integrate my application with camera apps.

View 2 Replies View Related

Android :: Put Algorithm Code To Create Binary Library / Link This In Main Function Code?

Jan 27, 2010

I can build a daemon with my algorithm code and main function.I put all files in one folder under /development and make This executable file works successfully

Now I want to separate my algorithm code and main function code. I hope to build a binary library with my algorithm code and main function will link this binary library to use.

Where to put my algorithm code to create binary library and how to link this binary library in main function code?

View 2 Replies View Related

Android :: Integrating Code With Home Screen Code

Jan 22, 2010

I am trying to integrate my code to the android Home Screen code. I want to launch my activity frm the HomeScreen by click of a button. I Did the code & then build it but when i click the button i m getting error as Application not installed on phone.

View 7 Replies View Related

Android :: Regarding Database Schema Of Android Native Database

Nov 1, 2009

I want to build my own cloned databases of all or some of the Android native databases like Contacts, Calllogs, Mediastore, Settings, etc. I want to know wheher it is possible and How I can know the database schemas of these databases? Is there any documentation on this?

View 4 Replies View Related

Android :: Roaming Feature Not Available?

Jan 6, 2010

I don't get it? I've never had a phone make such a pain until I got my Hero. In fact, it's never been a feature in any phone I've had until the Hero. So what's the big deal? Why bother including it if it doesn't do anything useful?

View 20 Replies View Related

Android :: Feature Of Linux In Our App?

Mar 25, 2010

Since Android is built on Linux Kernel.Are there any functionality of Linux which we can use in our application?

View 2 Replies View Related

Android :: Sent Email Inscription Feature Not Available?

Jul 19, 2010

Everybody knows how when you get an email from a Blackberry user, it will say at the bottom something to the effect of "sent from my Blackberry on Verizon Wireless" I have the first generation Motorola Droid. Why do my emails not say something similar? But a friend of mine recently purchased the Droid X and sent me an email and his email stated that it was "sent via Droid on Verizon Wireless". This feature would be nice to have to let people know you are mobile.

View 1 Replies View Related

Android :: How To Update My Number Feature?

Sep 2, 2010

Just got a Samsung Galaxy S on Orange UK, but when they ported my old number to this SIM card from vodafone, the 'my number' feature displays the old number and not the working one. How do I get it to say my actual number as that old number isn't even valid anymore, and its annoying. Hope I'm explaining my problem clearly enough, hard to explain.

View 1 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved