Lev
Lev

Reputation: 1009

List view does not populate

I am trying to following along the Udacity's tutorial got stuck on listview example to display the dummy list.

MainActivity.java :

package com.example.android.sunshine.app;

import android.app.Fragment;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity {

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


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public static class PlaceholderFragment extends Fragment {
        public PlaceholderFragment() {
        }


        @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {

                // Create some dummy data for the ListView.  Here's a sample weekly forecast
                String[] data = {
                        "Mon 6/23 - Sunny - 31/17",
                        "Tue 6/24 - Foggy - 21/8",
                        "Wed 6/25 - Cloudy - 22/17",
                        "Thurs 6/26 - Rainy - 18/11",
                        "Fri 6/27 - Foggy - 21/10",
                        "Sat 6/28 - TRAPPED IN WEATHERSTATION - 23/18",
                        "Sun 6/29 - Sunny - 20/7"
                };
                List<String> weekForecast = new ArrayList<String>(Arrays.asList(data));

                // Now that we have some dummy forecast data, create an ArrayAdapter.
                // The ArrayAdapter will take data from a source (like our dummy forecast) and
                // use it to populate the ListView it's attached to.
                ArrayAdapter<String> forecastAdapter =
                        new ArrayAdapter<String>(
                                getActivity(), // The current context (this activity)
                                R.layout.list_item_forecast, // The name of the layout ID.
                                R.id.list_item_forecast_textview, // The ID of the textview to populate.
                                weekForecast);

                View rootView = inflater.inflate(R.layout.fragment_main, container, false);
                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=".MainActivityFragment">

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


</FrameLayout>

list_item_forecast.xml

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list_item_forecast_textview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeight" />

I have been stuck on this a couple of days trying to retrace the tutorial. Please let me know where I went wrong.

Thank you.

Upvotes: 0

Views: 153

Answers (4)

Nadeem Iqbal
Nadeem Iqbal

Reputation: 2374

There are TWO problems in your code:

  1. You have not initialized the Listview using findViewById(...)
  2. You have not called the setAdapter on your listview object

solution: Add these two lines in your code before return statement

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

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

EDIT(thanks to Lev)

public class MainActivity extends AppCompatActivity {

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

    getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}

... other code

Upvotes: 2

Lev
Lev

Reputation: 1009

I did need the:

ListView forecastlist=(ListView)findViewById(R.id.listview_forecast);
forecastlist.setAdapter(forecastAdapter);

but I also needed to add the following code to the the :

protected void onCreate(Bundle savedInstanceState) method.

 if (savedInstanceState == null)
        {
            getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
        }

Upvotes: 0

Rajan Kali
Rajan Kali

Reputation: 12953

Add this two lines of code before return statement

ListView forecastlist=(ListView)findViewById(R.id.listview_forecast);
forecastlist.setAdapter(forecastAdapter);

Upvotes: 1

Ilya Tretyakov
Ilya Tretyakov

Reputation: 7010

I don't see ListView.setAdapter(ListAdapter adapter) call in your code.

ArrayAdapter<String> forecastAdapter =
                    new ArrayAdapter<String>(
                            getActivity(), // The current context (this activity)
                            R.layout.list_item_forecast, // The name of the layout ID.
                            R.id.list_item_forecast_textview, // The ID of the textview to populate.
                            weekForecast);

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

((ListView) rootView.findViewById(R.id.listview_forecast)).setAdapter(forecastAdapter);

Upvotes: 0

Related Questions