gpt4 book ai didi

java - 根据数据动态创建 View

转载 作者:行者123 更新时间:2023-12-01 09:05:16 25 4
gpt4 key购买 nike

我有名字、时间和经历。我将它们保存在一个屏幕中,并想将它们加载到另一个屏幕上。我认为,为每个人创建一个 View 将是一个很好的解决方案。但我愿意接受其他建议。这是我用来保存数据的代码。

            //Sacando el texto de los editText y guardandolos en sus variables
d_TaskName = etxt_TaskName.getText().toString();
d_TaskTime = etxt_TaskTime.getText().toString();
d_TaskExp = etxt_TaskExp.getText().toString();

// Guardar nombre de la tarea en TaskName.txt
try {
FileOutputStream fou1 = openFileOutput("TaskName.txt", MODE_WORLD_WRITEABLE);
OutputStreamWriter osw1 = new OutputStreamWriter(fou1);
try {
osw1.write(d_TaskName);
osw1.flush();
osw1.close();
} catch (IOException e) {
e.printStackTrace();
}

} catch (FileNotFoundException e) {
e.printStackTrace();
}

//Guardar tiempo que dura la tarea en TaskTime.txt
try {
FileOutputStream fou2 = openFileOutput("TaskTime.txt", MODE_WORLD_WRITEABLE);
OutputStreamWriter osw2 = new OutputStreamWriter(fou2);
try {
osw2.write(d_TaskTime);
osw2.flush();
osw2.close();
} catch (IOException e) {
e.printStackTrace();
}

} catch (FileNotFoundException e) {
e.printStackTrace();
}

// Guardar la experiencia de la tarea en TaskExp.txt
try {
FileOutputStream fou3 = openFileOutput("TaskExp.txt", MODE_WORLD_WRITEABLE);
OutputStreamWriter osw3 = new OutputStreamWriter(fou3);
try {
osw3.write(d_TaskExp);
osw3.flush();
osw3.close();
Toast.makeText(getBaseContext(), "Tarea Guardada", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}

} catch (FileNotFoundException e) {
e.printStackTrace();
}

最佳答案

为什么不使用 SharedPreferences?用于存储您的数据:

SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("TaskName", etxt_TaskName.getText().toString());
editor.apply();

加载数据:

SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String taskName = sharedPref.getString("TaskName", "");

关于java - 根据数据动态创建 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41330629/

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