Android select from selectors then display on map - java

First screen user is ask to select different buttons that are categories then after the categories are selected display the locations in a map.
For example user selects to search for restaurants and bars then click search. Then the next screen will show icons with the locations in the map.
Im confused on how to pass the data from my mainActivity to the MapActivity.
I try to do a loop for each button: but it displays all the buttons I only want to display the selected ones.
----------------Activityone----------------
if (toggleButton5.isChecked()) {
double point3=37326489;
double point4=-121772461;
Bundle b = new Bundle(); // create a bundle to pass to map activity
b.putDouble("point3",point3); // bundle the geo points for the map activity and convert to 1e6
b.putDouble("point4",point4);
Intent myIntent = new Intent(view.getContext(), Map.class); // create the intent to launch the map
myIntent.putExtras(b); // attach bundle to map activity
startActivityForResult(myIntent, 0); // start map activity
//map things!
}
----------------MapAcivity----------------
Bundle b = this.getIntent().getExtras();
Drawable drawable1 = this.getResources().getDrawable(R.drawable.button_food_on);
OverlayMap itemizedoverlay2 = new OverlayMap(drawable1, this);
GeoPoint point2 = new GeoPoint((int)(b.getDouble("point3")),(int) (b.getDouble("point4")));
OverlayItem overlayitem1 = new OverlayItem(point2, "", "");
itemizedoverlay2.addOverlay(overlayitem1);
mapOverlays.add(itemizedoverlay2);
Edit:
first here is an image of the first screen:

You're trying to do a lot of things here. Parsing xml in android is not trivial. Filtering a list of displayable icons in android is not trivial. When asked individually, answers to these problems are easy to find on this website.
I'll help you with one of the trivial problems to get you started on your way, though.
The reason you're not getting the variables that you want to filter to your MapActivity is because you aren't sending them.
Before you start your intent (and after you declare it) put the variables you want to filter in an Extra, possibly as an array of strings.
String[] searchFilter = {"restaurants", "bars"}; //build this array of variables from your buttons, this is just a static example
Intent myIntent3 = new Intent(view.getContext(), MapActivity.class);
myIntent3.putExtra("yourFilter", searchFilter);
startActivityForResult(myIntent3, 1);
Then in your MapActivity you can extract that data that you sent to it from the OnCreate or OnResume function (though really you can call it anywhere inside the activity).
String[] filterYouJustSent = this.getIntent().getStringArrayExtra("yourFilter");
filterYoujustSent in MapActivity should now have {"restaurants", "bars"} as its data.
If you didn't attach anything to that named extra, filterYouJustSent will be null.

Related

How can I use a button to switch between the projected listview items within a textview?

