0

I have a fragment with ListView and ArrayAdapter as global variables in custom fragment class. I also have a function that should update this list making additional text field visible or not. So if I call list update function after rotate (with changing to special land layout) from activity, I found ListView and ArrayAdapter objects are null. Therefore I can't update my list. What should I do? Thnx guys.

    private View fragmentView;
    private SimpleListNewsAdapter adapterNews;
    private ListView listNews;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        fragmentView = inflater.inflate(getLayoutId(), container, false);
        listNews = (ListView) fragmentView.findViewById(R.id.newsList); 
        adapterNews = new SimpleListNewsAdapter(fragmentView.getContext(), R.id.newsList);
        listNews.setAdapter(adapterNews);
        …
        return fragmentView;
    }

    public void showListExpanded(Boolean expanded){
        if (isShowDesc != expanded){
            isShowDesc = expanded;

            if (adapterNews != null && listNews != null){
                adapterNews.notifyDataSetChanged();             
                listNews.refreshDrawableState();
                listNews.invalidateViews();
                listNews.setAdapter(adapterNews);

            } else {
                // Here I should do something to refresh my list
            }
        }
    } 
Konstantin Konopko
  • 5,229
  • 4
  • 36
  • 62

2 Answers2

1

When you rotate your device (if you do not declare android:configChanges="orientation" at manifest) your activity is going to be re-created. I guess you are not properly initializing your listView and your adapter on your onCreate method (may be you are waiting a user interaction such as a button click). If your share your full code you can get an answer:)

Devrim
  • 15,345
  • 4
  • 66
  • 74
  • I init my list in onCreateView method. May be should I to do that in onCreate? – Konstantin Konopko Oct 20 '13 at 15:41
  • I don't think that will solve your problem. If you rotate your device firstly your activity's onCreate method then your fragment's onCreateView method is going to be called. – Devrim Oct 20 '13 at 15:51
  • I don't know what kind of logic you have. But if you try to init your list's data on activity's onCreate or fragment's onCreateView method that should work. Post your code, I'll try to help;) – Devrim Oct 20 '13 at 15:59
  • I have post essentian code guess. I call `showListExpanded` function from the activity when user select corresponding menu item. That's all. – Konstantin Konopko Oct 20 '13 at 16:38
  • Are you sure your adapterNews or listNews are null? As I see from code you have post and your comments they sould not be null. May be your if (isShowDesc != expanded) returns false? – Devrim Oct 20 '13 at 21:26
0

My fault was in creating a new empty fragment object from FragmentPagerAdapter.getItem in the activity and then calling showListExpanded. I solve this by bookkeeping links to original fragments in moment of its creation. I used this good example.

Community
  • 1
  • 1
Konstantin Konopko
  • 5,229
  • 4
  • 36
  • 62