Android :: Static Database Class To Use With Any Activity

Jan 5, 2010

I am new to Android, and I haven't developed any app regarding databases on Android yet. So I have a few basic questions. I am after a good database class sample, that will let me to run the CRUD operations. I would like to use it as a static class

like: clsDB->Select("select * from clients");
or objClient->Delete(clientid);

I am wondering if someone, can share his database classes, if more people share the best. Also I have in mind to reuse this class in multiple ways, I will have services, activities etc...

Android :: static database class to use with any activity


Android :: Call Non Static Method In Static SQLiteDatabase Class

Mar 30, 2010

i want to display a msg to the user (msg box or Toast) when exception happend in a static SQLite Database class that i use. the problem is that i cant call a non static method in a static class , how can i handle this. this is the class

private static SQLiteDatabase getDatabase(Context aContext) {

and i want to add something like this in the class when exception happen but context generates the problem of reference to non static in static class.

Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();

View 1 Replies View Related

Android :: Parsing XML And Initializing Static Variables Of Some Class

Dec 21, 2009

I have an application that is driven by a configuration XML: various app properties are loaded at the app start-time by parsing the XML and initializing static variables of some class. The data read from this XML drives different Activities of the application. Presently, I have called the "parsing and the properties-initialization" from the onCreate() of my Main Activity. I have a few questions as regards this case/approach: Should I invoke the app initialization method from the Application Object or is the current approach correct?

What advantages/disadvantages do/would we get/have if I choose to invoke it from the Application object? Do we really need a static class to store app properties? Or can we have all the properties as a static Collection variable in the application object? Parsing a XML(~200 nodes) at app load time might take some time (not sure how long tho); How can I avoid the dreaded ANRs? I am using a Pull Parser.

View 2 Replies View Related

Android :: Cannot Start Service If Inner UpdateService Class Not Static

Oct 2, 2009

if the inner UpdateService class is not static, it cannot start the service. The ActivityManager complains "Unable to start service Intent"...

View 8 Replies View Related

Android :: Put Static Data Into SQLite Database?

Jun 8, 2010

If I have a bunch of data that is never going to change (eg. an English language dictionary or the rgb values of a couple hundred color names), how do I use an SQLite database to store it? I know a database is faster than loading everything into memory when the app starts, but how do I make the database either the first time the app runs or "before" the apps ever runs?

View 2 Replies View Related

Android :: Call Non Static Method (Located In Main Class) From Different One

Oct 18, 2010

I've been trying to call a non-static method, located in my main application Class, from the Preferences Class. Because the method I call is not static, I instantiate the main class and then try to call the specific method I want but it's force closing.

Preferences.class (from where I call the method):
Preference sorted = (Preference) findPreference("sortPref");
sorted.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object newValue) {
Object d = new Dmarks();
((Dmarks) d).queryBookmarks();
return true;
}});
the Dmarks.class method I call:
public void queryBookmarks() {
Toast.makeText(context, Toast.LENGTH_LONG).show();
//context is not null and the Toast is working if I call it from Dmarks.class }

The Logcat:
E/AndroidRuntime(11718): FATAL EXCEPTION: main
E/AndroidRuntime(11718): java.lang.NullPointerException
E/AndroidRuntime(11718):
at android.content.ContextWrapper.getContentReso
lver(ContextWrapper.java:90)
E/AndroidRuntime(11718):
at android.app.Activity.managedQuery(Activity.ja
va:1520)
E/AndroidRuntime(11718):
at com.droidil.droidmarks.Dmarks.queryBookmarks(
Dmarks.java:101)
E/AndroidRuntime(11718):
at com.droidil.droidmarks.Preferences$2.onPrefer
enceChange(Preferences.java:47)
E/AndroidRuntime(11718):
at android.preference.Preference.callChangeListe
ner(Preference.java:756)
E/AndroidRuntime(11718):
at android.preference.ListPreference.onDialogClo
sed(ListPreference.java:219)
E/AndroidRuntime(11718):
at android.preference.DialogPreference.onDismiss
(DialogPreference.java:384)
E/AndroidRuntime(11718):
at android.app.Dialog$ListenersHandler.handleMes
sage(Dialog.java:1047)
E/AndroidRuntime(11718):
at android.os.Handler.dispatchMessage(Handler.ja
va:99)
E/AndroidRuntime(11718):
at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(11718):
at android.app.ActivityThread.main(ActivityThrea
d.java:4627)
E/AndroidRuntime(11718):
at java.lang.reflect.Method.invokeNative(Native
Method)
E/AndroidRuntime(11718):
at java.lang.reflect.Method.invoke(Method.java:5
21)
E/AndroidRuntime(11718):
at com.android.internal.os.ZygoteInit$MethodAndA
rgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime(11718):
at com.android.internal.os.ZygoteInit.main(Zygot
eInit.java:626)
E/AndroidRuntime(11718):
at dalvik.system.NativeStart.main(Native Method)
D/dalvikvm(11718): GC_FOR_MALLOC freed 4248 objects / 282248 bytes in 40ms
W/ActivityManager( 244): Force finishing activity com.droidil.droidmarks/.Preferences

View 5 Replies View Related

Android :: Static Variable From Java Class In Res / Values / Styles.xml?

Apr 15, 2010

I want to refer to a static Java variable in my styles.xml, is that possible? Here's a pseudo-xml example:

<style name="Artwork">
<item name="android:background">@drawable/border_{Constants.MY_COLOR}</item>
</style>

View 1 Replies View Related

NoClassDefFoundError For Static Inner Class In Library Jar

Dec 9, 2013

I have a thread on StackOverflow (titled: NoClassDefFoundError for Jersey 2.4.1 client in Android) but I thought I would reach out here in hope of getting a more specific/targeted audience group.I have followed instructions from other posts about the libs Android dependency fix but I still get the issue.

I am using Eclipse Juno, with an Android App targeting API 17. Basically I am getting a NoClassDefFoundError at runtime for the org. glassfish. jersey. model.internal.CommonConfig$1 class located in jersey-client-2.4.1.jar. This is when I just do a Run As... deploy from within Eclipse and it fails when I instantiate the CommonConfig class or one that depends on it internally. The start up log is given below:

[HIGH]12-10 12:25:20.657: I/dalvikvm(10170): Failed resolving Lorg/glassfish/jersey/model/internal/CommonConfig$1; interface 920 'Lcom/google/common/base/Function;'
12-10 12:25:20.657: W/dalvikvm(10170): Link of class 'Lorg/glassfish/jersey/model/internal/CommonConfig$1;' failed
12-10 12:25:20.657: E/dalvikvm(10170): Could not find class 'org.glassfish.jersey.model.internal.CommonConfig$ 1', referenced from method
[code]....

It doesn't seem to find a static inner class of CommonConfig. In the SO forum I tinkered with some options in ProGuard but I've come to learn that this isn't even used in the standard debug mode, only when you do a release build (Export from Eclipse). It would appear that something is happening with the class but I can't figure out why it would not be included if its parent is. Is there some kind of 'light' ProGuard process that goes on when a debug deploy is done? how to resolve this particular case of NoClassDefFoundError?

View 4 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 :: Communicating Between Receiver Class And An Activity Class

Jul 14, 2010

I'm just getting into Android development, and I have a question about communicating between a receiver class and an activity class. I'm very new to JAVA and Android so I hope I don't sound too stupid. I'm developing an application where I intercept an SMS message and then based on various elements of that SMS I might delete it once it's been saved to the inbox. I have a receiver class that intercepts the txt message, and I am also able to delete messages from my inbox with code in the activity class using a button at the moment. The problem I have is communicating between the receiver class and the activity class where the code to delete a message resides. I tried putting that code directly into the receiver class but as I'm sure most of you already know the BroadcastReceiver class doesn't seem to support what I need to delete messages. I've been searching for an answer to this for a while, but haven't been able to find anything. Honestly I'm not sure I know enough about JAVA and Android to even recognize a solution if I saw it.

View 2 Replies View Related

Android :: Access Application Class From Class Other Then Activity

Sep 8, 2010

I'm new to Java and android development. In my application I need data which is accessible for a few activities. I've read that a good solution is to use Application class for this. So I use it like this:

public class MyApplication extends Application {
private String str;
public String getStr(){
return str;
}
public void setStr(String s){
str = s;
}
}

and I can access this variable from activity like this:........................................

View 2 Replies View Related

Android :: Call Activity Class From Other Java Class?

Oct 8, 2010

I have just started android. I just want to know that how can i call activity class from other java class. i just want to pass class object to activity class.

public class GsonParser extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MagazineThread thread=new MagazineThread();
thread.start();
}
public GsonParser(JsonMagazineParser Obj)
{

}
}

