Search code examples
androidandroid-listfragment

ListFragment does not display correctly onCreate(), but is ok after onResume()


MyActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //list is global var
    list = (MyListFragment) getFragmentManager().findFragmentById(com.myapp.R.id.mainActivity_myListFragment);
    //add all objects to list
    list.getListView().invalidateViews();
}

@Override
protected void onResume(){
    super.onResume();
    list.getListView().invalidateViews();
}

MyListFragment

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    MyAdapter adapter = new MyAdapter(new ArrayList());
    setListAdapter(adapter);
}

public View getView(int position, View convertView, ViewGroup container) {
    View view = super.getView(position, convertView, container);
    TextView main = (TextView) view.findViewById(mylibs.common.R.list_main);
    TextView sub = (TextView) view.findViewById(mylibs.common.R.list_sub);
    ImageView image = (ImageView) view.findViewById(mylibs.common.R.list_image);

    //set the View content here.
}

The problem is, I can not get the first run to display correctly. However, if onResume() is called by Android, it all displays perfectly fine. How do I solve this?

The first item in the list displays correctly at all times, only the rest of the list is problematic.

When debugging getView() right after onCreate(), it clearly shows that the TextView and ImageView are set by the data that I want in the getView() method, but it does not display and instead of showing the data that I want, it shows the toString() String of the Object that's in the Adapter for one of the TextView and all others are left blank.


Solution

  • Android is a wreck and this is a waste of time.

    I am still looking for the answer, but a workaround is to create a new Thread, sleep for 1ms, then runonUIThread() invalidateViews().