Jump to content

Activity multiple

annasvensson

Hi,
I do not got this correct. I want to make two choices and write the choices in the main activity window.

The idea is to make the first choice (activity) and a new window pops up. Make the second choice (activity) and the total results, first and second activity print out in the main window.
Can you pls help me? 

MainActivity.java file
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private final int WHISKY_ID = 1;
private final int DAYS_ID = 2;
@override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void chooseDay(View caller)
{
String instruction = "Please select your whisky-weekday from the list below:";
String buttonText = "OK";
String[] array = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };
Intent intent = new Intent(this, com.goran.dayandwhisky.ChooserActivity.class);
intent.putExtra(ChooserActivity.PARAM_INSTRUCTION, instruction);
intent.putExtra(ChooserActivity.PARAM_CHOICES, array);
intent.putExtra(ChooserActivity.PARAM_BTN_TEXT, buttonText);
startActivityForResult(intent, DAYS_ID);
}
public void chooseWhisky(View caller)
{
String instruction = "Please select your favourite whisky from the list below:";
String buttonText = "Done";
String[] array = { "Lagavulin", "Laphroaig", "Caol Ila", "Ardbeg", "Bowmore" };
Intent intent = new Intent(this, com.goran.dayandwhisky.ChooserActivity.class);
intent.putExtra(ChooserActivity.PARAM_INSTRUCTION, instruction);
intent.putExtra(ChooserActivity.PARAM_CHOICES, array);
intent.putExtra(ChooserActivity.PARAM_BTN_TEXT, buttonText);
startActivityForResult(intent, WHISKY_ID);
}
public void quit(View caller)
{
finish();
}
@override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK)
{
String result = data.getStringExtra(ChooserActivity.RETURN_VALUE);
TextView textView = null;
if(requestCode==DAYS_ID)
textView = this.findViewById(R.id.textView1);
else if(requestCode==WHISKY_ID)
textView = this.findViewById(R.id.textView2);
if (textView != null)
textView.setText(result);
}
}
}


ChooseActivity.java file
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
public class ChooserActivity extends AppCompatActivity {
public static final String PARAM_INSTRUCTION = "instr";
public static final String PARAM_CHOICES = "choices";
public static final String PARAM_BTN_TEXT = "btnText";
public static final String RETURN_VALUE = "return";
private ArrayAdapter<String> adapter;
private Spinner spinner;
private String selectedString = "";
@override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chooser);
TextView txt = findViewById(R.id.textView1);
String instruction = this.getIntent().getStringExtra(PARAM_INSTRUCTION);
if (instruction != null)
txt.setText(instruction);
Button btn = findViewById(R.id.button1);
String btnText = this.getIntent().getStringExtra(PARAM_BTN_TEXT);
if (btnText != null)
btn.setText(btnText);
String[] array = this.getIntent().getStringArrayExtra(PARAM_CHOICES);
if (array != null)
setUpTheSpinner(array);
}
private void setUpTheSpinner(String[] array)
{
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, array);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner = findViewById(R.id.spinner1);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id){
selectedString = spinner.getSelectedItem().toString();
}
public void onNothingSelected(AdapterView<?> parent) {}
});
}
public void quit(View caller)
{
Intent returnIntent = new Intent();
returnIntent.putExtra(RETURN_VALUE, selectedString);
setResult(RESULT_OK, returnIntent);
finish();
}
}

Best regards

/Anna

Link to comment
Share on other sites

Link to post
Share on other sites

==moved to programing==

please update this with the code tag

Good luck, Have fun, Build PC, and have a last gen console for use once a year. I should answer most of the time between 9 to 3 PST

NightHawk 3.0: R7 5700x @, B550A vision D, H105, 2x32gb Oloy 3600, Sapphire RX 6700XT  Nitro+, Corsair RM750X, 500 gb 850 evo, 2tb rocket and 5tb Toshiba x300, 2x 6TB WD Black W10 all in a 750D airflow.
GF PC: (nighthawk 2.0): R7 2700x, B450m vision D, 4x8gb Geli 2933, Strix GTX970, CX650M RGB, Obsidian 350D

Skunkworks: R5 3500U, 16gb, 500gb Adata XPG 6000 lite, Vega 8. HP probook G455R G6 Ubuntu 20. LTS

Condor (MC server): 6600K, z170m plus, 16gb corsair vengeance LPX, samsung 750 evo, EVGA BR 450.

Spirt  (NAS) ASUS Z9PR-D12, 2x E5 2620V2, 8x4gb, 24 3tb HDD. F80 800gb cache, trueNAS, 2x12disk raid Z3 stripped

PSU Tier List      Motherboard Tier List     SSD Tier List     How to get PC parts cheap    HP probook 445R G6 review

 

"Stupidity is like trying to find a limit of a constant. You are never truly smart in something, just less stupid."

Camera Gear: X-S10, 16-80 F4, 60D, 24-105 F4, 50mm F1.4, Helios44-m, 2 Cos-11D lavs

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×