Call one fragment from another

user1743673 picture user1743673 · Aug 22, 2013 · Viewed 20.5k times · Source

I'm very bad in explaining things, i'll try to do my best :)

I was trying to use a Tabhost and display list fragments in each tab.

[ListFragments in tabview]:enter image description here

This is how it looks.

On click on the list element, another fragment is invoked to display the details, it's displaying as below. [Onclick on the list elements]:enter image description here There are 2 problems.

  1. The DetailsFragment is not full screen. i.e. an able to see the list(u can see in the above image)
  2. When i go back to the tab view and click on the list it says : "java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. [in ListView(16908298, class android.widget.ListView) with Adapter(class android.widget.SimpleAdapter)] " TabView xml:

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >
    
            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0"
                android:orientation="horizontal" />
    
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="0" />
    
            <FrameLayout
                android:id="@+android:id/realtabcontent"
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="1" />
        </LinearLayout>
    </TabHost>
    

detailsFragment xml :

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container1"
    android:layout_width="match_parent"
    android:layout_height="fill_parent" >

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#ffffff"
            android:shrinkColumns="*"
            android:stretchColumns="*" >

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="50dip"
                android:gravity="center_horizontal" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:text="Msg Id"
                    android:textColor="#000000"
                    android:textSize="13sp" />

                <TextView
                    android:id="@+id/tv1"
                    android:layout_weight="1"
                    android:background="#a09f9f"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:textColor="#000000"
                    android:textSize="13sp" />
            </TableRow>

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:text="PID"
                    android:textColor="#000000"
                    android:textSize="13sp" />

                <TextView
                    android:id="@+id/tv2"
                    android:layout_weight="1"
                    android:background="#a09f9f"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:textColor="#000000"
                    android:textSize="13sp" />
            </TableRow>

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:text="Sender BS"
                    android:textColor="#000000"
                    android:textSize="13sp" />

                <TextView
                    android:id="@+id/tv3"
                    android:layout_weight="1"
                    android:background="#a09f9f"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:textColor="#000000"
                    android:textSize="13sp" />
            </TableRow>

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:text="Sender NS"
                    android:textColor="#000000"
                    android:textSize="13sp" />

                <TextView
                    android:id="@+id/tv4"
                    android:layout_weight="1"
                    android:background="#a09f9f"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:textColor="#000000"
                    android:textSize="13sp" />
            </TableRow>

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:text="Sender Interface"
                    android:textColor="#000000"
                    android:textSize="13sp" />

                <TextView
                    android:id="@+id/tv5"
                    android:layout_weight="1"
                    android:background="#a09f9f"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:textColor="#000000"
                    android:textSize="13sp" />
            </TableRow>

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:text="Receiver BS"
                    android:textColor="#000000"
                    android:textSize="13sp" />

                <TextView
                    android:id="@+id/tv6"
                    android:layout_weight="1"
                    android:background="#a09f9f"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:textColor="#000000"
                    android:textSize="13sp" />
            </TableRow>

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:text="Receiver NS"
                    android:textColor="#000000"
                    android:textSize="13sp" />

                <TextView
                    android:id="@+id/tv7"
                    android:layout_weight="1"
                    android:background="#a09f9f"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:textColor="#000000"
                    android:textSize="13sp" />
            </TableRow>

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:text="Receiver Interface"
                    android:textColor="#000000"
                    android:textSize="13sp" />

                <TextView
                    android:id="@+id/tv8"
                    android:layout_weight="1"
                    android:background="#a09f9f"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:textColor="#000000"
                    android:textSize="13sp" />
            </TableRow>

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:text="Msg Type"
                    android:textColor="#000000"
                    android:textSize="13sp" />

                <TextView
                    android:id="@+id/tv9"
                    android:layout_weight="1"
                    android:background="#a09f9f"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:textColor="#000000"
                    android:textSize="13sp" />
            </TableRow>

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:text="Msg State"
                    android:textColor="#000000"
                    android:textSize="13sp" />

                <TextView
                    android:id="@+id/tv10"
                    android:layout_weight="1"
                    android:background="#a09f9f"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:textColor="#000000"
                    android:textSize="13sp" />
            </TableRow>

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:text="Exe Time"
                    android:textColor="#000000"
                    android:textSize="13sp" />

                <TextView
                    android:id="@+id/tv11"
                    android:layout_weight="1"
                    android:background="#a09f9f"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:textColor="#000000"
                    android:textSize="13sp" />
            </TableRow>

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:text="Init Time"
                    android:textColor="#000000"
                    android:textSize="13sp" />

                <TextView
                    android:id="@+id/tv12"
                    android:layout_weight="1"
                    android:background="#a09f9f"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:textColor="#000000"
                    android:textSize="13sp" />
            </TableRow>

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:text="Send Time"
                    android:textColor="#000000"
                    android:textSize="13sp" />

                <TextView
                    android:id="@+id/tv13"
                    android:layout_weight="1"
                    android:background="#a09f9f"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:textColor="#000000"
                    android:textSize="13sp" />
            </TableRow>

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:text="Admin User"
                    android:textColor="#000000"
                    android:textSize="13sp" />

                <TextView
                    android:id="@+id/tv14"
                    android:layout_weight="1"
                    android:background="#a09f9f"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:textColor="#000000"
                    android:textSize="13sp" />
            </TableRow>

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:text="Payload"
                    android:textColor="#000000"
                    android:textSize="13sp" />

                <TextView
                    android:id="@+id/tv15"
                    android:layout_weight="1"
                    android:background="#a09f9f"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:textColor="#000000"
                    android:textSize="13sp" />
            </TableRow>
        </TableLayout>
    </ScrollView>

</FrameLayout>

Thanks in advance

Answer

Ritesh Gune picture Ritesh Gune · Aug 22, 2013
java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification.

Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. [in ListView(16908298, class android.widget.ListView) with Adapter(class android.widget.SimpleAdapter)] "

Please call notifyDataSetChanged() and yourListView.requestLayout() in the UI thread after adding items.The problem is that your collection that bound to Adapter is changed in the Background thread. Hence the solution is moving it to UI thread(as I stated above) or simply wrapping it with runOnUiThread().

runOnUiThread(new Runnable() {
  public void run() {
    your_collection.add(item);
    your_adapter.notifyDataSetChanged();
    yourListView.requestLayout();
  }
});

To call another fragment the current:

SecondFragment secFrag = new SecondFragment();
FragmentTransaction fragTransaction = getFragmentManager().beginTransaction();
                    fragTransaction.replace(R.id.frame_fragment,secFrag );
                    fragTransaction.addToBackStack(null);
                    fragTransaction.commit();

EDIT:

To Display Detail fragment full Screen, please make following changes:

  1. In your scrollView Replace android:layout_height="wrap_content" with android:layout_height="fill_parent"

  2. In your TableLayout Replace android:layout_height="wrap_content" with android:layout_height="fill_parent"