英文:
Saving data from fragment to file AndroidStudio
问题
我有Fragment1和Fragment2。 Fragment1是EditTexts的一种形式,它将其数据保存到文本文件中。我希望用户能够在Fragment1和Fragment2之间来回滑动,每次滑动时,都将Fragment1中的数据保存到文件中。
到目前为止,我已经使用ViewPager将每个表单实现为新的片段,并使用addOnPageChangeListener
在用户滑动时保存文件。这个方法是有效的,但用户输入的数据为空。
以下是用户滑动以保存数据的当前代码:
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
//将数据保存到字符串数组中(这是一个小片段)
Details = new String[2];
//这行保存了!
Details[0] = "Details:\n\n";
//这行在文件中只包含“Detail1”
Details[1] = "Detail1: " + Detail1.getText().toString().trim() + "\n";
saveToTxtFile(Details);
}
@Override
public void onPageScrollStateChanged(int state) {
}
以及用于'saveToTxtFile'的代码:
private void saveToTxtFile(String[] Details) {
if (Details != null) {
try {
File myDir = new File(getFilesDir().getAbsolutePath());
FileWriter fw = new FileWriter(myDir + "/" + fileName(), false);
for (String Detail : Details) {
fw.write(Detail);
}
fw.close();
Toast.makeText(this, "Saved to " + getFilesDir() + "/" + fileName(), Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
}
}
我对Android Studio和Java都还不太熟悉,所以希望能得到一些适合初学者的提示。
英文:
I have Fragment1 and Fragment2. Fragment1 is a form of EditTexts which saves its data into a text file. I want the user to be able to swipe back and forth between Fragment1 and Fragment2, where each time they swipe, the data from Fragment1 is saved onto a file.
So far, I've implemented each form as a new fragment using ViewPager and used an addOnPageChangeListener
to save the file each time the user swipes. This works, but the user-input data is completely empty.
This is my current code for where the user swipes to save:
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
//Saves the data into an array of strings (this is a small snippet)
Details = new String[2];
//This line saves!
Details[0] = "Details:\n\n";
//This line contains only "Detail1" in the file
Details[1] = "Detail1: " + Detail1.getText().toString().trim() + "\n";
saveToTxtFile(Details);
}
@Override
public void onPageScrollStateChanged(int state) {
}
And my code for 'saveToTxtFile':
private void saveToTxtFile(String[] Details) {
if (Details != null) {
try {
File myDir = new File(getFilesDir().getAbsolutePath());
FileWriter fw = new FileWriter(myDir + "/" + fileName(), false);
for (String Detail : Details) {
fw.write(Detail);
}
fw.close();
Toast.makeText(this, "Saved to " + getFilesDir() + "/" + fileName(), Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
}
}
I'm pretty new to Android Studio and Java, so would appreciate some beginner-friendly tips.
答案1
得分: 0
我不会使用PageChangeListener,因为它的范围错误,会破坏使用Fragments的概念。错误的范围可能是导致获取字段文本为空,并且PageChangeListener触发太晚以无法从Fragment获取细节。
尝试使用FragmentStatePagerAdapter
作为您的适配器,详情请参阅:https://developer.android.com/reference/androidx/fragment/app/FragmentStatePagerAdapter
因为它可以自动保存Fragment的状态以便稍后恢复(但不会将其保存到文件中)。
如果您真的想将其保存到文件中,那么在Fragment中触发保存操作,这将是正确的范围。
在当前版本的ViewPager
中,当Fragment不再是主要Fragment(屏幕上显示的Fragment)时,会调用Fragment的onPause
方法,因此将保存方法放在Fragment的onPause
中。
当Fragment成为主要Fragment时,会调用Fragment的onResume
方法,这是您可以从文件执行“还原”的地方。
请注意,在旧的ViewPager
版本中,这种生命周期行为必须进行配置,但现在它是默认行为。
英文:
I would not use a PageChangeListener as this is in the wrong scope and breaks the concept of using Fragments. The wrong scope is probably why getting the text of the field returns empty and the PageChangeListener is triggered too late to get details from the Fragment.
Try using FragmentStatePagerAdapter
for your adapter https://developer.android.com/reference/androidx/fragment/app/FragmentStatePagerAdapter
As this can automatically save the state of the Fragment for later restore (It won't save it to a file though).
If you really do want to save it to a file then trigger the saving in the Fragment, this will be the right scope.
In the current version of ViewPager
the Fragment's onPause
method is called when it is no longer the primary Fragment (the one on screen), so put your save method in the Fragment's onPause
.
The Fragment's onResume
method is called when a Fragment becomes the primary Fragment, this is where you would do a "Restore" from file.
Note that on old Viewpager
versions, this lifecycle behaviour had to be configure on, but now it is the default.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论