askIT

Android Convert string to long

Databases may use the long for their primary key. This will make a need for changing an Android String to Long.

String to long

String stringPrimaryKey = "5";
long longPrimaryKey = Long.parseLong(stringPrimaryKey);

Example of String to long:

String stringPrimaryKey = "5";
long longPrimaryKey = 0;
try{
        longPrimaryKey = Long.parseLong(stringPrimaryKey);
}
catch(NumberFormatException nfe) {
        System.out.println("Could not parse " + nfe);
}
Toast.makeText(getActivity(), "String: " + stringPrimaryKey + "\nLong: " + longPrimaryKey, Toast.LENGTH_SHORT).show();

Add comment

avatar_blank_60.png

Comment:

Name:

Email:

Comments

No comments yet. You can be the first one to comment. Just write your comment in the form and click on the submit button.