【问题标题】:Refresh current fragment from MainActivity从 MainActivity 刷新当前片段
【发布时间】:2015-12-17 18:54:06
【问题描述】:

我希望当我在ListView 中添加一个新项目时,该片段会被刷新。

我已经阅读了很多帖子,但什么都没有。我已经尝试添加:

fragTransaction.detach(currentFragment);
fragTransaction.attach(currentFragment);
fragTransaction.commit();

或其他事情,但我做不到。

这是片段:

public class GiocatoFragment extends Fragment {

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View v  = inflater.inflate(R.layout.fragment_giocato, null);
    ListView giocatoList = (ListView)v.findViewById(R.id.listView_giocato);

    Cursor cursor = MainActivity.userDb.getGames(ClasseDiAppoggio.getConsole(), "Giocato");
    String[] fromFieldNames = new String[] {UserDataBase.COL_VIDEOGAME_NAME, UserDataBase.COL_VIDEOGAME_DEVELOPER, UserDataBase.COL_VIDEOGAME_VALUE};
    int[] toViewsIDs = new int[] {R.id.textView_game_name, R.id.textView_developper, R.id.textView_value};
    SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(this.getContext(), R.layout.game_row, cursor, fromFieldNames, toViewsIDs, 0);
    giocatoList.setAdapter(myCursorAdapter);

    return v;
}

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);


    if (isVisibleToUser) {
        ClasseDiAppoggio.setItem("Giocato");
    }
}

}

这是在数据库中添加项目的方法。

    private void addGame(String game, String console) {


    myDb.getGame(game);

    userDb.addGame(console, game, ClasseDiAppoggio.getItem());

    TextView txt = (TextView) findViewById(R.id.textView);
    txt.setText(DataBase.gameInfo[0] + ", " + DataBase.gameInfo[1]);




}

我该怎么办?

【问题讨论】:

  • 如果您谈论的是具有Adapter 的实际ListView,那么您只需在添加或删除项目后调用adapter.notifyDatasetChanged()
  • 作为适配器,我在片段中使用 simpleCursorAdapter..
  • 这就是myDb 吗?如果是这样,您需要做的就是致电myDb.requery() 我相信。 requery 是您用于SimpleCursorAdapter 而不是notifyDatasetChanged()
  • 我应该把它放在哪里?
  • 如果 userDb 是适配器,请在 userDb.addGame() 调用之后立即调用 userDb.requery()。如果没有,您需要先从 ListView 中获取适配器 var。

标签: android listview android-fragments fragment


【解决方案1】:

如果您在活动中获取数组列表或在活动中从数据库中获取,那么您可以从活动中将本地广播从活动发送到片段,并且可以在片段中接收该广播并可以从数据库中获取数据,并且从这里您可以操作列表并可以通知列表,否则您可以在片段中创建一个方法并从活动中该片段的实例调用调用,在该方法中您可以从数据库中获取列表并显示它。

【讨论】:

  • 对不起,我不明白你的意思
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-13
  • 1970-01-01
  • 2016-10-27
  • 1970-01-01
  • 1970-01-01
  • 2023-03-03
  • 1970-01-01
相关资源
最近更新 更多