why on all demos and tutorials in SQLiteOpenHelper class variables are always: public static final
Have a look:
public static final String ORDER_ID = "order_id";
public static final String ORDER_LOGIN_NAME = "login_name";
public static final String ORDER_RESTO_U_NAME = "resto_uniqe_name";
My question is: I am making an application and its database is too big. So, I will have to make atleast 60-70 variables of these kind. Won't it affect application performance? As these are static variables.
Any help will be highly appreciated....
Well, whether they public or private or package-protected depends on your needs, but final static is a good way of declaring constants per Android guidelines, take a look here for explanation: http://developer.android.com/training/articles/perf-tips.html#UseFinal
Consider the following declaration at the top of a class:
static int intVal = 42;
static String strVal = "Hello, world!";
The compiler generates a class initializer method, called , that is executed when the class is first used. The method stores the value 42 into intVal, and extracts a reference from the classfile string constant table for strVal. When these values are referenced later on, they are accessed with field lookups.
We can improve matters with the "final" keyword:
static final int intVal = 42;
static final String strVal = "Hello, world!";
The class no longer requires a method, because the constants go into static field initializers in the dex file. Code that refers to intVal will use the integer value 42 directly, and accesses to strVal will use a relatively inexpensive "string constant" instruction instead of a field lookup.
Related
I am using Shared preferences for the first time and getting errors.
my code is like this :
public class MainActivity extends Activity {
static final String ONE = "";
static final String TWO = "";
private static SharedPreferences mSharedPreferences;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences.Editor edi = mSharedPreferences.edit();
edi.putString(ONE, "1");
edi.putString(TWO, "2");
edi.commit();
String one = mSharedPreferences.getString(ONE,"1");
String two = mSharedPreferences.getString(TWO,"2");
System.out.println("Your Numbers: "one+ " " + two);
}
}
Expected Output:
Your Numbers: 1 2
Console Output:
Your Numbers: 2 2
I can't figure out what i am doing wrong in it. Share your views.
You need to add some string to the names/keys. Currently both key names are blank and hence your code is overwriting the same preference value
.
Change the static strings as follows and it should work fine.
static final String ONE = "one";
static final String TWO = "two";
Also try using a helper class to make things simpler with shared preferences. Here is one that i wrote: Android-SharedPreferences-Helper
Because of this:
static final String ONE = "";
static final String TWO = "";
change it to:
static final String ONE = "One";
static final String TWO = "Two";
U need unique values for every preference.
In your case the ONE gets overridden by the TWO.
Extra info
If u look in the android docs here you will see that putString requires two parameters:
key : String: The name of the preference to modify.
value : String: The new value for the preference.
and if u than look at getString here you will notice that it also has two parameters, both the same as putString:
key : String: The name of the preference to retrieve. -- important
defValue : String: Value to return if this preference does not exist.
The name/key is the part that let the get part know from which preference it needs to get the value.
Hope this will make things a bit clearer for u!
Both the strings are empty
static final String ONE = "";
static final String TWO = "";
It should be like :
static final String ONE = "one";
static final String TWO = "two";
I would like to ask which would be better to put config files in Android, since Android doesn't support subfolders under values.
Example:
values/
config/
api.xml
What is the preferred style?
Creating a class with static variable:
package myapp.config;
public static class Api{
public static String CLIENT_ID = "1234567890";
public static String CLIENT_SECRET = "123456789";
}
Add everything in string.xml:
<string name="config_api_client_id">1234567890</string>
<string name="config_client_secret">123456789</string>
I would prefer class with final static variables. It is as constant as using string.xml, but when you use string resources (string.xml), you need a Context object to get the strings.
When changing your code snippet to the snippet below, you can access the variables from each and every piece of code and you know that the value of the variables never change.
public class Api{
public final static String CLIENT_ID = "1234567890";
public final static String CLIENT_SECRET = "123456789";
}
Then you can access the variables like so Api.CLIENT_ID
I am trying to define a search string and new to android, not sure how to do the same. Any clue?
Here's my code for the same:
public final static String PROD_ENVIRONMENT = "https://mobile13.com/fwd/answers/answers/service/v1/?q=**KEYWORD**%20revenue&ui.theme=novadark&uuid=PADACT-002&userAgent=iphone";
I want to replace the KEYWORD with a dynamic string like %s, which can be recognized with a static string say "public static string KEYWORD" , which i can check ,in turn matches the typed keyword and display the results accordingly
Try this
public final static String PROD_ENVIRONMENT = "https://mobile13.com/fwd/answers/answers/service/v1/?q="+KEYWORD+"%20revenue&ui.theme=novadark&uuid=PADACT-002&userAgent=iphone";
That is if you have the static variable in the same class. If you have it in a different class, say StaticUtils class then you can put it as StaticUtils.KEYWORD.
why will String.format not work?
android documentation
I would like to read your thoughts on how to solve the following problem. Here are the basic requirements of the application I am working on.
Display multiple locations one city on a map.
Support for multiple cities.
Display the properties of a location on a separate details view.
Location properties of each city differ.
I would like to implement a flexible decision logic that switches between cities based on the location that the map is at. That means, the initial information I rely on, is the map center. Here is the theoretical workflow.
Get the current center location from the map.
Translate the location into a city url and uri.
Download locations via HTTP using the url.
Store the locations in local database. One table for each city since location properties differ.
Load cached or downloaded data from a content resolver using the uri.
Create overlay items and include the uri reference and a unique id.
Open a details view when tapping on a location.
The details view should render the city specific location properties.
The location properties again are loaded via a content resolver based on the passed uri and unique id.
Questions:
I am particular interested on how you would switch cities, query parameters such as column names using the UriMatcher class.
Would you prepare one details view for each city? Or do you see any practical solution to swap text fields, label, .. based on the available property information?
To illustrate the different properties, here are two example cities and a content provider.
Example:
public class DatabaseParis {
public static final class Contract {
public static final String COLUMN_LATITUDE = "latitude";
public static final String COLUMN_LONGITUDE = "longitude";
public static final String COLUMN_NAME = "name";
}
And another city ...
public class DatabaseDenver {
public static final class Contract {
public static final String COLUMN_LATITUDE = "lat";
public static final String COLUMN_LONGITUDE = "lon";
public static final String COLUMN_HEIGHT = "height";
public static final String COLUMN_DIAMETER = "diameter";
}
And a content provider ...
public class CustomContentProvider extends ContentProvider {
public static final class Contract {
public static final Uri URI_PARIS = Uri.parse("content://" + AUTHORITY + "/cities/paris");
}
private static final String AUTHORITY = "com.example.cities.locations";
private static final int URI_CODE_PARIS = 0;
private static final UriMatcher URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
static {
URI_MATCHER.addURI(AUTHORITY, "cities/paris", URI_CODE_PARIS);
}
I am unsure about using one or multiple content provider since I read putting all database references into one can be a problem when the data should be synced in the future.
I guess one who deals with such a scenario should consume the following information sources.
Developing Android REST client applications, Virgil Dobjanschi, Google I/O San Francisco, 2010
ContentProvider and REST API, Benjamin Brombach, Android UserGroup Berlin, 2012
I am just wondering what is the format in Zxing.
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
Do you think I can delete this line of code?
According to the latest version in the trunk:
/**
* Call intent.getStringExtra(RESULT_FORMAT) to determine which barcode format was found.
* See Contents.Format for possible values.
*/
public static final String RESULT_FORMAT = "SCAN_RESULT_FORMAT";
Contents.Format actually doesn't exist (anymore), but browsing through the code it seems fair to assume the "formats" have been replaced by "types". Contents.Type defines the following possibilities:
public static final String TEXT = "TEXT_TYPE";
public static final String EMAIL = "EMAIL_TYPE";
public static final String PHONE = "PHONE_TYPE";
public static final String SMS = "SMS_TYPE";
public static final String CONTACT = "CONTACT_TYPE";
public static final String LOCATION = "LOCATION_TYPE";
In other words: it gives you information about the type of data that is encoded by the scanned barcode. If you don't care about the type, you can simply ignore it.
This is a string naming the format of the barcode scanned, like "QR_CODE" or "UPC_A". Values come from the class BarcodeFormat. Can you remove it? sure, don't write this line of code if you don't need it!
Are you talking about using the intent integrator? If so, this is the kind of barcode that you just scanned. I don't know what your purposes are, but if you want to support more barcodes, you might want to consider this line more important.