J'essaie d'obtenir un identifiant de vue de liste à partir d'un autre fichier xml (fragment_friends.xml) pendant que je suis dans la présentation activity_main.xml. J'ai essayé 2 manières, mais elles ne fonctionnent pas correctement.Comment utiliser inflater pour obtenir une vue de liste à partir d'un fragment dans Android Studio
Ci-dessous le code provoquera l'application plantage tout de suite parce que je suis en train d'obtenir une carte d'identité qui n'existe pas dans cette mise en page (activity_main)
code original
ListView friendList = (ListView) findViewById(R.id.friend_listView);
friendList.setAdapter(friendAdaptor);
Ci-dessous le code approprié, mais Je ne serai pas en mesure d'insérer ou d'afficher des données dans ma base de données.
Essayez 1 code:
View inflatedView = getLayoutInflater().inflate(R.layout.fragment_friend, null);
ListView friendList = (ListView) inflatedView.findViewById(R.id.friend_listView);
friendList.setAdapter(friendAdaptor);
Ci-dessous le code la base de données et de travaux LISTVIEW, mais mon tiroir navbar est parti et moi quand je tente de revenir à activity_main, app plante
Essayez 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>
Je vais supprimer ma réponse originale et en fournir une nouvelle qui, je l'espère, aborde tout ce qui se passe ici. –
D'accord, j'ai aussi mis à jour le code pour app_bar main – mmm