我需要的很简单。
1-用户在EditTexts上输入一些信息。
2-使用intent和bundle将数据带到另一个活动
3-显示从EditTexts到TextViews存储的数据。
但是,这就是问题所在。 EditTexts位于MainActivity,接收数据的活动不是下一个,而是最后一个,名为finalizar_relatorio.class。
而且,当我调用一个方法时,我正在尝试将此包发送到下一个活动,因为如果我在onCreate中使用startActivity(),它将在按下play后立即启动该活动。我该如何从方法中调用startActivity()?
有4个数字EditTexts和一个char。
我应该使用哪个Bundle.putXX?
喜欢char:Bundle.putString("VariableBeingCalledInNextActivity", variableThatStoresEditTextdata);
我在这里尝试过其他问题,但我猜我的问题是当我将EditText数据保存到Bundle
MainActivity(更新)
package com.example.relatoriodeobras;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Toast;
import java.io.File;
public class MainActivity extends AppCompatActivity {
public int tipo;
SharedPreferences dadosprocesso;
public static final String PREFERENCES = "MyPrefs" ;
public static final String processo = "processo" ;
public static final String requerente = "requerente" ;
public static final String portas = "portas" ;
public static final String janelas = "janelas" ;
public static final String unhab = "unhab" ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText editText4 = (EditText)findViewById(R.id.editText4);
final EditText editText5 = (EditText)findViewById(R.id.editText5);
final EditText editText6 = (EditText)findViewById(R.id.editText6);
final EditText editText7 = (EditText)findViewById(R.id.editText7);
final EditText editText8 = (EditText)findViewById(R.id.editText8);
Spinner dynamicSpinner = (Spinner) findViewById(R.id.dynamic_spinner);
String[] items = new String[] { "Tipo de fiscalização","Alvará", "Habite-se" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, items);
dynamicSpinner.setAdapter(adapter);
dynamicSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String processo1 = editText4.getText().toString();
String requerente1 = editText5.getText().toString();
String portas1 = editText6.getText().toString();
String janelas1 = editText7.getText().toString();
String unhab1 = editText8.getText().toString();
dadosprocesso = getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = dadosprocesso.edit();
editor.putString(processo, processo1);
editor.putString(requerente, requerente1);
editor.putString(portas, portas1);
editor.putString(janelas, janelas1);
editor.putString(unhab, unhab1);
editor.commit();
Log.v("item", (String) parent.getItemAtPosition(position));
boolean fieldsOK = validate(new EditText[]{editText4,editText5,editText6,editText7,editText8});
if(fieldsOK) {
switch (position) {
case 1:
tipo = 1;
Intent intent = new Intent(MainActivity.this, alvara.class);
startActivity(intent);
break;
case 2:
tipo = 2;
Intent intent1 = new Intent(MainActivity.this, habitese.class);
startActivity(intent1);
break;
}
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
obraFolder();
}
public void obraFolder(){ //Criar a pasta do projeto e o diretório em que os projetos estarão conditos, caso não tenha sido criado.
EditText projectName = (EditText) findViewById(R.id.editText4);
String obraName = projectName.getText().toString(); //obraName é a variável String que define o nome da pasta do projeto
obraName = obraName.trim();
File myInternalFile;
String filepath = "Projetos" + obraName;
String filename = obraName + ".txt";
ContextWrapper contextWrapper = new ContextWrapper(getApplicationContext());
File directory = contextWrapper.getDir(filepath, Context.MODE_PRIVATE);
myInternalFile = new File(directory, filename);
}
private boolean validate(EditText[] fields){
for(int i=0; i<fields.length; i++){
EditText currentField=fields[i];
if(currentField.getText().toString().length()<=0){
Context context = getApplicationContext();
CharSequence text = "Atenção! Exitem campos obrigatórios vazios!!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
//postDelayed
return false;
}
}
startActivity(dadosdaobra);
return true;
}
}
finalizar_relatorio.java(更新)
package com.example.relatoriodeobras;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class finalizar_relatorio extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_finalizar_relatorio);
SharedPreferences dadosprocesso = getSharedPreferences("MyPrefs.xml", MODE_PRIVATE);
String restoredtext = dadosprocesso.getString("text", null);
if(restoredtext != null){
String processo = dadosprocesso.getString("processo", "processo");
String requerente = dadosprocesso.getString("requerente", "requerente");
String portas = dadosprocesso.getString("portas", "portas");
String janelas = dadosprocesso.getString("janelas", "janelas");
String unhab = dadosprocesso.getString("unhab", "unhab");
((TextView)findViewById(R.id.textView18)).setText(processo);
((TextView)findViewById(R.id.textView20)).setText(requerente);
((TextView)findViewById(R.id.textView22)).setText(portas);
((TextView)findViewById(R.id.textView24)).setText(janelas);
((TextView)findViewById(R.id.textView26)).setText(unhab);
}
}
}
答案 0 :(得分:0)
我可能会对您的问题感到困惑,但如果您在onCreate中启动活动,请尝试抓取覆盖的onResume()
e.g。
protected void onResume() {
String edittext4 = editText4.getText().toString();
String edittext5 = editText5.getText().toString();
String edittext6 = editText6.getText().toString();
String edittext7 = editText7.getText().toString();
String edittext8 = editText8.getText().toString();
Intent dadosdaobra = new Intent(MainActivity.this, finalizar_relatorio.class);
Bundle dados = new Bundle();
dados.putString("extra_processo", edittext5);
dados.putString("extra_requerente", edittext4);
dados.putString("extra_portas", edittext6);
dados.putString("extra_janelas", edittext7);
dados.putString("extra_unhab", edittext8);
dadosdaobra.putExtras(dados);
addSpinnerListeners();
}
private void addSpinnerListeners() {
dynamicSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Log.v("item", (String) parent.getItemAtPosition(position));
boolean fieldsOK = validate(new EditText[] {editText4,editText5,editText6,editText7,editText8});
if(fieldsOK) {
switch (position) {
case 1:
tipo = 1;
Intent intent = new Intent(MainActivity.this, alvara.class);
startActivity(intent);
break;
case 2:
tipo = 2;
Intent intent1 = new Intent(MainActivity.this, habitese.class);
startActivity(intent1);
break;
}
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
答案 1 :(得分:0)
使用共享偏好来共享偏好并从中检索。
答案 2 :(得分:0)
try This :-
Write this code in your main activity :-
public static final String KEY_PREFERNCE = "prefernce";
public static final String KEY_ID = "id";
SharedPrefernce shraedprefernce = MainActivity.this.getSharedPrefernce(KEY_PREFERNCE ,PRIVATEMODE);
SharedPrefernce.Editor editor = shraedprefernce.edit();
editor.putString(KEY_ID ,youredittext.getText.toString);
editor.commit;
the following code used in your last activity:-
SharedPrefernce shraedprefernce = YourActivity.this.getSharedPrefernce(MainActivity.KEY_PREFERNCE ,PRIVATEMODE);
String data = shraedprefernce.getString(MainActivity.KEY_ID,"");
yourTextView.setData(data);
Don't forget to commit the editor in your main activity.