I'm currently trying to construct a method where the system compare two strings and if they are the same an intent is fired. This is code i'm using right now, it should be pretty self-explanatory:
String string 1 = ("Text 1");
String string 2 = EditText.getText().toString();
if(((string 1)).equals(string 2)) {
Intent intent = new Intent(this, SelectActivity.class);
startActivity(intent);
} else {}
The error I'm getting is the following (This code is inside "MainActivity"):
There is no applicable constructor to '(com.mycompany.myapp.MainActivity.(anonymous), java.lang.Class)'
I'm not really sure what this means so if someone would enlighten me that would be appreciated :)
Not sure why you need parentheses around string1's value during initialization, and hopefully your EditText in string2 actually contains a value derived form "R.find statement"
See if this helps:
String string1 = "Text 1";
String string2 = EditText.getText().toString();
if(string1.equals(string2)) {
Intent intent = new Intent(this, SelectActivity.class);
startActivity(intent);
} else {}
You should also follow up on MH's advice and make sure your method is properly defined.
Should that not be
String string1 = ...
String string2 = ...
if (string1.equals(string2)) {
note the lack of spaces...
Related
I tried to send data from one activity to another. The problem is I am not receiving all the datas. Just 3 instead of 6.PS: I am new to android development
sending data from this
Intent i=new Intent(getActivity(),PigeonInfo.class);
String n=p.getPigeonID();
String f=p.getFathersID();
String m=p.getMothersID();
String g=p.getGender();
String gr=p.getGroup();
String u=p.getPicURL();
i.putExtra("PID",n);
i.putExtra("FID",f);
i.putExtra("MID:",m);
i.putExtra("PGN:",g);
i.putExtra("PGR:",gr);
i.putExtra("PUR",u);
startActivity(i);
To this:
Intent i=getIntent()
e1.setText(i.getStringExtra("PID"));
e2.setText(i.getStringExtra("PGR"));
e3.setText(i.getStringExtra("PGN"));
e4.setText(i.getStringExtra("FID"));
e5.setText(i.getStringExtra("MID"));
String url= i.getStringExtra("PUR");
Use getIntent() instant of intent for getting the intent value
`
first Activity
Intent i=new Intent(getActivity(),PigeonInfo.class);
String n=p.getPigeonID();
String f=p.getFathersID();
String m=p.getMothersID();
String g=p.getGender();
String gr=p.getGroup();
String u=p.getPicURL();
i.putExtra("PID",n);
i.putExtra("FID",f);
i.putExtra("MID:",m);
i.putExtra("PGN:",g);
i.putExtra("PGR:",gr);
i.putExtra("PUR",u);
startActivity(i);
Second activity
Intent inent = getIntent();
e1.setText(intent.getStringExtra("PID"));
e2.setText(intent.getStringExtra("PGR"));
e3.setText(intent.getStringExtra("PGN"));
e4.setText(intent.getStringExtra("FID"));
e5.setText(intent.getStringExtra("MID"));
String url= intent.getStringExtra("PUR");`
For these 3 lines you add an extra : to the end of the key:
i.putExtra("MID:",m);
i.putExtra("PGN:",g);
i.putExtra("PGR:",gr);
But when you retrieve them from the intent, you don't have a : anymore in the key:
e5.setText(intent.getStringExtra("MID"));
So you can fix this by removing the extra : characters in your putExtra(...) calls.
Having typo's in these keys is pretty commons. You can work around such issues by defining these keys in static fields, which you reference from both places:
class Keys {
public static final String PIGEON_MID = "pigeon_mid"
}
...
intent.putExtra(Keys.PIGEON_MID, pigeon.getMid());
...
String mid = intent.getStringExtra(Keys.PIGEON_MID);
I use intents to communicate from a service to the activity. I use intent.putExtra(Tag, message); if I have a error or something.
In the Activity I can get these Extras like following
String message_error_1 = intent.getStringExtra("Message_Error_1");
String message_error_2 = intent.getStringExtra("Message_Error_2");
String message_error_3 = intent.getStringExtra("Message_Error_3");
but just one has data. I can now make an If statement for every entry but I think there is a way to figere out which entry has data. Is there a way?
If there's only going to be one error, I'd consider passing an ERROR_TYPE int as one extra and the ERROR_MESSAGE string as another extra, so you don't have to write so many if statements. For example:
int type = intent.getIntExtra("error_type"); // 1, 2, or 3
String message = intent.getStringExtra("error_message");
To answer your question, you could have the service put in an extra that indicates which error message key to use. For example:
// In Service
intent.putExtra("error_key", "Message_Error_1"); // error 1 has the message!
// In Activity
String key = intent.getStringExtra("error_key");
String actualErrorMessage = intent.getStringExtra(key);
Good evening guys,
I'm making an app and I want to know how to send a string to "List-View" in another activity ?
You can send data using the following code -
Intent intent = new Intent(this,newActivity);
intent.putExtra(name, value)
name = The name of the extra data
value = The String data value.
startActivity(intent);
In the new activity, you receive string via following (in onCreate)
Intent intent = getIntent();
String str = intent.getString(name)
name = The name of the extra data
Now search the web on how to add a string to list view. You will find it easily
I want to split string got from bluetooth. i'm using
StringTokenizer splitStr = new StringTokenizer(readMessage, "\\|");
String numberSpeed = splitStr.nextToken(); //splitStr[0].replaceAll("\\D+","");
String numberTorque = splitStr.nextToken(); //splitStr[1].replaceAll("\\D+","");
numberSpeed = numberSpeed.replaceAll("\\D+","");
numberTorque = numberTorque.replaceAll("\\D+","");
Did it with split string before.
If i get corupted data without delimiter the app crashes while trying to do impossible.
How to check if there is delimiter or not and then proceed split or skip it?
you can check for delimeter in string by contains() method
if(str.contains("_your_delimiter")) { //for safe side convert your delimeter and search string to lower case using method toLowerCase()
//do your work here
}
Try this, I use it in my app.
String container = numberSpeed ;
String content = "\\D+";
boolean containerContainsContent = StringUtils.containsIgnoreCase(container, content);
It will return true if it has delimiter, and false it not.
Use that with an if statement.
ex.
if(containerContainsContent){
//split it
} else {
//skip it
}
This is quote from tokenizer docs: StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code.
Try to user String.split() instead.
if(str.contains(DEILIMITER)) {
String tab[] = str.split(DEILIMITER);
//enter code here
}
I want to have more than one extra added to Intent. One to hold a double and one to hold long. Is this possible?
If so, how would I do it and how would I get the information from each extra?
You can add as many extras to an Intent as your heart desires, they are all just key value data:
Intent intent = new Intent();
intent.putExtra("name", "MyName");
intent.putExtra("age", 35);
intent.putExtra("weight", 155.6);
And they can be retrieved using the same key names:
String name = intent.getStringExtra("name");
int age = intent.getIntExtra("age", 0);
double weight = intent.getDoubleExtra("weight", 0.0);
intent.putExtra(#ExtraDoubleKey, #ExtraDoubleValue);
intent.putExtra(#ExtraLongKey, #ExtraLongValue);
Where #ExtraDoubleKey is a string that you will use to access the extra (i.e. "price" or something), and #ExtraDoubleValue is the value of the extra (the double variable you wish to pass). Similarly for #ExtraLongKey and #ExtraLongValue.
Then to access the extras in your next activity you can use:
double doubleValue = getIntent().getExtras().getDouble(#ExtraDoubleKey);
long longValue = getIntent().getExtras().getLong(#ExtraLongKey);
to get the value of the double extra with the key #ExtraDoubleKey.
https://stackoverflow.com/a/11461530/776075
Devunwired is correct.
But the way i see is
you can keep only one value per Type.
Like one string, one int, one double etc..
You are not capable of containing 2 string values. or two integers.
I have experienced this on a program and i have overcome it by using
one string and one Boolean.
You can use Bundle and pass it as a parameter to the Intent.
Intent nextActivity = new Intent(this, Activity2.class);
Bundle passData = new Bundle(); //to hold your data
passDataBndl.putString("fname", fname); //put in some String. the first parameter to it is the id, and the second parameter is the value
nextActivity.putExtras(passDataBndl); //Add bundle to the Intent
startActivityForResult(nextActivity, 0); //Start Intent