and i am just doing like from other class. GsonParser obj=new GsonParser(this);passing obj to activity class.how can i achieve that.

View 1 Replies View Related

Socket Object As Static Class Field?

Mar 20, 2012

I am developing an application which incorporates socket programming. I need to refer to the socket object created in the starting application when connected to a server in any other activity in that application .

Can I define a class in an acitivity and define a socket as a static field in that class and refer to that socket object from any other activity in that application?

View 4 Replies View Related

Android : Way To Store - Retrieve Static Database File With Some Values

Feb 1, 2010

I am using ganymade eclipse 3.4 IDE and android sdk for development . I am trying to store static database file with some values on it in sqlite format. how can i add this file into the IDE and fetch the data from that file and store it . give me the guidance to do the development.

View 1 Replies View Related

Android :: Static Layout For Whole App That Must In All Activity

Nov 17, 2010

Static Layout for whole app that must be in all Activity

View 12 Replies View Related

Android :: Use Of Static Finals In Activity

Sep 8, 2010

Why is the use of static final variables encouraged to declare constants over just final variables? The use of static sounds logical when there will be many instances of a class but is this argument correct when used for a Android activity. In fact, since the Class instance will be around even after the activity finishes and is eventually garbage collected, it seems like all these constants will still be in memory until the class loader is around. Also, does the compiler inline non-static final variables(ints and String) just like it does for static final variables?

