【发布时间】: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