在 activity (SQLite) 中修改数据后如何更新片段?
How do I update my Fragment after data modification in my activity (SQLite)?
我在应用程序启动时填充的片段中有一个 ListView。
我在我的 newInstance 方法中将一个 ParcelableArrayList 放入一个 Bundle 中,然后在我的 Activity(这是从 SQLite 数据库读取的数据)的 newInstance 方法中传递 ArrayList 后,我将它取回我的 OnCreateView。
这部分有效,因为我在 Fragment 中正确显示了我的数据。
我实现了一个从 table 中删除所有数据的按钮,现在我想在清理 table 后更新我的视图。
删除所有按钮在我的主 activity 中处理,我调用我的数据库处理程序来清空 table.
最好的方法是什么?以下是与我相关的代码部分:
我的片段class:
public class MainFragment extends Fragment {
public static final String RECETTES_KEY = "recettes_key";
private List<Recette> mRecettes;
private ListView mListView;
public MainFragment() {
// Required empty public constructor
}
public static MainFragment newInstance(List<Recette> r) {
MainFragment fragment = new MainFragment();
Bundle bundle = new Bundle();
bundle.putParcelableArrayList(RECETTES_KEY, (ArrayList<? extends Parcelable>) r);
fragment.setArguments(bundle);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mRecettes = getArguments().getParcelableArrayList(RECETTES_KEY);
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_main, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
configureListView();
}
// Configure ListView
private void configureListView(){
this.mListView = getView().findViewById(R.id.activity_main_list_view);
RecetteAdapter adapter = new RecetteAdapter(getContext(), mRecettes);
mListView.setAdapter(adapter);
}
}
我主要活动的相关部分:
这是在我的 OnCreate 方法中:
mDatabaseHandler = new DatabaseHandler(this);
mRecettes = mDatabaseHandler.readRecettes();
mDatabaseHandler.close();
这是我用来显示片段的方法:
if (this.mMainFragment == null) this.mMainFragment = MainFragment.newInstance(mRecettes);
this.startTransactionFragment(this.mMainFragment);
让我知道是否应该添加更多我的代码,这是我第一次发帖 :)
露西尔
在您的 R.layout.fragment_main
中,您可以将 id 添加到根视图,例如 android:id@+id/fragment_root
以及每当您想要更改片段视图时:
在activity中:
MainFragment fragment = (MainFragment) getSupportFragmentManager().getFragmentById(R.id.fragment_root);
fragment.updateList(mRecettes);
然后在 MainFragment
中创建新方法 updateList()
public void updateList(List<Recette> recettes) {
mRecettes.clear();
mRecettes.addAll(recettes);
configureListView();
}
您还可以在将片段添加到交易时标记片段而不是使用其 id,然后使用 getSupportFragmentManager().getFragmentByTag()
我在应用程序启动时填充的片段中有一个 ListView。 我在我的 newInstance 方法中将一个 ParcelableArrayList 放入一个 Bundle 中,然后在我的 Activity(这是从 SQLite 数据库读取的数据)的 newInstance 方法中传递 ArrayList 后,我将它取回我的 OnCreateView。
这部分有效,因为我在 Fragment 中正确显示了我的数据。
我实现了一个从 table 中删除所有数据的按钮,现在我想在清理 table 后更新我的视图。 删除所有按钮在我的主 activity 中处理,我调用我的数据库处理程序来清空 table.
最好的方法是什么?以下是与我相关的代码部分:
我的片段class:
public class MainFragment extends Fragment {
public static final String RECETTES_KEY = "recettes_key";
private List<Recette> mRecettes;
private ListView mListView;
public MainFragment() {
// Required empty public constructor
}
public static MainFragment newInstance(List<Recette> r) {
MainFragment fragment = new MainFragment();
Bundle bundle = new Bundle();
bundle.putParcelableArrayList(RECETTES_KEY, (ArrayList<? extends Parcelable>) r);
fragment.setArguments(bundle);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mRecettes = getArguments().getParcelableArrayList(RECETTES_KEY);
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_main, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
configureListView();
}
// Configure ListView
private void configureListView(){
this.mListView = getView().findViewById(R.id.activity_main_list_view);
RecetteAdapter adapter = new RecetteAdapter(getContext(), mRecettes);
mListView.setAdapter(adapter);
}
}
我主要活动的相关部分: 这是在我的 OnCreate 方法中:
mDatabaseHandler = new DatabaseHandler(this);
mRecettes = mDatabaseHandler.readRecettes();
mDatabaseHandler.close();
这是我用来显示片段的方法:
if (this.mMainFragment == null) this.mMainFragment = MainFragment.newInstance(mRecettes);
this.startTransactionFragment(this.mMainFragment);
让我知道是否应该添加更多我的代码,这是我第一次发帖 :) 露西尔
在您的 R.layout.fragment_main
中,您可以将 id 添加到根视图,例如 android:id@+id/fragment_root
以及每当您想要更改片段视图时:
在activity中:
MainFragment fragment = (MainFragment) getSupportFragmentManager().getFragmentById(R.id.fragment_root);
fragment.updateList(mRecettes);
然后在 MainFragment
updateList()
public void updateList(List<Recette> recettes) {
mRecettes.clear();
mRecettes.addAll(recettes);
configureListView();
}
您还可以在将片段添加到交易时标记片段而不是使用其 id,然后使用 getSupportFragmentManager().getFragmentByTag()