I tried to pass a string parameter to the web app javascript code. It's failed while passing a variable with the string value. But it works when we hardcode the data. Please let me know what's wrong with this.
Working hardcoded code:
mWebview.evaluateJavascript("cm.setData('N051783')", new ValueCallback<String>() {
#Override
public void onReceiveValue(String value3) {
Log.d(" setData Return Value"," setData Return... "+value3);
}
});
Not working code with string variable
mWebview.evaluateJavascript("cm.setData("+sub_data+")", new ValueCallback<String>() {
#Override
public void onReceiveValue(String value3) {
Log.d(" sub_data Return Value"," sub_data Return... "+value3);
}
});
You are probably missing ""
My JavaScript function at com.UserProfile
function setUserName(name){
alert('username is ' + name)
}
How to call it from Java
mWebview.evaluateJavascript("com.UserProfile.setUserName("Rohit")",null);
How to pass the parameter?
You need to concatenate " with parameter. " is a special character. This is how you can concatenate " in a String.
Here is an example:
String username = "Rohit";
String func = com.UserProfile.setUserName(\""+ username + "\")";
mWebview.evaluateJavascript(func, null);
You can use StringBuilder to concatenate as well as"
Let's break it down line by line;
StringBuilder sb = new StringBuilder();
sb.append("com.UserProfile.setUserName") //com.UserProfile.setUserName
.append("(") //com.UserProfile.setUserName(
.append("\"") //com.UserProfile.setUserName("
.append(username) //com.UserProfile.setUserName("Rohit
.append("\"") //com.UserProfile.setUserName("Rohit"
.append(")") //com.UserProfile.setUserName("Rohit")
String func = sb.toString();
mWebview.evaluateJavascript(func, null);
Related
i have problem how to , remove the comma in the of my output. I use replaceall but it doesnt remove the comma , this my code
public void onClick(View v) {
String space = "";
String foo = " ";
String foo1 = ",";
String sentences = null;
//Splitting the sentence into words
sentences = multiple.getText().toString().toLowerCase();
String[] splitwords = sentences.trim().split("\\s+");
for (String biyak : splitwords) {
foo = (foo + "'" + biyak + "'" + foo1);
foo.replaceAll(",$", " ");//foo.replaceAll();
wordtv.setText(foo);
My codes Output : 'Hello','world',
My desire output: 'Hello','world'
String instances are immutable. As a result, methods like replaceAll() do not modify the original string but instead return a modified copy of the string. So replace foo.replaceAll(...) with foo = foo.replaceAll(...).
you can use substring method of String class. or You can use deleteCharAt() method of StringBuilder or StringBuffer classStringBuffer sb=new StringBuffer(your_string);sb.deleteCharAt(sb.length()-1);
U can also use a if statement and traverse the whole string .
If a comma(,) is found replace it with a space(" ").
I have a String userId:344556\\ncustomerId:233 .
I want to replace the substring \\n with new line. So i tried
String more = strmoreInfo.replace("\\n", "\n");
My desired output is:
userId:344556
customerId:233
But my current output is:
userId:344556\
customerId:233
What am I doing wrong?
'\' is an escape character. You'll want to change the statement to:
String more = strmoreInfo.replace("\\\\n", "\n");
I will try your code but i could not found any problem its works......
public class HelloWorld
{
public static void main(String[] args)
{
String strmoreInfo="userId:344556\\ncustomerId:233";
String more = strmoreInfo.replace("\\n", "\n");
System.out.println(more);
}
}
Simply put a space e.g "< SPACE >\n" in second argument of replace method.
String more = strmoreInfo.replace("\\n", " \n");
I'm using Codenameone to develop application in mobile. I want to create a method to show an error message on Screen. But I got an error:
This is my code
public class Common {
public static boolean checkNullOrEmpty(String value){
return !(value != null && !value.equals(""));
}
public static void showMessage(String title,String msgID, Object... params){
String result = String.format(msgID, params);
Dialog.show(title, result, "OK", "Cancel");
}
}
And this is the way I call that method:
Common.showMessage("Error", "Item %s ; Item %s","01","02");
This is error message:
error: cannot find symbol
String result = String.format(msgID, params);
symbol: method format(String,Object[])
location: class String
Can anybody help me? Thanks a lot.
String.format isn't supported by the Codename One subset of the Java API. You should be able to use something like StringUtil.replaceAll etc. to replace entries e.g. for this:
Common.showMessage("Error", "Item {0} ; Item {1}","01","02");
You should be able to do something like this:
String result = msgID;
for(int iter = 0 ; iter < params.length ; iter++) {
result = StringUtil.replaceAll(result, "{" + iter + "}", params[iter]);
}
I've made a class which holds some string and integers, in that class I made a function to convert the data in the class in to a readable string;
public String GetConditions() {
String BigString = null;
String eol = System.getProperty("line.separator");
try {
BigString += "Depth: " + ci(Depth) + eol;
and so on...
Because I have to convert many integers, I made an extra function to convert a integer to a string;
public String ci(Integer i) {
// convert integer to string
if (i != null) {
String a = new Integer(i).toString();
return a;
} else {
return "n/a";
}
}
This throws a NullPointerException exception on return a. I'm quite new to Java, this is probally a noob question... Sorry about, thanks in advance!
There is a much simpler way to convert an Integer to a String: use String#valueOf(int).
public String ci(Integer i)
{
return i == null ? "n/a" : String.valueOf(i);
}
Try converting the Integer you pass in your method to string, instead of instantiating a new one.
You can do it straight forward like:
String a = i.toString();
or
String a = Integer.toString(i.intValue());
Thanks guys, but I found the problem, I've tried to add something to a string which was 'null' , this line:
String BigString = null;
Can anybody please tell what toString in Android is for and how it can be used?
As example would be highly appreciated.
toString is not specific to android. Its a method in java's Object class, which is the superclass of every java object. 'toString' is meant for returning textual representation of an object. This is generally overridden by java classes to create a human readable string to represent that object.
Apart from many other uses, it is widely used for logging purpose so as to print the object in a readable format. Appending an object with string automatically calls the toString() of that object e.g. "abc" + myObject will invoke 'toString' of myObject and append the returned value to "abc"
A good example of toString implementation would look like -
#Override
public String toString() {
return new StringBuilder()
.append("{Address:")
.append(" street=").append(street)
.append(", pincode=").append(pincode)
.append("}").toString();
}
Its the same as in normal Java...
I use it for debugging like this:
class MyClass {
var myVar;
var myOtherVar;
public String toString() {
return "myVar: " + myVar + " | myOtherVar: " + myOtherVar;
}
}
with Log.d("TAG", myClassObject.toString()); I can log what my object contains...
thats just one of countless possibilities.
class Account {
public final String name;
public final String email;
public Account(String name, String email) {
this.name = name;
this.email = email;
}
#Override
public String toString() {
return new Gson().toJson(this);
}
}
It's not like Java. Override example:
#Override
public String toString()
{
return new StringBuffer()
.append("[Museum]-")
.append(name)
.append(" Lat:")
.append(lat)
.append(" Lon: ")
.append(lon).toString();
}
And then usage of the method is
Log.i(LOG_TAG, museum.toString()); // Instead of just "museum"