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>
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 aFragmentTransaction
to dynamically add/replace/remove fragments from that frame.In your posted code, inside
app_bar_main.xml
, I see this: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:
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: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 topopulateFriendList()
from youronCreate()
method.Instead, you should populate the friends list from within the
friend
fragment itself! Probably the right place to do this is inside theonActivityCreated()
method of your fragment.To find a view from inside a
Fragment
subclass, you can't just writefindViewById()
the way you can in anActivity
. However, you can write this instead:getView().findViewById()
... as long as you're doing this after youronCreateView()
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.