我花了很多时间,但我不能让它工作。我在主要活动中查看寻呼机。在此活动中,当用户单击按钮时,启动新活动,其中用户可以向DB添加新项目。完成后,此活动关闭,用户返回主要活动。问题是我的视图寻呼机不希望在onResume被调用时刷新。我调试它,一切正常,但列表显示旧数据。但是当我滑到第三个标签然后返回到第一个数据时,数据就可以了。所以我在这里阅读了许多内容,尝试不可能,不可能,但仍然无法正常工作。第二个问题是如果我滑到第二个选项卡然后返回到第一个然后这个列表视图是空的但我不知道为什么。
主要活动:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = (ViewPager) findViewById(R.id.view_pager);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
selectedDayTabPosition = position;
((OnRefreshListener) mSectionsPagerAdapter.getItem(position)).onRefresh(MainActivity.this, position);
}
});
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(
actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(MainActivity.this));
}
}
@Override
protected void onResume() {
super.onResume();
refresh(selectedDayTabPosition);
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
public void refresh(final int position) {
subjectObjLinkedList.clear();
subjectObjLinkedList = db.getAllSubjects(position, subjectObjLinkedList);
runOnUiThread(new Runnable() {
@Override
public void run() {
if (lvAllSubjectsAtDay != null) {
subjectsTableAdapter.notifyDataSetChanged();
Log.d("refresh()", "notifyDataSetChanged");
}
}
});
}
SectionsPagerAdapter
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return new PlaceholderFragment();
}
PlaceholderFragment
public class PlaceholderFragment extends Fragment implements OnRefreshListener {
private TextView tvMainPageNoSubjectLabel;
private TextView tvMainPageNoSubjectDescLabel;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
lvAllSubjectsAtDay = (ListView) rootView.findViewById(R.id.lvAllSubjectsAtDay);
subjectsTableAdapter = new SubjectsTableAdapter(getActivity(), subjectObjLinkedList);
lvAllSubjectsAtDay.setAdapter(subjectsTableAdapter);
return rootView;
}
@Override
public void onRefresh(Activity activity, int position) {
refresh(position);
}
}
答案 0 :(得分:0)
好的,我发现解决方案并不完美,但正在发挥作用。我创建了7个独立的片段,并在onResume的片段类中调用notifyDataChanged。当然,我从活动中删除所有刷新方法。我不知道为什么以前的解决方案不刷新数据,但这是有效的。
@Override
public Fragment getItem(int position) {
switch (position){
case 0: return new Fragments.FragmentTab1(db);
case 1: return new Fragments.FragmentTab2(db);
case 2: return new Fragments.FragmentTab3(db);
case 3: return new Fragments.FragmentTab4(db);
case 4: return new Fragments.FragmentTab5(db);
case 5: return new Fragments.FragmentTab6(db);
case 6: return new Fragments.FragmentTab7(db);
default: return new Fragments.FragmentTab1(db);
}
}
public static class FragmentTab1 extends Fragment {
private TextView tvMainPageNoSubjectLabel;
private TextView tvMainPageNoSubjectDescLabel;
private SubjectsTableAdapter subjectsTableAdapter = null;
private LinkedList<SubjectObj> objLinkedList = new LinkedList<>();
private MySQLiteHelper db = null;
public FragmentTab1(MySQLiteHelper db){
this.db = db;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
ListView lvAllSubjectsAtDay = (ListView) rootView.findViewById(R.id.lvAllSubjectsAtDay);
tvMainPageNoSubjectLabel = (TextView) rootView.findViewById(R.id.tvMainPageNoSubjectLabel);
tvMainPageNoSubjectDescLabel = (TextView) rootView.findViewById(R.id.tvMainPageNoSubjectDescLabel);
lvAllSubjectsAtDay.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Intent myIntent = new Intent(getActivity(), ConfigSubjectActivity.class);
myIntent.putExtra("idSelectedSubject", parent.getItemIdAtPosition(position));
getActivity().startActivity(myIntent);
return true;
}
});
subjectsTableAdapter = new SubjectsTableAdapter(getActivity(), objLinkedList);
lvAllSubjectsAtDay.setAdapter(subjectsTableAdapter);
return rootView;
}
@Override
public void onResume() {
super.onResume();
objLinkedList.clear();
objLinkedList = db.getAllSubjects(0, objLinkedList);
subjectsTableAdapter.notifyDataSetChanged();
hideDefaultLabels();
}
private void hideDefaultLabels() {
if (objLinkedList.size() > 0) {
tvMainPageNoSubjectLabel.setVisibility(View.GONE);
tvMainPageNoSubjectDescLabel.setVisibility(View.GONE);
} else {
tvMainPageNoSubjectLabel.setVisibility(View.VISIBLE);
tvMainPageNoSubjectDescLabel.setVisibility(View.VISIBLE);
}
}
}