Мне бы очень понравилась помощь (новая для android).Передача информации между несколькими действиями
В настоящее время у меня есть 4 действия. Первые три действия имеют в них поля EditText, а затем окончательное действие отображает всю информацию, которая была введена до сих пор. Кажется, достаточно просто, не так ли?
Итак, посмотрев на этом сайте, я обнаружил, что для этого нужно использовать putExtra() и getExtra();
Это не совсем похоже на работу, так как все вещи, которые я добавляю в hashset extras, недоступны в конечном действии. Любой совет?
Благодаря
код ниже для помощи
package com.example.smallchangebigloss;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class EnterName extends Activity {
private EditText edit;
private Bundle extras = getIntent().getExtras();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_enter_name);
edit = (EditText) findViewById(R.id.nameEdit);
}
public Bundle getExtras(){
return extras;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.enter_name, menu);
return true;
}
public void next(View view) {
if (edit.getText().toString().equals("")) {
new AlertDialog.Builder(this).setTitle("Ut Oh!")
.setMessage("Please enter your name.")
.setNeutralButton("Try again", null).show();
}
else {
Intent i = new Intent(getApplicationContext(), CurrentWeight.class);
i.putExtra("name", edit.getText().toString());
startActivity(i);
}
}
}
package com.example.smallchangebigloss;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
общественный класс CurrentWeight расширяет активность {
private EditText edit;
private String name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_current_weight);
Bundle extras = getIntent().getExtras();
name = extras.getString("name");
edit = (EditText) findViewById(R.id.currentWeightEdit);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.current_weight, menu);
return true;
}
public void next(View view) {
if (edit.getText().toString().equals("")) {
new AlertDialog.Builder(this).setTitle("Ut Oh!")
.setMessage("Please enter your current weight.")
.setNeutralButton("Try again", null).show();
}
else {
Intent i = new Intent(getApplicationContext(),GoalWeight.class);
i.putExtra("name", name);
i.putExtra("current", edit.getText().toString());
startActivity(i);
}
}
}
package com.example.smallchangebigloss;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
общественный класс GoalWeight расширяет активность {
private EditText edit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_goal_weight);
Bundle extras = getIntent().getExtras();
System.out.println(extras.containsKey("name"));
extras.containsKey("current");
edit = (EditText) findViewById(R.id.currentWeightEdit);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.goal_weight, menu);
return true;
}
public void next(View view) {
if (edit.getText().toString().equals("")) {
new AlertDialog.Builder(this).setTitle("Ut Oh!")
.setMessage("Please enter your Goal Weight.")
.setNeutralButton("Try again", null).show();
}
else {
Intent i = new Intent(getApplicationContext(), Complete.class);
i.putExtra("goal", edit.getText().toString());
startActivity(i);
}
}
}
package com.example.smallchangebigloss;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
public class Complete extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
// TextView current = (TextView) findViewById(R.id.completeCurrentMod);
// current.setText("hi");
//// current.setText(extras.getString("current")); These lines break it
setContentView(R.layout.activity_complete);
System.out.println("name: " + extras.getString("name") + "Current weight: "
+ extras.getString("current") + "goal: " + extras.getString("goal"));//Only displaying last ones...bundle them everytime in each class?
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.complete, menu);
return true;
}
}
Каковы типы данных? –