I have a listview (for player names entered in the edittext on my loginpage). My second activity page (the actual game refers to those player names (projects the first of the list top of the screen as can be seen here: https://gyazo.com/7d8f003cc28f02343715edc66af1d819).
In order to do so, I created the following intent on the loginpage:
public void openActivity2() {
Intent intent = new Intent(this, Gamescreen.class);
String s= list.get(0);
intent.putExtra("Name1", s);
startActivity(intent);
}}
In the game screen I refer to this as such:
Intent iin= getIntent();
Bundle b = iin.getExtras();
if(b!=null)
{
String j =b.getString("Name1");
playerturn.setText(j);
}
Basically what I want is that when I press the button (next player) on the gamescreen that it moves from the first player name on the list to the second. As such as you keep clicking the next player button -> you move down the list.
I will need to make an onclicklistener for the click of the button, but how do I make the rest possible, with the current way I have approached it? Help much appreciated!

share data in TextView in activity one direct to calculation formula in activity two [duplicate]

This question already has answers here:
How do I pass data between Activities in Android application?
(53 answers)
Closed 5 years ago.
i make app with 2 activity 1 and 2 and want to share number wrote in text view in page activity 1 direct in calculation in page activity 2 to be viewed in text view in this page 2 (for example if first text view contains number 10 and i want to use this number in second activity in calculation like "value+10" and the result will be viewed in text view in page activity 2) without make any addition field in page 2
You could pass it in a Bundle
Intent intent = new Intent(this, OtherActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("something", 1);
intent.putExtras(bundle);
startActivity(intent);
In you OtherActivity:
Bundle bundle = getIntent().getExtras();
int something;
if (bundle != null) {
something = bundle.getInt("something");
}else{
//
}
This way you can pass a bunch of arguments to the next activity.
Code In Activity1
Intent intent = new Intent(this, Activity2.class);
intent.putExtra("number", 10);
startActivity(intent);
Code In Activity2
int number = getIntent().getIntExtra("number", 0);

Android advice on creating new Layout in Activity and dynamically changing items inside Layout

I have an Activity from which I get some input that I must use afterwards. I get an Integer number from a NumberPicker and an ArrayList<String> from a multiple choice AlertDialog. In that Activity I also have a button that starts the "game". When the button is clicked I want to open a new Activity so I can use the Integer number and the ArrayList but I will lose them if I go to a new Activity. So my guess is I should create a new Layout from the current Activity and handle the input there. I need some TextViews and a counter that can be dynamically changed on button click. For example the first TextView will be the first element of that ArrayList. On button clicks the counter must add 1 (counter += 1). When the counter equals the Integer number from the NumberPicker (user input) the TextView must change from ArrayList(first element) to ArrayList(second element). I don't need help on the logic. This was just to clarify things. I need to know how to do that with a layout dynamically. I need some guidance how can I do that or is that the way to go (layout not Activity). Tutorial links/examples/advice will help me a lot.
When the button is clicked I want to open a new Activity so I can use
the Integer number and the ArrayList but I will lose them if I go to a
new Activity.
Look into the putStringArrayListExtra() method in the Intent class. This will allow you to pass your ArrayList to the new Activity:
List<String> stringArrayList = new ArrayList<>();
int intValue = 5;
Intent i = new Intent(ActivityOne.this, ActivityTwo.class);
i.putExtra("int_key", intValue);
i.putStringArrayListExtra("string_key", stringArrayList);
startActivity(i);

Switching between activities without data loss

I'm working on an app that has two activities.
When I go from one activity to another then return I loss the data.
Imagine:
First activity shows a random number on a text view that changes on a button click
Second activity does nothing it only has a return to the main activity button
*I know this app is worthless but its easier than showing you my large application...
So you have a random number on the textview, I go to the next activity then return to the main activity and the textview loses the number and shows another.
What the question is, is how to return to the activity without lossing the data (random number)?
I dont want to save it on a database or on the sharedprefes since that wouldn't be good when being used in a large app with many data shown...
Thx in advance
Edit:
Code to act1:
//public clas... oncreate...
Onclick(...){
Intent intent = New intent (this, acttwo.class);
StartActivity (intent);
Finish ();
}
Code to act2:
//public clas... oncreate...
Onclick(...){
Intent intent = New intent(this, actone.class);
StartActivity (intent);
Finish ();
}
Your problem cause, because
You are calling finish() in Activity 1 when you starting Activity 2.
So remove finish() from Activity 1. and implement onActivityResult()
Like,
Onclick(...){
Intent intent = New intent (this, acttwo.class);
StartActivityForResult(intent, 0); // Result to ensure about back track of proper activity
}
Now Activity 2 just call finish() no need to call start Activity 1 again as Activity 1 is already in back stack of Application task.
So it should be,
//public clas... oncreate... of acttwo
Onclick(...){
finish(); // We are just finishing Activity 2 not starting Activity 1 again
}
Note:
In future it may happen Android system may kill your Application on Low Memory issue, so better to save your data and state of activity in SharedPreferences or Database.
You need to save your data one way or another. Without using DB or other classical methods(shared preferences) used for saving data in Android, as I see it, you are left with two options:
Pass the data with Intents as extras
Save it using method described in Accepted Answer at Saving Activity state in Android
You could use a static variable in your Activity to hold whatever random number your TextView is meant to hold and set its text to that every onResume() call. Something like;
class MyActivity extends Activity {
private static int randomNumber;
private TextView myTextView;
...
#Override
public void onResume(Bundle b){
super.onResume(b);
myTextView.setText(randomNumber);
}
}
Your TextView should persist the number that you had before. If you were setting these values in the onResume() method, then they would change every time the activity resumes. If you terminate your activity with a call to finish (while calling B) and then start a new activity A again (from B), then it will show a random number
If you have to do either of the methods I mentioned, you can add the values you want to persist in an intent as an extra, pass this to the second activity and then pass it back like so:
// Start activity B and pass the value you want to save in there
Intent A = new Intent(this, ActivityB.class);
A.putExtra("value", number);
// Start activity A from B and pass the value you saved in there
Intent B = new Intent(this, ActivityA.class);
B.putExtra("value", getIntent().getExtra("value");
Then retrieve number and set the TextView in your onResume (or onCreate if you are starting a fresh activity. But in this case do a check to see if the intent starting this activity has an extra named value - if yes, then display it else generate a new random number).

switch activity and send string as an extra to another activity

I have a TabLayoutObjectActivity that shows 2 tabs with a different activity(TabActivity1 & TabActivity2).
I have another activity called ObjectActivity. It contains an array and if it is clicked I want the app to change to the TabLayoutObjectActivity (which shows TabActivity1 first) and sends a string from an array that clicked to TabActivity1.
I have tried some code but the app always wants to force close after I click one of the array list.
Intent i = null;
i = new Intent(this, TabLayoutObjekActivity.class);
Intent ii = null;
ii = new Intent(this, TabActivity1.class);
ii.putExtra("name", String.valueOf(menuArray[position].toString()));
startActivity(i);
startActivity(ii);
Please give me your opinion how to do this. Thanks :)
Refer to this. It explains how to solve your problem in detail. onclick even you can handle the issue.

Categories

Resources