Is there a way to call multiple strings with just one intent, like in a webview activity and using the if / else clause like this:
public void openGo(View v) {
TextView display = (TextView) findViewById(R.id.display);
String check = display.getText().toString();
if (check.contentEquals("1")) {
Intent i = new Intent(this, WebViewActivity.class);
i.putExtra("keyHTML", "file:///android_asset/page1.html");
startActivity(i);
} else if (check.contentEquals("10")) {
Intent i = new Intent(this, WebViewActivity.class);
i.putExtra("keyHTML", "file:///android_asset/page2.html");
startActivity(i);
If string 1, and 12 and 16 have the same webview page, how do you link them together? Is this possible like below? NOTICE THE BOLD TEXTS.
public void openGo(View v) {
TextView display = (TextView) findViewById(R.id.display);
String check = display.getText().toString();
**if (check.contentEquals("1,12,16")) {**
Intent i = new Intent(this, WebViewActivity.class);
i.putExtra("keyHTML", "file:///android_asset/page1.html");
startActivity(i);
} else if (check.contentEquals("10")) {
Intent i = new Intent(this, WebViewActivity.class);
i.putExtra("keyHTML", "file:///android_asset/errorpage.html");
startActivity(i);
Thank you.
use
if (check.contentEquals("1") || check.contentEquals("12") || check.contentEquals("16") )
I dont think what you use work correctly
Related
So I'm new to programming. I have a string array named values that has about 150 strings in it. Instead of using a ton of if statements I wan't to to use a for loop that each time it cycles through the loop increments to the next element in the array. I'm sure it's a super simple fix but I just can't solve it. Thanks for any advice!
routeListView.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String route = values[position];
int i;
for (i=0; i < values.length;i++) {
if (route.equals(values[0])) {
Intent intent = new Intent(view.getContext(), RouteDetails.class);
intent.putExtra("route", routeDetail[0]);
startActivity(intent);
}
values++;
}
/*if (route.equals(values[0])) {
Intent intent = new Intent(view.getContext(), RouteDetails.class);
intent.putExtra("route", routeDetail[1]);
startActivity(intent);
}
if (route.equals("Main Wall")) {
Intent intent = new Intent(view.getContext(), RouteDetails.class);
intent.putExtra("route", "Map of Main Wall");
startActivity(intent);
}
if (route.equals("1. Shark Bait - 5.9")) {
Intent intent = new Intent(MainActivity.this, RouteDetails.class);
intent.putExtra("route", "Shark Bait");
startActivity(intent);
}
*/
}
Inside of the loop, replace the hard coded 0's with "i". This will allow your code to be ran for each iteration of the loop. For example, the i will be replaced with 0, then 1, then 2, etc.
for (int i=0; i<values.length; i++) {
if (route.equals(values[i])) {
Intent intent = new Intent(view.getContext(), RouteDetails.class);
intent.putExtra("route", routeDetail[i]);
startActivity(intent);
}
}
Also, there is no need to add a counter for values at the end, since it is handled by the i++ in the for loop. Hope that helps!
Looks like you can use switches...if you need both int and string comparisons you can use two switches to do that.
String route = values[position];
switch(position) {
case 0:
Intent intent = new Intent(MainActivity.this, RouteDetails.class);
intent.putExtra("route", routeDetail[0]);
startActivity(intent);
return;
case 1:
// Do stuff
return;
}
switch(route) {
case "Main Wall":
Intent intent = new Intent(MainActivity.this, RouteDetails.class);
intent.putExtra("route", "Map of Main Wall");
startActivity(intent);
return;
case "Shark Bait":
Intent intent = new Intent(MainActivity.this, RouteDetails.class);
intent.putExtra("route", "Shark Bait");
startActivity(intent);
return;
}
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
I am successfully create an onClick based on adapter getPosition.
the common way is that making onClicklistener at onBindViewHolder
#Override
public void onBindViewHolder (final MyViewHolder holder, final int position){
holder.mId.setText(itemList.get(position).getId());
holder.itemView.setOnClickListener(new View.OnClickListener(){
public void onClick(View view) {
final Intent intent;
if (position == 0) {
intent = new Intent(context, MyActivity.class);
} else if (position == 1) {
intent = new Intent(context, MyActivity2.class);
} else {
intent = new Intent(context, MyActivity3.class);
}
context.startActivity(intent);
But, what I am trying achieve is, the parameter that I'm gonna use to go to next Activity is the getId.
I've modified my code to this.
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final String idNumber = itemList.get(position).getId();
final Intent intent;
if (idNumber == "7") {
intent = new Intent(context, MyActivity.class);
} else if (idNumber == "10") {
intent = new Intent(context, MyActivity2.class);
} else {
intent = new Intent(context, MyActivity3.class);
}
context.startActivity(intent);
Log.e("YOUR ID NUMBER IS", idNumber);
Toast.makeText(context, "Recycle Click" + idNumber,Toast.LENGTH_SHORT).show();
}
});
When I run it, and when I click on any item, it keeps going to MyActivity3
But, on the Log and toast says, the right idNumber that I click.
Sometimes if(StringValue == StringValue1) does not gives expected result. try replacing
if (idNumber == "7")
with
if (idNumber.equals("7"))
so your code will be like,
if (idNumber.equals("7")) {
intent = new Intent(context, MyActivity.class);
} else if (idNumber.equals("10")) {
intent = new Intent(context, MyActivity2.class);
} else {
intent = new Intent(context, MyActivity3.class);
}
context.startActivity(intent);
Happy Coding.
There are many many questions about starting new Intent from another Intent but i'm unable to find solutions of mine. In my main Activity i started an Activity this way
Intent intent = new Intent(MainActivity.this, DownloadManagerSettings.class);
intent.putExtra("DM_SETTINGS_ACTIVITY", "MainActivity");
startActivity(intent);
and trying to get extra String value this way
public class DownloadManagerSettings extends Activity {
private Button dmOk;
private Bundle extra;
private String className;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dm_settings);
extra = getIntent().getExtras();
dmOk = (Button) findViewById(R.id.dm_settings_ok);
final Bundle strExtra = extra;
dmOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
className = strExtra.getString("DM_SETTINGS_ACTIVITY");
try {
Intent intent = new Intent(DownloadManagerSettings.this, Class.forName(className));
startActivity(intent);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
});
}
But in my LogCat shows following error
08-28 14:19:33.777: E/AndroidRuntime(17837): java.lang.NullPointerException08-28 14:19:33.777: E/AndroidRuntime(17837): at com.downloadmanager.settings.DownloadManagerSettings$1.onClick(DownloadManagerSettings.java:101)
Here 101 Line is
className = strExtra.getString("DM_SETTINGS_ACTIVITY");
UPDATE
new error
08-28 14:55:11.237: E/AndroidRuntime(24623): Caused by: java.lang.NullPointerException 08-28 14:55:11.237: E/AndroidRuntime(24623): at com.downloadmanager.settings.DownloadManagerSettings.onCreate(DownloadManagerSettings.java:66)
66 Line is
className = strExtra.getString("DM_SETTINGS_ACTIVITY");
NEW UPDATE1
Now
Intent intent = new Intent(MainActivity.this, DownloadManagerSettings.class);
intent.putExtra(getApplicationContext().getPackageName()+".DM_SETTINGS_ACTIVITY", "MainActivity");
startActivity(intent);
Another Activity
className = getIntent().getStringExtra(getApplicationContext().getPackageName()+".DM_SETTINGS_ACTIVITY");
final String newClass = className;
Log.d("DM_AAAAAAAA", className);
dmOk.setOnClickListener(new View.OnClickListener() {
Find in LogCat
08-28 15:09:14.670: E/AndroidRuntime(27291): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.addon.downloadmanager/com.downloadmanager.settings.DownloadManagerSettings}: java.lang.NullPointerException: println needs a message
Thanks in advance
try this:
public class DownloadManagerSettings extends Activity {
private Button dmOk;
private Bundle extra;
private String className;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dm_settings);
extra = getIntent().getExtras();
dmOk = (Button) findViewById(R.id.dm_settings_ok);
final Bundle strExtra = extra;
className = strExtra.getString("DM_SETTINGS_ACTIVITY");
dmOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method
try {
Intent intent = new Intent(DownloadManagerSettings.this, Class.forName(className));
startActivity(intent);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
});
}
Get the String data in onCreate first and then use it in onClick.
Update :
seems you'll have to add your package name as the prefix, see this. After that, use getStringExtra/getCharSequenceExtra to get the string data.(note we don't use Bundle any more). This should work.
update 1:
The following code work on my device.
Intent newIntent = new Intent(getApplicationContext().getPackageName());
newIntent.setClass(this, SecondActivity.class);
newIntent.putExtra(getApplicationContext().getPackageName()+".abc", "test");
startActivity(newIntent);
and in the other activity:
String data = getIntent().getStringExtra(getIntent().getAction()+".abc");
From the javadoc of Intent.putExtra(String,String) :
The name must include a package prefix, for example the app com.android.contacts would use names like "com.android.contacts.ShowAll".
So try this :
intent.putExtra(getPackageManager().getPackageName()+".DM_SETTINGS_ACTIVITY", "MainActivity");
and later:
className = strExtra.getString(getPackageManager().getPackageName()+".DM_SETTINGS_ACTIVITY");
Use getStringExtra() instead of getExtra()
or you can also do in from other side during intenet creation
Intent intent = new Intent(MainActivity.this, DownloadManagerSettings.class);
Bundle extras = new Bundle();
extras.putString("DM_SETTINGS_ACTIVITY", "MainActivity");
startActivity(intent);
Try this way,hope this will help you to solve your problem.
Intent intent = new Intent(MainActivity.this, DownloadManagerSettings.class);
intent.putExtra("DM_SETTINGS_ACTIVITY", "MainActivity");
startActivity(intent);
className = className = getIntent().getStringExtra("DM_SETTINGS_ACTIVITY");
OR
Intent intent = new Intent(MainActivity.this, DownloadManagerSettings.class);
Bundle bundle = new Bundle();
bundle.putString("DM_SETTINGS_ACTIVITY", "MainActivity");
startActivity(intent);
className = className = getIntent().getBundleExtra().getString("DM_SETTINGS_ACTIVITY");
The problem in your code is here,
intent.putExtra(getApplicationContext().getPackageName()+".DM_SETTINGS_ACTIVITY", "MainActivity");
Just replace this line with
intent.putExtra("MainActivity",getApplicationContext().getPackageName()+".DM_SETTINGS_ACTIVITY");
Check this code :
1. **MainActivity.java**
Intent intent = new Intent(MainActivity.this,MainActivity2.class);
intent.putExtra("MY TEXT", getApplicationContext().getPackageName()+".TestActivity");
startActivity(intent);
2. **MainActivity2.java**
Intent i = getIntent();
final String className = i.getStringExtra("MY TEXT");
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try {
Intent intent = new Intent(MainActivity2.this,Class.forName(className));
startActivity(intent);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
});
This is worked on my device.
I know there are few questions similar to mine and I have tried all the suggested solutions mentioned in the existing questions however it's still not working. Pretty sure I'm doing something wrong logically but unable to figure out where. Please point me to right direction.
Spinner activity code method
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3)
{
// TODO Auto-generated method stub
Intent main = new Intent (this, MainActivity.class);
Bundle myBundle = new Bundle();
String chosenAppType = appliancestypespinner.getSelectedItem().toString();
myBundle.putString("appliance type spinner",chosenAppType);
main.putExtra("chose appliance type", myBundle);
startActivity(main);
}
TextView Activity (getting the spinner value as string in textview (tvApplianceType = textview )
Bundle myBundle = this.getIntent().getExtras();
if(myBundle == null)
{
return;
}
String str_recieved_appType = myBundle.getString("appliance type spinner");
if (str_recieved_appType != null)
{
tvApplianceType.setText(str_recieved_appType);
}
}
Just to add that I'm also passing the value of Edittext present from the Spinner activity to the same textview that i'm passing the spinner value to that EditText to TextView is working fine. Is it not working because i'm using the same textview for both operations however either operation need to work at one time :/. So either edittext to textView OR spinner to textview.
Edittext Code
String str_appliance_type = et_input_Appliance_Type.getText().toString().trim();
if (!str_appliance_type.equals(""))
{
data.add(str_appliance_type );
aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data );
appliancestypespinner.setAdapter(aa);
//passing edittext value to MainActivity class
Bundle carrier = new Bundle();
Intent i = new Intent (this, MainActivity.class);
i.putExtra("appliance type",str_appliance_type);
startActivity(i);
}
Passing Value fro Editext to textview
//getting the edittext value from inputAppliance class
Bundle carrier = getIntent().getExtras();
if(carrier == null)
{
return;
}
String str_recieved = carrier.getString("appliance type");
if (str_recieved != null)
{
tvApplianceType.setText(str_recieved);
}
String str = appliancestypespinner.getSelectedItem().toString(); // I assume this line is proper.
Intent i = new Intent (this, MainActivity.class);
Bundle b = new Bundle();
b.putString("your_key", str);
i.putExtras(b);
startActivity(i);
In MainActivity
Bundle b = getIntent().getExtras();
String s = b.getString("your_key");
your_textView.setText(s);
Intent main = new Intent (this, MainActivity.class);
main.putExtra("SELECTED_DATA", appliancestypespinner.getSelectedItem().toString());
startActivity(main);
In Another Activity:
String data=this.getIntent.getStringExtra("SELECTED_DATA");
Try using Intent:
Spinner Activity:
Intent calculate = new Intent(getApplicationContext(),Second.class);
calculate.putExtra("carry_name", entername.getText().toString());
On Second Activity:
Intent intent = getIntent();
String name = intent.getStringExtra("carry_name");
congrates.setText("Congratulation " + name);
Change this
main.putExtra("chose appliance type", myBundle);
startActivity(main);
to
main.putExtras(myBundle);
startActivity(main);
I have a borderless button on in one of my layouts. When it is clicked, I want the intent to transfer both the button's tag and the text it contains. I am having trouble doing this.
This is what I have for when the button is clicked:
public void openGoalWeek (View view) {
Intent intent = new Intent(this, ViewGoal.class);
Button button = (Button) findViewById(R.id.week_goal);
Bundle bundle = new Bundle();
bundle.putString(EXTRA_MESSAGE, button.getText().toString());
bundle.putString(EXTRA_TAG, button.getTag().toString());
intent.putExtras(bundle);
startActivity(intent);
}
This is what I have in the ViewGoal class:
public class ViewGoal extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_goal);
// make sure running on honeycomb or higher for actionbar API
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
}
// get the message from the intent
Intent intent = getIntent();
String message = intent.getBundleExtra(MainActivity.EXTRA_MESSAGE).toString();
String tag = intent.getBundleExtra(MainActivity.EXTRA_TAG).toString();
String text = "null";
if (tag == "year_tag") {
DatabaseHandler db = new DatabaseHandler(this);
Goal goal = db.getYearGoal(message);
text = goal._title + "\n" + goal._description;
}
if (tag == "month_tag") {
DatabaseHandler db = new DatabaseHandler(this);
MonthGoal goal = db.getMonthGoal(message);
text = goal._title + "\n" + goal._description;
}
if (tag == "week_tag") {
DatabaseHandler db = new DatabaseHandler(this);
WeekGoal goal = db.getWeekGoal(message);
text = goal._title + "\n" + goal._description;
}
// create the text view
TextView textView = new TextView(this);
textView.setTextSize(10);
textView.setText(text);
// display the content
setContentView(textView);
}
}
It throws an error at me and I am not sure why. Any help is appreciated!
why don't you just do this, just put your EXTRA instead of using bundle:
...
Intent intent = new Intent(this, ViewGoal.class);
intent.putExtra( EXTRA_MESSAGE, button.getText().toString());
intent.putExtra( EXTRA_TAG, button.getTag().toString());
....
Later on in the other activity:
....
// get the message from the intent
Intent intent = getIntent();
String id = intent.getStringExtra(EXTRA_MESSAGE);
String name = intent.getStringExtra(EXTRA_TAG);
....
In onClick button, instead of this used classname.this
Some of my errors are removed by this. Try if it helps you.
public void openGoalWeek (View view) {
Intent intent = new Intent(className.this, ViewGoal.class);
Button button = (Button) findViewById(R.id.week_goal);
Bundle bundle = new Bundle();
bundle.putString(EXTRA_MESSAGE, button.getText().toString());
bundle.putString(EXTRA_TAG, button.getTag().toString());
intent.putExtras(bundle);
startActivity(intent);