View 2 Replies View Related

Android :: Utility Class For Database Query

Sep 23, 2009

Can anybody suggest what functionality to provide in a query utility class/classes? The idea is to provide a database utility package for android developers. I just wanna do it for android community and of course for my learning.:-)

View 5 Replies View Related

Android :: Avoid Non Static Inner Classes In Activity?

Jun 17, 2010

Since I seem to have caught two activity references in a heapdump, where the Activity is set to singleTask. Romain's advice on avoiding memory leaks includes: "Avoid non-static inner classes in an activity if you don't control their life cycle, use a static inner class and make a weak reference to the activity inside"

What does this mean exactly? I can't find any examples, positive, or negative for this rule. I do have some non static inner classes in my activity. Most of them are anonymous inner classes like this one. I see hundreds of them in the samples:....................

View 24 Replies View Related

Android :: Activity Lifecycle And Static Variables

Jul 14, 2009

I just ran into a situation where it looks like a static variable reference is persisted across activity sessions. I didn't expect that because I thought that when an activity exits, it's de-referenced and garbage collected. I am wondering if anyone can shed some (more) light on when the VM eliminates object references for Activities and Services and in particular when static variables get reset to default values?

View 7 Replies View Related

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 :: Cannot Open Database In Service Layer Class

Sep 15, 2010

When I create a instance of the SQLiteOpenHelper, I always need a "Context" to be passed as its constructor's argument. To use this "Context", in my service layer class, I extends Activity class, though I do not display this activity on the screen because it is my service layer. I always get NullPointer exception when I open the SQLiteOpenHelper in my service layer class, Is it because my service layer class extends the Activity class but I did not show this activity on mobile screen? (That's there is no intent to my service layer class), if it is so, how to get rid of it if I wanna open a SQLiteOpenHelper in my service layer class which do not need to extends Activity, but need a Context object to pass to SQLiteOpenHelper?

View 2 Replies View Related

Android :: Start An Activity From Non Activity Class

Aug 19, 2009

