【发布时间】:2016-11-21 07:20:02
【问题描述】:
我有一个片段,其中包含一些项目的列表视图。单击列表视图中的项目时,将启动 Activity 以查看有关列表视图的详细信息。 Activity 上有一个返回按钮,可以将我们带回到片段页面。按下后退按钮并关闭活动页面后,我希望在片段列表视图中刷新数据。我已经尝试过 onResume() 方法,但它不会刷新数据。
有人可以帮助我如何实现这一点,这在这方面很新。
public class FavCommittee extends Fragment {
public ArrayList<Committee> favCommitteeData = new ArrayList<>();
@Nullable
@Override
public void onResume(){
super.onResume();
final SharedPreferences pref = getContext().getSharedPreferences("MyFav", 0);
final SharedPreferences.Editor editor = pref.edit();
View rootView =getView();
Map<String,?> entries = pref.getAll();
final Set<String> keys = entries.keySet();
int count=0;
for (String key:keys) {
String val = pref.getString(key, null);
String[] store = val.split(":");
if (store[0].equals("committee"))
count=count+1;
}
for (int i=0;i<keys.size();i++) {
String val = pref.getString(Integer.toString(i), null);
if (val != null) {
String[] store = val.split(":");
if (store[0].equals("committee")) {
count = count - 1;
new GetAllCommittees(getContext(), rootView).execute(store[1], Integer.toString(count));
}
}
} final ListView yourListView = (ListView) rootView.findViewById(R.id.house_listview);
yourListView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
Committee obj = (Committee)yourListView.getItemAtPosition(position);
Intent intent = new Intent(FavCommittee.this.getActivity(), committee_info.class);
intent.putExtra("Committee",obj.getCommittee_id());
startActivity(intent);
}
});
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.house, container, false);
return rootView;
}
【问题讨论】:
-
放片段代码
-
@RajeshKushvaha 添加了
-
您是否将导航上的 onBackpress 覆盖为主页点击活动?我在 onBackpress 和导航后点击了 onResume
-
目前我正在这样做:-public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: onBackPressed();返回真;默认值:返回 super.onOptionsItemSelected(item); } } @Override 公共无效 onBackPressed() { super.onBackPressed();结束(); } 我怎样才能改变它
-
您的 onBackPress 代码可以写在注释中,因此您应该获得片段的 onResume,如果仍然无法正常工作,请尝试 startActivityforResult(intent, code),刷新您的视图 onActivityResult();
标签: android android-fragments android-activity