gpt4 book ai didi

java - 保存不同 Activity 的数据

转载 作者:行者123 更新时间:2023-11-29 05:54:21 25 4
gpt4 key购买 nike

我在保存用户在我的 Android Activity 类中输入的数据时遇到了问题。我已经确定我需要使用 onSavedInstanceState(Bundle outState) 方法,但我的程序编写方式使这变得困难。

用户在DataEntry.java类中输入各种数据,提交的信息显示在DataSummary.java中。这很好用。

但是,当用户离开 DataSummary.java 时,要填写 DataEntry.java 上的其余信息,如果您离开,原始提交的数据将丢失回到 DS.java 看看你已经写了什么。下面是 DataSummary.java 的代码。

public class DataSummary extends Activity {

ImageView resultImage;
TextView resultName;
TextView resultDescription;
TextView resultType;
TextView resultProject;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data_summary);

//Check if there is anything in the 'bundle' and if not produce message - AVOIDS NULLPOINTEREXCEPTION when navigating to Activity
Bundle bundle = this.getIntent().getExtras();
if (bundle != null){
int image = bundle.getInt("image");
String name = bundle.getString("key");
String description = bundle.getString("key1"); //gets data from DataEntry activity
String type = bundle.getString("key2");
String project = bundle.getString("key3");

resultImage=(ImageView)findViewById(R.id.resultImage);
resultName=(TextView)findViewById(R.id.resultName); //adds the TextViews to the activity
resultType=(TextView)findViewById(R.id.resultType);
resultDescription=(TextView)findViewById(R.id.resultDesc);
resultProject=(TextView)findViewById(R.id.resultProject);

resultImage.setImageResource(image);
resultName.setText(name); // Fills the textviews with imported data
resultType.setText(type);
resultDescription.setText(description);
resultProject.setText(project);
}

else
{
Toast.makeText(DataSummary.this,"Received no data yet!", Toast.LENGTH_LONG).show();
}

}

/* MANAGES ACTIVITY LIFESTYLE */
public void onSavedInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
}

我如何扩展 onSavedInstanceState 方法以获取在创建 Activity 时收到的导入数据,并在用户导航离开此 Activity 时保留它?希望上面的解释足够好?

很难弄清楚如何使用 onCreate 中的变量,而且我无法从其他方法访问它们(我想如果我知道如何执行此操作,我就可以完成该方法)。

最佳答案

您可以在您的类上设置静态变量。当用户输入数据时,您只需将该数据写入变量即可。当用户返回应用程序时,您只需将存储在变量中的数据写入 EditText。

编辑:示例

假设您有 2 个 EditTexts et1 和 et2,它们存储的值是用户名和邮件。

public class DataEntryActivity extends Activity{

private static String username;
private static String mail;

@Override
public void onCreate(Bundle b){
super.onCreate(b);
setContentView(R.layout.layout_data_entry);
EditText et1 = (EditText) findViewById(R.id.editTextUsername);
EditText et2 = (EditText) findViewById(R.id.editTextMail);
if (username != null)
et1.setText(username);
if (mail != null)
et2.setText(mail);
Button save = (Button) findViewById(R.id.saveButton);
save.setOnClickListener(new OnClickListener(){

public void onClick(View v){

if (et1.getText().toString().length() > 0)
username = et1.getText().toString();
if (et2.getText().toString().length() > 0)
mail = et2.getText().toString();
Intent intent = new Intent(DataEntryActivity.this, YourOtherActivity.class);
startActivity(intent);
}

});

}


public static String getUsername(){
return username;
}

public static String getMail(){
return mail;
}

}

从您的其他 Activity 中,您只需调用 DataEntryActivity.getUsername() 和 DataEntryActivity.getMail() 来取回它们。

关于java - 保存不同 Activity 的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12743215/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com