我想在textView上保存一条消息,而不仅仅是在另一个活动中显示它。例如,如果应用程序在下次打开时关闭,那么我想查看添加的消息。
我有两个活动。
Activity one
->我使用SharedPraferences将日期保存在变量NAME_RESTAURANT中,并发送了日期抛出方法'getMsg()'
Activity two
->我收到日期,我想将其放入名为etWelcomeToRestaurant2
的TextView中
日期由一个字符串表示,该字符串是我从第一个Activity中名为etDRestaurantName
的EditText中获得的。
我的问题是在SecondActivity
中没有显示日期。
保存日期以及将日期传输到其他活动的活动
public class AdminAreaActivity extends AppCompatActivity {
public static final String SHARED_PREFS = "sharedPrefs";
public static final String RESTAURANT_NAME = "restaurantName";
private String NAME_RESTAURANT;
private EditText etDRestaurantName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin_area);
etDRestaurantName = findViewById(R.id.etRestaurantName);
final Button bRestaurantChange = findViewById(R.id.bRestaurantChange);
bRestaurantChange.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!etDRestaurantName.getText().toString().matches("")){
Intent mainIntent = new Intent(AdminAreaActivity.this,MainActivity.class);
saveData();
loadData();
etDRestaurantName.getText().clear();
startActivity(mainIntent);
}
else
{
AlertDialog.Builder builder = new AlertDialog.Builder(AdminAreaActivity.this);
builder.setMessage("Failed!")
.setNegativeButton("Retry", null)
.create()
.show();
}
}
});
}
public void saveData(){
SharedPreferences sharedPreferences =getSharedPreferences(SHARED_PREFS,MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(RESTAURANT_NAME,etDRestaurantName.getText().toString()+"!");
editor.apply();
Toast.makeText(this,"Data saved!",Toast.LENGTH_SHORT);
}
public void loadData(){
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS,MODE_PRIVATE);
NAME_RESTAURANT = sharedPreferences.getString(RESTAURANT_NAME,"Your Restaurant here!");
}
public String getMsg(){
return NAME_RESTAURANT;
}
}
我想放置数据和接收数据的地方的活动:
public class MainActivity extends AppCompatActivity {
private TextView etWelcomeToRestaurant2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView etWelcomeToRestaurant = findViewById(R.id.etWelcomeToRestaurant);
String messg = "Welcome to,\n";
etWelcomeToRestaurant.setText(messg);
etWelcomeToRestaurant2 = findViewById(R.id.etWelcomeToRestaurant2);
AdminAreaActivity admOBj = new AdminAreaActivity();
etWelcomeToRestaurant2.setText(((AdminAreaActivity)admOBj).getMsg());
}
}
答案 0 :(得分:0)
在Android中,永远不要将new运算符与Activity类一起使用。
MainActivity应该改为打开相同的共享首选项文件,并使用AdminAreaActivity中使用的相同密钥读取值。您已经具有首选项文件名和密钥作为公共静态变量,因此可以从MainActivity引用这些变量。
更好的是,创建一个帮助程序类来管理首选项。像PreferenceHelper这样称呼它,它看起来像这样:
public class PreferenceHelper {
public static final String SHARED_PREFS = "sharedPrefs";
public static final String RESTAURANT_NAME = "restaurantName";
public static String getResturantName(Context context) {
return context.getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE).getString(RESTAURANT_NAME,"Your Restaurant here!");
}
public static void setResturantName(Context, String name) {
context.getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE).edit().putString(RESTAURANT_NAME, name).apply();
}
}
现在,您可以从两个班级中呼叫您的助手。
答案 1 :(得分:0)
SharedPreferences可能是错误的数据存储方式,因为“它正在使用昂贵的操作,可能会使应用变慢。”看一下Room进行存储。
回答您的问题:
在下面查看我的修复程序
AdminAreaActivity
public class AdminAreaActivity extends AppCompatActivity {
public static final String SHARED_PREFS = "sharedPrefs";
public static final String RESTAURANT_NAME = "restaurantName";
private EditText etDRestaurantName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin_area);
etDRestaurantName = findViewById(R.id.etRestaurantName);
final Button bRestaurantChange = findViewById(R.id.bRestaurantChange);
bRestaurantChange.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!etDRestaurantName.getText().toString().matches("")) {
Intent mainIntent = new Intent(AdminAreaActivity.this, MainActivity.class);
saveData(RESTAURANT_NAME, etDRestaurantName.getText().toString() + "!");
etDRestaurantName.getText().clear();
startActivity(mainIntent);
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(AdminAreaActivity.this);
builder.setMessage("Failed!").setNegativeButton("Retry", null).create().show();
}
}
});
}
public void saveData(String prefName, String prefValue) {
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(prefName, prefValue);
editor.apply();
Toast.makeText(this, "Data saved!", Toast.LENGTH_SHORT);
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
private TextView etWelcomeToRestaurant2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView etWelcomeToRestaurant = findViewById(R.id.etWelcomeToRestaurant);
String messg = "Welcome to,\n";
etWelcomeToRestaurant.setText(messg);
etWelcomeToRestaurant2 = findViewById(R.id.etWelcomeToRestaurant2);
etWelcomeToRestaurant2.setText(loadData(AdminAreaActivity.RESTAURANT_NAME, "Your Restaurant here!"));
}
public String loadData(String prefName, String defValue){
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS,MODE_PRIVATE);
return sharedPreferences.getString(prefName, defValue);
}
}