An activity can be called from existing activity through *startActivity(intent); *

Is t possible to call an activity from non Activity class ? and how?

View 2 Replies View Related

Android :: Remove Static Variable Value , When Will I Close My Activity?

Aug 26, 2010

I want to clear variable value when will i close activity.

View 1 Replies View Related

Android :: Create Static Options Menu For All Activity Screens

Jan 15, 2010

I wanted to create a static options menu for all my activity screens. I dont want to override onCreateOptionsMenu() in each activity. Since Menu class is an interface with a huge number of methods, its difficult to create a static object of the implementing class.

View 3 Replies View Related

Android :: Static Fields In Activity Classes Guaranteed To Outlive Create / Destroy Cycle?

Oct 28, 2009

I frequently run into the problem that I have to preserve state between several invocations of an activity (i.e. going through several onCreate()/onDelete() cycles). Unfortunately, Android's support for doing that is really poor. As an easy way to preserve state, I thought that since the class is only loaded once by the class loader, that it would be safe to store temporary data that's shared between several instances of an activity in a static Bundle field. However, occasionally, when instance A creates the static bundle and stores data in it, then gets destroyed, and instance B tries to read from it, the static field is suddenly NULL. Doesn't that mean that the class had been removed and reloaded by the classloader while the activity was going through a create/destroy cycle? How else could a static field suddenly become NULL when it was referencing an object before?

View 2 Replies View Related

Android :: Cannot Make Static Reference To Anon-static Method

Feb 7, 2010

I'm having some issues with the old "Cannot make a static reference to a non-static method" error in my Android program. I am creating a sand falling game (similar to the Powder Game) and I created a class called Control to create a Control Bar at the bottom of the screen with a slider for brush size (that works fine) and a button to pop up a Dialog to allow users to pick the selected element. However, when I call DemoActivity.showDialog(2) from my code, it gives the static reference to non-static error (DemoActivity is the main activity of my application). I also tried changing it to just Activity.showDialog(2), but I got exactly the same error!

Here's my code:

CODE:................

I fixed it by adding the following to my Control.java code:

CODE:..........

And then calling control.setActivity(this); from my onResume section of DemoActivity.java!

View 2 Replies View Related

Android :: Android - Use One SQLiteOpenHelper Class For Multiple Database Files

Nov 20, 2010

My app uses two databases (separate files). To handle these databases I have created two Helper classes which extend SQLiteOpenHelper, one for each database.

I am now going to add a third database and wonder whether I need to create yet another Helper class (and if I used a 4th and a 5th database would I need even more Helper classes), or can I use the same Helper class for multiple databases?

The problem that I see with trying to use just one Helper class is that I can't see how to pass the name of the individual database files to the Helper. At present the name of the database is hard-coded as a Static field of each of the Helper classes, but if I had only one Helper class I would need to be able to pass the different names in to the Constructor when creating the separate Helper objects; the problem is that the SQLiteOpenHelper Constructor seems to be called by Android with just one parameter: the Context.

View 2 Replies View Related

Android :: Non-static Method Cannot Be Referenced From A Static Context

May 26, 2010

I am modifying the source code here: http://thinkandroid.wordpress.com/2009/12/30/getting-response-body-of-httpresponse/

I get this error:

code:.............

This error is line 13 on the second box.

View 5 Replies View Related

Android :: Context To Use In A Activity Class?

Mar 3, 2010

I have a class defined as

public class viewGroups extends ListActivity

Somewhere in the class I have

objItem = new clsContactGroups(context);

I am wondering what is advised to be used here? Which context?
I know four choices, but maybe there are others...

this
this.getApplicationContext()
this.getBaseContext()
this.getParent()

I use this Context to show a Toast. So I would like to show on the front-most view visible.

View 1 Replies View Related

Android : Way To Use Intent From Non-activity Class?

Jan 2, 2010

I have three classes one main-activity(named MainMap), one non-activity class(named MyItemizedOverlay), and one activity class(named AudioStream). I want to start AudioStream activity from non-activity class but i don't know how to.

View 1 Replies View Related







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