Search code examples
androidandroid-listviewandroid-listfragment

Android: Displaying a list in a fragment


I have a existing fragment, in which i want to display typical address kind of information like Street, City, PIN and List of Phone Numbers.

To display List of Phone Numbers, i added listview in addressfragment.xml:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="16dp" >

        <TextView
            android:id="@+id/txtStreetAddr"
            style="?android:textAppearanceMedium"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:lineSpacingMultiplier="1.2"
            android:text="default" />

        <ListView
            android:id="@+id/phones"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="0dp"
            android:paddingLeft="0dp" >
        </ListView>
        </LinearLayout>
</ScrollView>

My fragment class:

        public class AddressFragment extends Fragment {
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {

ViewGroup rootView = (ViewGroup) inflater
                .inflate(R.layout.fragment_screen_slide_page, container, false);

                ((TextView) rootView.findViewById(R.id.txtStreetAddr)).setText(
                        "123, Corner Street");

                ListView phoneListView = ((ListView) rootView.findViewById(R.id.phones));

                ArrayList<String> phNumbers = new ArrayList<String>();
                phNumbers.add("4343534343");
                phNumbers.add("6767566766");

                ArrayAdapter<String> arrayAdapter =      
                new ArrayAdapter<String>(MyApp.getAppContext(),android.R.layout.simple_list_item_1, phNumbers);
                // Set The Adapter
                phoneListView.setAdapter(arrayAdapter); 
    return rootView;
        }

The phone list view just does not show up on the screen - Why? Is there a better way to handle this?


Solution

  • I think you forget to inflate View in onCreateView(...)

    View rootView= inflater.inflate(R.layout.your_layout, container, false);
    

    add your onCreateView(...) should be

     public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
    
             View rootView= inflater.inflate(R.layout.your_layout, container, false);
    
            ((TextView) rootView.findViewById(R.id.txtStreetAddr)).setText(
                    "123, Corner Street");
    
            ListView phoneListView =((ListView) rootView.findViewById(R.id.phones);
    
            ArrayList<String> phNumbers = new ArrayList<String>();
            phNumbers.add("4343534343");
            phNumbers.add("6767566766");
    
            ArrayAdapter<String> arrayAdapter =      
            new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, phNumbers);
            // Set The Adapter
            phoneListView.setAdapter(arrayAdapter);
    
         return rootView;
      }