在 notifyDataSetChanged 后视图寻呼机不刷新

Posted

技术标签:

【中文标题】在 notifyDataSetChanged 后视图寻呼机不刷新【英文标题】:View pager is not refreshing after notifyDataSetChanged 【发布时间】:2015-04-22 03:29:34 【问题描述】:

我花了很多时间,但我无法让它工作。我在主要活动中查看寻呼机。在此活动中,当用户单击按钮启动新活动时,用户可以将新项目添加到数据库。完成后,此活动关闭,用户返回主要活动。问题是我的视图寻呼机在调用 onResume 时不想刷新。我调试它,一切正常,但列表显示旧数据。但是当我滑动到第三个选项卡并返回到第一个现在数据是好的。所以我在这里读了很多,尝试可能,不可能但仍然无法正常工作。第二个问题是,如果我滑动到第二个选项卡然后返回到第一个选项卡,那么这个列表视图是空的,但我不知道为什么。

pager offscreen limit is default >> 1 page to each side is in RAM.有谁知道什么是坏的?感谢您的时间和帮助。

主要活动:

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

占位符片段

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

【问题讨论】:

在fragment的onResume中执行listview的更新 尝试我得到相同的结果列表视图显示旧数据。我调试它的项目列表是好的,通知被调用但列表视图仍然没有更新。 【参考方案1】:

好的,我发现解决方案并不完美,但可以正常工作。我创建了 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);
        
    

【讨论】:

以上是关于在 notifyDataSetChanged 后视图寻呼机不刷新的主要内容,如果未能解决你的问题,请参考以下文章

360度全景视频后视镜

在 customAdapter.notifyDataSetChanged 之后 ListView 不更新

SyntaxError: (irb):4: 后视模式无效(正向后视/前瞻)

[完成]notifyDataSetChanged() 不会自动更新 ListActivity

在 notifyDataSetChanged 后 Android ListView 不刷新

ArrayAdapter.NotifyDataSetChanged() 不工作?