ListView is empty, despite items added to the adapter sometime when the application was resuming

I have an application on the market with an error that I cannot solve. Today I tracked it to the next method.

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

This method is passed to arraylist transaction objects that are pulled from the Internet. When the application is open, data is retrieved from the Internet, and the above method is called, which creates a new adapter that is set for ListFragment. This method is also called when the user requests more transactions.

, , , , , . , , , , ( , RAM). , dealListAdapter null, . , , , .

, , if 21 . , .

05-23 01:52:32.879: D/UPDATE_DEAL_LIST(3478): Notified list  created new21
+5
3

( :)

, , , onRestoreInstanceState(). , - .

ListActivity onRestoreInstanceState(). , . , .

contentView onResume(), onCreate(), .

, .

0

, , . , notifyDataSetChanged() , , .

    public static class Utility {
    public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }


        int totalHeight = 0;
        int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        int newHeight = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        if (newHeight >= 100)
            params.height = newHeight;  
        else 
            params.height = 100;
        listView.setLayoutParams(params);
        listView.requestLayout();
    }
}

, !

0

You can try one or more of the following options.

  • Try calling setListShown(false)just before setting up your list adapter.

  • Otherwise, do setListShown(false);
    setListAdapter(null);

  • Otherwise do requestLayout()

0
source

All Articles