尽管在恢复应用程序时某些项目已添加到适配器,但ListView为空

ben*_*lis 5 android android-listview android-adapter android-listfragment

我在市场上有一个应用程序,我似乎无法解决这个问题.今天我跟踪了以下方法.

public void updateDealList(ArrayList<Deal> deals) {
    // first call or showing bookmakrs (also gets called other times when adapter is null)
    if (dealListAdapter == null || showingBookmarks || !dealListAdapter.moreDealsToDownload()) { 
        dealListAdapter = new EndlessDealListAdapter(getActivity(),
                R.layout.deal_list_item, deals);
        setListAdapter(dealListAdapter);
        listView = getListView();
        if (getActivity().findViewById(R.id.right_fragment_container)!=null){ // if tablet
            listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            listView.setVerticalScrollbarPosition(View.SCROLLBAR_POSITION_LEFT);
        }
        showingBookmarks=false;
        Log.d("UPDATE_DEAL_LIST", "Notified list  created new" + dealListAdapter.getCount());
        return; //PROBLEM OCCURS WHEN APP COMES IN HERE AFTER RESUMING
    }
    dealListAdapter.getDealListAdapter().clear();
    for (Deal d: deals){
        dealListAdapter.getDealListAdapter().add(d);
        Log.d("UPDATE_DEAL_LIST", "added " + d.title);
    }
    dealListAdapter.notifyDataSetChanged();
    Log.d("UPDATE_DEAL_LIST", "Notified list " + dealListAdapter.getCount());
}
Run Code Online (Sandbox Code Playgroud)

该方法被传递给从互联网上下拉的交易对象的arraylist.首次打开应用程序时,将从Internet中提取数据并调用上述方法,从而创建为ListFragment设置的新适配器.当用户请求更多交易时,也会调用此方法.

我遇到的问题是列表有时会认为它是空的,它包含了包含交易的适配器.这似乎当使用恢复该应用程序时,他们的设备内存不足发生(I假定应用程序的一部分已经从RAM移除).调用此方法并且dealListAdapter为null,因此创建了一个新方法并添加了交易.尽管发生了这种情况,但列表仍然是空的,用户必须强制关闭应用程序以使其再次运行.

下面的行显示调用方法时它进入if和21个交易添加到适配器.不幸的是,该列表对用户来说是空的.

05-23 01:52:32.879: D/UPDATE_DEAL_LIST(3478): Notified list  created new21
Run Code Online (Sandbox Code Playgroud)

bal*_*azs 0

一个想法(相当遥远的想法:)

如果应用程序本身没有被杀死,只是 Activity 被杀死,系统会尝试重新创建 Activity 的最后状态,调用 onRestoreInstanceState()。如果应用程序被杀死,则不会调用此方法 - 所以这是两者之间的最大区别之一。

ListActivity 覆盖 onRestoreInstanceState()。它确保列表在继续之前存在。如果该列表不存在,则会从默认位置对其进行扩充。

如果您在 onResume() 中设置 contentView,请尝试将其移至 onCreate(),这可能会解决问题。

如果我看到活动的代码,我可以提供更多帮助。