Gautam Hans
Gautam Hans

Reputation: 135

ListView in Fragment doesn't show anything

I know that this question has been asked a lot of times already but I have went to all of the answers and none of them worked for me.

So, issue with the App is that I am trying to populate an ArrayList into a ListView using an ArrayAdapter and the app MainActivity doesn't show anything but white screen when launched.

Here's MainActivity.java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}


public static class PlaceholderFragment extends Fragment{

    private ArrayAdapter<String> mForecastAdapter;

    public PlaceholderFragment(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        //Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);

        String[] forecastArray = {
                "Today - Sunny - 88/63",
                "Tomorrow - Foggy - 70/46",
                "Weds - Cloudy - 72/63",
                "Thurs - Rainy - 64/51",
                "Fri - Foggy - 70/46",
                "Sat - Sunny - 76/68"
        };

        List<String> weekForecast = new ArrayList<String>(Arrays.asList(forecastArray));
        ListView listView = (ListView) rootView.findViewById(R.id.listview_forecast);
        listView.setAdapter(mForecastAdapter);

        mForecastAdapter = new ArrayAdapter<String>(
                //The current context (current activity)
                getActivity(),
                //ID of list item layout
                R.layout.list_item_forecast,
                //id of the textView to populate
                R.id.list_item_forecast_textview,
                //data to populate
                weekForecast
        );

        return rootView;
    }
} 

fragment_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.gautamhans.sunshine.app.MainActivityFragment"
    tools:showIn="@layout/activity_main">

    <ListView
        android:id="@+id/listview_forecast"
        android:layout_width="match_parent"
        android:layout_height="667dp"
        />

</FrameLayout>

Here's list_item_forecast.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:gravity="center_vertical"
    android:id="@+id/list_item_forecast_textview"
    >

</TextView>

Upvotes: 1

Views: 351

Answers (3)

Luca Nicoletti
Luca Nicoletti

Reputation: 2407

Of course the activity is not showing anything, you're not placing the fragment inside your activity! Use this in your activity's onCreate():

getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new PlaceholderFragment(), "fragment_tag").commit();

Where R.id.container is a layout inside your main_activity_layout file (empty layout)

UPDATE:

have these 2 file:

activity_main:

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

</RelativeLayout>

fragment_main:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.gautamhans.sunshine.app.MainActivityFragment"
    tools:showIn="@layout/activity_main">

    <ListView
        android:id="@+id/listview_forecast"
        android:layout_width="match_parent"
        android:layout_height="667dp"
        />

</FrameLayout>

keep the 3° layout file, it's ok.

Now, in your activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getSupportFragmentManager().beginTransaction()
    .replace(R.id.container, new PlaceholderFragment(), "fragment_tag").commit();
}

also:

        mForecastAdapter = new ArrayAdapter<String>(
                //The current context (current activity)
                getActivity(),
                //ID of list item layout
                R.layout.list_item_forecast,
                //id of the textView to populate
                R.id.list_item_forecast_textview,
                //data to populate
                weekForecast
        );
//move this after the initialization of adapter
listView.setAdapter(mForecastAdapter);

Upvotes: 1

Karyuu Ouji
Karyuu Ouji

Reputation: 314

Is your PlaceHolderFragment within your MainActivity.java file?

If so, try separating it on its own file PlaceHolderFragment.java..

As far as I know, java only allows one public class per source file.. That maybe the reason of your problem..

Upvotes: 0

A.simple
A.simple

Reputation: 20

When you set the ListView adapter,the adapter's reference is null and you set the null adapter for ListView.You should create the adapter before set ListView adapter.

ListView listView = (ListView) rootView.findViewById(R.id.listview_forecast);

    mForecastAdapter = new ArrayAdapter<String>(
            //The current context (current activity)
            getActivity(),
            //ID of list item layout
            R.layout.list_item_forecast,
            //id of the textView to populate
            R.id.list_item_forecast_textview,
            //data to populate
            weekForecast
    );
    listView.setAdapter(mForecastAdapter);

Upvotes: 0

Related Questions