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;
}
Related
Is there any way to check if an extra has been passed when starting an Activity?
I would like to do something like (on the onCreate() in the Activity):
Bundle extras = getIntent().getExtras();
String extraStr = extras.getString("extra");
if (extraStr == null) {
extraStr = "extra not set";
}
But this is throwing a java.lang.NullPointerException.
Thank you.
Use the Intent.hasExtra(String name) to check if an extra with name was passed in the intent.
Example:
Intent intent = getIntent();
if (intent.hasExtra("bookUrl")) {
bookUrl = b.getString("bookUrl");
} else {
// Do something else
}
Also, use Intent.getStringExtra(String name) directly on the intent to handle the NullPointerException if no extras were passed.
Well, I had similiar problem. in my case the null point exception was happen when I checked if my bundle.getString() was equall to null.
here is how IN MY CASE I solved it:
Intent intent = getIntent();
if(intent.hasExtra("nomeUsuario")){
bd = getIntent().getExtras();
if(!bd.getString("nomeUsuario").equals(null)){
nomeUsuario = bd.getString("nomeUsuario");
}
}
if (this.getIntent().getExtras() != null && this.getIntent().getExtras().containsKey("yourKey")) {
// intent is not null and your key is not null
}
I think you need to check when extras != null
Bundle extras = getIntent().getExtras();
if (extras != null) {
String extraStr = extras.getString("extra");
}else {
extraStr = "extra not set";
}
I would use this solution in your case.
String extraStr;
try {
extraStr = getIntent().getExtras().getString("extra");
} catch (NullPointerException e ) {
extraStr = "something_else";
}
Working Code
If you want to check first Intent have no extra
Intent intent = getIntent();
if (intent.getExtras() == null){
startActivity(new Intent(Splash.this, Main.class));
overridePendingTransition(R.anim.enter, R.anim.exit);
finish();
}else {
if (intent.hasExtra("type")) {
String type = getIntent().getStringExtra("type");
switch (type){
case "showRateUsDialog":
Intent i = new Intent(Splash.this, Main.class);
i.putExtra("type", "showRateUsDialog");
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
overridePendingTransition(R.anim.enter, R.anim.exit);
finish();
break;
case "refer":
Intent i2 = new Intent(Splash.this, Refer.class);
i2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i2);
overridePendingTransition(R.anim.enter, R.anim.exit);
finish();
break;
default:
startActivity(new Intent(Splash.this, Main.class));
overridePendingTransition(R.anim.enter, R.anim.exit);
finish();
}
}
}
}
here Heloo guys im working on an app with auto suggestion and search the problem is when i type a letter in the searchfield and the suggestion changes still the items in the search launches the same intents according to ther position and not the actual intended synonym class how can i set specific intent on the search entries such that even if the position changes still the same activity is launched by a diffrent search entry whose name corresponds with the search result
first screenshot
second screen
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the search menu action bar.
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu_search, menu);
// Get the search menu.
MenuItem searchMenu = menu.findItem(R.id.app_bar_menu_search);
searchView = (SearchView) MenuItemCompat.getActionView(searchMenu);
mSearchAutoComplete = (SearchView.SearchAutoComplete) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
mSearchAutoComplete.setThreshold(0);
mSearchAutoComplete.setBackgroundColor(getResources().getColor(R.color.tabcolor));
mSearchAutoComplete.setTextColor(Color.BLACK);
mSearchAutoComplete.setDropDownBackgroundResource(android.R.color.darker_gray);
/* Create a new ArrayAdapter and add data to search auto complete object.
how can i set each word when onclicked to open the corresponding class no matter the position
*/
String dataArr[] = {"Kiambu county", "Kisumu county", "Kitui county", "Laikipia county", "Lamu county", "Meru county", "Mombasa county", "Muranga county", "Nairobi county", "Nakuru county", "Narok county", "kajiado county", "Uansingishu county", "Makueni County", "Machakos county"};
ArrayAdapter<String> newsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, dataArr);
mSearchAutoComplete.setAdapter(newsAdapter);
// Listen to search view item on click event.
mSearchAutoComplete.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if (i == 0) {
Intent intent = new Intent(County.this, Kiambu.class);
startActivity(intent);
}
if (i == 1) {
Intent intent = new Intent(County.this, Kisumu.class);
startActivity(intent);
}
if (i == 2) {
Intent intent = new Intent(County.this, Kitui.class);
startActivity(intent);
}
if (i == 3) {
Intent intent = new Intent(County.this, Laikipia.class);
startActivity(intent);
}
if (i == 4) {
Intent intent = new Intent(County.this, Lamu.class);
startActivity(intent);
}
if (i == 5) {
Intent intent = new Intent(County.this, Meru.class);
startActivity(intent);
}
if (i == 6) {
Intent intent = new Intent(County.this, Mombasa.class);
startActivity(intent);
}
if (i == 7) {
Intent intent = new Intent(County.this, Muranga.class);
startActivity(intent);
}
if (i == 8) {
Intent intent = new Intent(County.this, Nairobi.class);
startActivity(intent);
}
if (i == 9) {
Intent intent = new Intent(County.this, Nakuru.class);
startActivity(intent);
}
if (i == 10) {
Intent intent = new Intent(County.this, Narok.class);
startActivity(intent);
}
if (i == 11) {
Intent intent = new Intent(County.this, Kajiado.class);
startActivity(intent);
}
if (i == 12) {
Intent intent = new Intent(County.this, Singishu .class);
startActivity(intent);
}
if (i == 13) {
Intent intent = new Intent(County.this, Makueni.class);
startActivity(intent);
}
if (i == 14) {
Intent intent = new Intent(County.this, Machakos.class);
startActivity(intent);
}
}
});
return super.onCreateOptionsMenu(menu);
}
help out im really stuck
The problem is you have added condition base on i which gives the current position of adapter item clicked.
So, when you search something, it filters the data and displays it accordingly, and when you click on say 1st item, it will always open Kiambu class.
You need to update your condition inside your onItemClick method to something like:
if (newsadapter.getItem(i).equals(dataArr[0])) {
Intent intent = new Intent(County.this, Kiambu.class);
startActivity(intent);
} else if (newsadapter.getItem(i).equals(dataArr[1])) {
Intent intent = new Intent(County.this, Kisumu.class);
startActivity(intent);
} else if (newsadapter.getItem(i).equals(dataArr[2])) {
Intent intent = new Intent(County.this, Kitui.class);
startActivity(intent);
} else if (newsadapter.getItem(i).equals(dataArr[3])) {
Intent intent = new Intent(County.this, Laikipia.class);
startActivity(intent);
} else if (newsadapter.getItem(i).equals(dataArr[4])) {
Intent intent = new Intent(County.this, Lamu.class);
startActivity(intent);
} else if (newsadapter.getItem(i).equals(dataArr[5])) {
Intent intent = new Intent(County.this, Meru.class);
startActivity(intent);
} else if (newsadapter.getItem(i).equals(dataArr[6])) {
Intent intent = new Intent(County.this, Mombasa.class);
startActivity(intent);
} else if (newsadapter.getItem(i).equals(dataArr[7])) {
Intent intent = new Intent(County.this, Muranga.class);
startActivity(intent);
} else if (newsadapter.getItem(i).equals(dataArr[8])) {
Intent intent = new Intent(County.this, Nairobi.class);
startActivity(intent);
} else if (newsadapter.getItem(i).equals(dataArr[9])) {
Intent intent = new Intent(County.this, Nakuru.class);
startActivity(intent);
} else if (newsadapter.getItem(i).equals(dataArr[10])) {
Intent intent = new Intent(County.this, Narok.class);
startActivity(intent);
} else if (newsadapter.getItem(i).equals(dataArr[11])) {
Intent intent = new Intent(County.this, Kajiado.class);
startActivity(intent);
} else if (newsadapter.getItem(i).equals(dataArr[12])) {
Intent intent = new Intent(County.this, Singishu.class);
startActivity(intent);
} else if (newsadapter.getItem(i).equals(dataArr[13])) {
Intent intent = new Intent(County.this, Makueni.class);
startActivity(intent);
} else if (newsadapter.getItem(i).equals(dataArr[14])) {
Intent intent = new Intent(County.this, Machakos.class);
startActivity(intent);
}
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.
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
i have a listview with 3 text. NV1 - NV2 - NV3. My problem is when I touch on NV1 the intent show me the layout of NV1. But with the others nothing happen. It just show me the same NV.
I create a 2 method for passing the info.
private void adapter (PT1Activity a){
this.a = a;
}
private void showGame(int nivel){
Intent intent = new Intent (PT1Activity.this, NV1.class);
intent.putExtra("nivel2", nivel);
startActivity(intent);
}
And a:
private PT1Activity a;
adapter(this);
ltNvs.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
a.showGame(position);
}
});
What I have to do? Pass the information to the other NV2 Activity with a Bundle?? Or something similar?
Your showgame function should be something like this:
private void showGame(int nivel){
Intent intent;
switch (nivel){
case 1:
intent = new Intent (PT1Activity.this, NV1.class);
intent.putExtra("nivel1", nivel);
break;
case 2:
intent = new Intent (PT1Activity.this, NV2.class);
intent.putExtra("nivel2", nivel);
break;
case 3:
intent = new Intent (PT1Activity.this, NV3.class);
intent.putExtra("nivel3", nivel);
break;
default:
intent = new Intent();
}
startActivity(intent);
}
However, this assumes all your classes are in the same package. If you want to launch an activity in a different package, the Intent(this, yourclass.Class) constructor won't work. Instead, try something like this:
Intent intent = new Intent();
intent.setComponent(ComponentName.unflattenFromString("your.other.package/your.other.package.your_other_class_name"));
startActivity(intent);
Note: your_other_class_name would be something like NV1, not NV1.class.
I have to be honest, I'm not completely sure what it is you're asking for. Hopefully looking at this you can get an idea.
private void showGame(int nivel){
switch( nivel ) {
case 1:
Intent intent = new Intent (PT1Activity.this, NV1.class);
intent.putExtra("nivel1", nivel);
startActivity(intent);
break;
case 2:
Intent intent = new Intent( PT1Activity.this, NV2.class );
intent.putExtra( "nivel2", nivel );
startActivity( intent );
break;
case 3:
Intent intent = new Intent( PT1Activity.this, NV3.class );
intent.putExtra( "nivel3", nivel );
startActivity( intent );
break;
}