0

I have onclicklistener that works. I am trying to launch a new fragment from the button click on the list view. Right now, the fragment does not launch. However, the emulator we are using does not crash, so if we're understanding this correctly, it isn't connecting to the new fragment/XML page.

  public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l)
{
    myBadData.setId(i);
    Fragment fr = new event_description();
    //fr.setArguments(bundle);
    FragmentManager fm = getFragmentManager();
    FragmentTransaction fragmentTransaction = fm.beginTransaction();
    //int contId = v.getId();
    fragmentTransaction.add(R.id.page, fr);
   // fragmentTransaction.add(view.getId(), fr);
   // fragmentTransaction.commit();

    Intent intent =new Intent(eventList.this, fr.getClass());

}

Here is our XML code for the view that we are trying to add the fragment over top of.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/page"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="com.example.alex.qtapandroid.ui.fragments.DayFragment">

<!-- TODO: Update blank fragment layout -->



    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" />

    <ListView
        android:id="@+id/dayList"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

 </FrameLayout>
Benjamin Li
  • 1
  • 1
  • 2
  • Please post your LogCat as there may very well be an error message or two there. – Ken Y-N Mar 30 '17 at 03:32
  • use this line fragmentTransaction.commit(); if not works then post logcat – AAA Mar 30 '17 at 03:35
  • Possible duplicate of [How to call Fragment from OnClickListener](http://stackoverflow.com/questions/21285068/how-to-call-fragment-from-onclicklistener) – Sniffer Mar 30 '17 at 05:48

2 Answers2

0

change this line

Intent intent =new Intent(eventList.this, fr.getClass());

to:

fragmentTransaction.commit;

and better replace fragment in container ,

fragmentTransaction.replace(R.id.page, fr);
Suresh Basnet
  • 147
  • 2
  • 13
0

You should be using commit() to start your transaction, not an Intent(), you also want to use replace() not add():

 public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l)
{
    //Assuming that this creates a new fragment
    Fragment fr = new event_description();
    FragmentManager fm = getFragmentManager();
    FragmentTransaction fragmentTransaction = fm.beginTransaction();
    fragmentTransaction.replace(R.id.page, fr, "TAG ID");
    fragmentTransaction.commit();
}
BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156