0

Well, i'm trying to find a ListView inside a fragment, to be Able to add and remove items through the Main Activity that created it. here is my attempt (i based it on com.viewpagerindicator.sample), but when i try to access the mainListView in the end, it crashes.

public final class ContactListFragment extends Fragment {
    private ListView MyListView;

    public ListView GetListView()
    {
        return MyListView;
    }


    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        LinearLayout theLayout = (LinearLayout) inflater.inflate(R.layout.tab_frag, container, false);
        MyListView = (ListView)theLayout.findViewById(R.id.listViewx);
        MyListView.setAdapter(adapter);     
        return theLayout;
    }
}

and here it it's layout

    <ListView
        android:id="@+id/listViewx"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >
    </ListView>

</LinearLayout>

and that's the code of Main Activity that i want to access listview from to be able to modify public class ContactListActivity extends BaseSampleActivity {

@Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.simple_tabs);


    List<Fragment> fragments = new Vector<Fragment>();

    fragments.add(Fragment.instantiate(this, ContactListFragment.class.getName()));
    fragments.add(Fragment.instantiate(this, ContactListFragment.class.getName()));
    fragments.add(Fragment.instantiate(this, ContactListFragment.class.getName()));


    PagerAdapter mAdapter = new PagerAdapter(getSupportFragmentManager(), fragments);


    ViewPager mPager = (ViewPager) findViewById(R.id.pager);
    mPager.setAdapter(mAdapter);

    TabPageIndicator mIndicator = (TabPageIndicator) findViewById(R.id.indicator);
    mIndicator.setViewPager(mPager);

    ContactListFragment XCLF = (ContactListFragment) fragments.get(1);
    ListView mainListView = (ListView) XCLF.GetListView();



}

any help would be appreciated, thanks

2 Answers2

1

You can't get the Fragment's views in Activity's onCreate() as the Fragment's layout is not inflated in this stage. Use Fragment's onActivityCreated() to update it's ListView.

See Access Fragment View from Activity's onCreate

Community
  • 1
  • 1
biegleux
  • 13,179
  • 11
  • 45
  • 52
  • i did as that post suggested, but now the problem is when i move away from the fragment in the ViewPagerIndicator widget by 2 pages, and then return to first one, the ListView is empty i tried calling "Adapter.notifyDataSetChanged()", but as soon as i call it, the app crashes. – Zakri Vorschted Aug 26 '12 at 13:49
0

I ran into this problem too. To remedy it, I keep a reference to the Layout rather than the specific view. Then when I need to return the specific View (i.e. when you call getListView() you must check whether the reference to the Layout is null. If it is, inflate it, set it, then find the view you need and return it.

Then in the onCreateView() method check whether the layout is set. If it is simply return that, otherwise inflate it and set it like you are right now.

This way you're ensuring it's only inflated once, and always inflated when you need it

bennettaur
  • 1,243
  • 10
  • 8
  • it's still always null, i even tried to use the same method and create a TextView, declared on the fragment and created onCreateView, with (new TextView(getActivity())) and when returning it from GetTextView() (Same as getListView above) it returns null always. – Zakri Vorschted Aug 25 '12 at 19:08