How to Using inflater to get a list view from fragment in Android Studio

583 views Asked by At

I am trying to get a list view id from another xml file (fragment_friends.xml) while i am in activity_main.xml layout. I tried 2 ways, but they are not working properly.

Below code will cause the app to crash straight away because i am trying to get an id that does not exist in this layout (activity_main)

Original code

    ListView friendList = (ListView) findViewById(R.id.friend_listView);
    friendList.setAdapter(friendAdaptor);

Below code works, but i wont be able to insert or display any data into my database.

Try 1 code:

    View inflatedView = getLayoutInflater().inflate(R.layout.fragment_friend, null);
    ListView friendList = (ListView) inflatedView.findViewById(R.id.friend_listView);
    friendList.setAdapter(friendAdaptor);

Below code the database and listView works, but my navbar drawer is gone and i when i try to get back to activity_main, app crashes

Try 2 code

    setContentView(R.layout.fragment_friend);
    ListView friendList = (ListView) inflatedView.findViewById(R.id.friend_listView);
    friendList.setAdapter(friendAdaptor);

app_bar_main.xml

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <!--<include layout="@layout/content_main" />-->
    <FrameLayout
        android:id="@+id/flContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>
1

There are 1 answers

1
Ben P. On BEST ANSWER

I believe the root of your problem comes down to a lack of familiarity with the general Android Fragment APIs. That's fine; everyone is new when they start. But it means that your question is quite difficult to answer concisely (beyond "read about Fragments").

You should, of course, start by reading about Fragments: https://developer.android.com/guide/components/fragments.html

Still, I think we can cover things here in brief.

All Fragments must be hosted in an Activity. The normal way to do this is either by adding a <fragment> tag to your activity's xml layout, or by including a <FrameLayout> in your activity's xml layout and then using a FragmentTransaction to dynamically add/replace/remove fragments from that frame.

In your posted code, inside app_bar_main.xml, I see this:

<FrameLayout
    android:id="@+id/flContent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="?attr/actionBarSize"/>

This is clearly the place where your fragments will eventually live, but you'll have to add them to the frame yourself in your Java code. You actually have a method that does this already:

private void displaySelectedScreen(int id){
    Fragment fragment = ...

    if(fragment != null){
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.flContent, fragment);
        ft.commit();
    }

    ...
}

It looks like this is meant to be used when your user clicks on an item in your navigation drawer, but the concept is the same. If you want your app to always start up with the "friends" fragment displayed, you can add this code to your activity's onCreate() method:

if (savedInstanceState == null) {
    getSupportFragmentManager()
        .beginTransaction()
        .replace(R.id.flContent, new friend())
        .commit();
}

Now you will have a friend fragment to work with!

However, fragment transactions aren't guaranteed to be executed right away, so even though (as a user) you'll always see this friend fragment when the app starts up, you won't necessarily be able to interact with it (as a programmer) immediately after writing this code. So you'll also have to move your call to populateFriendList() from your onCreate() method.

Instead, you should populate the friends list from within the friend fragment itself! Probably the right place to do this is inside the onActivityCreated() method of your fragment.

To find a view from inside a Fragment subclass, you can't just write findViewById() the way you can in an Activity. However, you can write this instead: getView().findViewById()... as long as you're doing this after your onCreateView() method has returned.

Anyway, this answer is getting extremely long even as I try to keep it as short as possible. I hope this is enough information for you to get your app up and running. Good luck.