android - 如何在多个片段中显示保存的数据?
问题描述
介绍
大家好,目前我正在开发一个问题形式的应用程序。在 MainActivity 中,用户可以添加一个项目,之后会打开一个 QuestionListActivity。单击该列表中的第一项将打开 Main2Activity。此活动包含 3 个片段,均包含一个问题(Edittext)。用户在片段中实现的数据是可保存的。回答并保存问题后,每个表单都以列表的形式出现在 MainActivity 中。单击这些项目会将它们带回 QuestionListActivity,然后单击第一个项目应再次打开片段,并显示已保存的数据。
问题
保存片段后,MainActivity的字符串成功,例如。通过保存第一个片段(询问表单名称)设置的标题。因此,保存到我的实用程序类是成功的。问题是,单击 MainActivity 中的已保存项目,然后单击 QuestionList 中的第一个项目以打开 Main2Activity(带问题的片段),打开片段但带有空的 EditText 字段,应显示保存的数据以查看它们或做出改变。
问题
如何在多个片段中而不是在一个活动中显示保存的数据,我做错了什么?并且在使用 8 个问题(每个问题一个片段)时是否建议使用相同的格式?
(我在 StackOverFlow 上找不到合适的问题,因为几乎每个关于这个主题的问题都是关于实例状态的。但这在我的项目中不是问题)
代码
这是我的 Main2Activity 和一个片段(Frag1)。我设置的另一个片段与 Frag1 的设置方式相同。我可能在片段 java 类中做错了什么,但我不确定是什么。希望有人可以帮助我。
public class Main2Activity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
private String mNoteFileName;
private Note mLoadedNote;
private EditText title, question2, question3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
title = findViewById(R.id.note_et_title1);
question2 = findViewById(R.id.note_et_question2);
question3 = findViewById(R.id.note_et_question3);
String title;
String question2 ;
String question3;
mNoteFileName = getIntent().getStringExtra("NOTE_FILE");
if(mNoteFileName !=null && !mNoteFileName.isEmpty()) {
mLoadedNote = Utilities.getNoteByName(this, mNoteFileName);
if(mLoadedNote !=null) {
title = mLoadedNote.getTitle();
question2 = mLoadedNote.getQuestion2();
question3 = mLoadedNote.getQuestion3();
}
}
List<Fragment> fragments = new ArrayList<>();
fragments.add(Frag1.newInstance(title));
fragments.add(Frag2.newInstance(question2));
fragments.add(Frag3.newInstance(question3));
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(),fragments);
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_note_new, menu);
return true;
}
private void saveNote() {
Note note;
if(title.getText().toString().trim().isEmpty()){
Toast.makeText(this, "Please enter a title", Toast.LENGTH_SHORT).show();
}
if(mLoadedNote ==null) {
note = new Note(System.currentTimeMillis(), title.getText().toString(), question2.getText().toString(), question3.getText().toString());
}else {
note = new Note(mLoadedNote.getDateTime(), title.getText().toString(), question2.getText().toString(), question3.getText().toString());
}
if (Utilities.saveNote(this, note)){
Toast.makeText(this, "saved", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "not enough space", Toast.LENGTH_SHORT).show();
}
finish();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_note_save:
saveNote();
break;
}
return true;
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main2, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> mFragments;
public SectionsPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
mFragments = fragments;
}
@Override
public Fragment getItem(final int position) {
return mFragments.get(position);
}
@Override
public int getCount() {
return mFragments.size();
}
}
}
public class Frag1 extends Fragment {
private static final String EXTRA_TEXT = "text";
private EditText mEtTitle;
public static Frag1 newInstance(String message) {
Bundle args = new Bundle();
args.putString(EXTRA_TEXT, message);
Frag1 fragment = new Frag1();
fragment.setArguments(args);
return fragment;
}
public Frag1 () {
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag1_layout, container, false);
mEtTitle = (EditText) view.findViewById(R.id.note_et_title1);
Bundle bundle = getArguments();
if (bundle != null) {
mEtTitle.setText(bundle.getString(EXTRA_TEXT));
}
return view;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_green_light">
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:text="Vraag 1, bv naam" />
<EditText
android:id="@+id/note_et_title1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:ems="10"
android:gravity="top"
android:inputType="textMultiLine" />
解决方案
您应该使用 Shared Preferences 或 Database 来保存您的数据并显示它。
推荐阅读
- powershell - PowerShell 访问按枚举逐项列出的多维数组
- office-js - Mac 上的 Outlook 2019 中缺少 SeriesId
- wordpress - 为什么将 SSL 域添加到 WordPress 多站点会导致:“潜在的安全风险”警告
- java - 根据集合中的值过滤列表中的值
- c - C如何跨平台?
- couchbase - 与沙发底座的连接正在挂起
- c++ - C++ Win10以编程方式确定手动锁和OS锁?
- javascript - 如何在特定上下文中使用 querySelectorAll
- typescript - IE 上的 HierarchyRequestError 4 和 DOM 操作上的 Edge
- python-3.x - pathos.multiprocessing 有星图吗?