如何在我的活动(SQLite)中的数据修改后更新我的片段?

huangapple go评论81阅读模式
英文:

How do I update my Fragment after data modification in my activity (SQLite)?

问题

以下是翻译好的部分:

我在一个片段中有一个 ListView,在应用程序启动时填充数据。
我在我的 newInstance 方法中将一个 ParcelableArrayList 放入 Bundle 中,在我的 OnCreateView 中取回它,此前我在 Activity 中通过在 newInstance 方法中传递 ArrayList(这些数据从 SQLite 数据库中读取)。
这部分工作正常,因为我正确地在片段中显示了数据。

我实现了一个按钮,用于从表中删除所有数据,我现在希望在清空表格后更新我的视图。
删除所有按钮在我的主 Activity 中处理,我在其中调用数据库处理程序来清空表格。

最佳方法是什么?以下是我认为相关的代码部分:

我的片段类:

public class MainFragment extends Fragment {

    public static final String RECETTES_KEY = "recettes_key";
    private List<Recette> mRecettes;
    private ListView mListView;

    public MainFragment() {
        // 必要的空公共构造函数
    }

    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);
        // 为此片段充气布局
        return inflater.inflate(R.layout.fragment_main, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        configureListView();
    }

    // 配置 ListView
    private void configureListView(){
        this.mListView = getView().findViewById(R.id.activity_main_list_view);
        RecetteAdapter adapter = new RecetteAdapter(getContext(), mRecettes);
        mListView.setAdapter(adapter);
    }
}

来自我的主 Activity 的相关部分:
这在我的 OnCreate 方法中:

mDatabaseHandler = new DatabaseHandler(this);
mRecettes = mDatabaseHandler.readRecettes();
mDatabaseHandler.close();

这在我用于显示片段的方法中:

if (this.mMainFragment == null) this.mMainFragment = MainFragment.newInstance(mRecettes);
this.startTransactionFragment(this.mMainFragment);

如果需要更多代码,请告诉我,这是我第一次发帖 如何在我的活动(SQLite)中的数据修改后更新我的片段? Lucile

英文:

I have a ListView in a Fragment that is populated when the app starts.
I put a ParcelableArrayList in a Bundle in my newInstance method, and I get it back in my OnCreateView after passing the ArrayList in the newInstance method in my Activity (which is the data read from the SQLite database).

This part works, as I display my data in my Fragment correctly.

I implemented a button that removes all data from the table, and I would now like to update my view after I cleaned the table.
The remove all button is handled in my main activity where I call my database handler to empty the table.

What is the best way to do that ? Here are the parts of the code that seem relevant to me :

My Fragment class :

public class MainFragment extends Fragment {

    public static final String RECETTES_KEY = &quot;recettes_key&quot;;
    private List&lt;Recette&gt; mRecettes;
    private ListView mListView;

    public MainFragment() {
        // Required empty public constructor
    }

    public static MainFragment newInstance(List&lt;Recette&gt; r) {
        MainFragment fragment = new MainFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList(RECETTES_KEY, (ArrayList&lt;? extends Parcelable&gt;) 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);
    }
}

Relevant parts from my Main acivity :
This is in my OnCreate method :

mDatabaseHandler = new DatabaseHandler(this);
mRecettes = mDatabaseHandler.readRecettes();
mDatabaseHandler.close();

This is in the method I use to show a fragment :

if (this.mMainFragment == null) this.mMainFragment = MainFragment.newInstance(mRecettes);
this.startTransactionFragment(this.mMainFragment);

Let me know if I should add more of my code, this is my first time posting 如何在我的活动(SQLite)中的数据修改后更新我的片段?
Lucile

答案1

得分: 0

在你的 R.layout.fragment_main 中,可以为根视图添加一个 id,比如使用 android:id@+id/fragment_root

每当你想要更改片段视图时:

在活动中:

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()

英文:

In your R.layout.fragment_main, you can add an id to the root view, say with android:id@+id/fragment_root

And whenever you want to change the fragment view:

In activity:

MainFragment fragment = (MainFragment) getSupportFragmentManager().getFragmentById(R.id.fragment_root);
fragment.updateList(mRecettes);

And then create the new method updateList() in your MainFragment

public void updateList(List&lt;Recette&gt; recettes) {
    mRecettes.clear();
    mRecettes.addAll(recettes);
    configureListView();
}

Also you can tag your fragment when you add it to your transaction instead of using its id, and then use getSupportFragmentManager().getFragmentByTag()

huangapple
  • 本文由 发表于 2020年4月6日 17:53:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/61057111.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定