Skip to content
This repository has been archived by the owner on Jun 23, 2022. It is now read-only.

Commit

Permalink
1.06 Hook the adapter up to the ListView
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyla committed Mar 4, 2015
1 parent 0281f77 commit 1d42493
Showing 1 changed file with 6 additions and 0 deletions.
Expand Up @@ -24,6 +24,7 @@
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -101,6 +102,11 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
weekForecast);

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

// Get a reference to the ListView, and attach this adapter to it.
ListView listView = (ListView) rootView.findViewById(R.id.listview_forecast);
listView.setAdapter(mForecastAdapter);

return rootView;
}
}
Expand Down

13 comments on commit 1d42493

@martipello
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still current when I create the project I get a separate main activity fragment with a single fragment inside as such I've changed
ListView listView = (ListView) rootView.findViewById(R.id.listview_forecast);
To
ListView listView = (ListView) getActivity().findViewById(R.id.listview_forecast);
Which causes a force close

@ratanachai
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran a test for you. That's because getActivity().findViewById returned null.

@Pvag
Copy link

@Pvag Pvag commented on 1d42493 Jun 29, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@martipello did you create rootView?
See line 104:
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
(and then return it in line 110).
The code is still valid if you create rootView like that and then return it at the end of onCreateView method.

@martipello
Copy link

@martipello martipello commented on 1d42493 Jun 30, 2015 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@austinov
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try to check content of fragment_main.xml for existing the following line (in ListView control):
android:id="@+id/listview_forecast"

@StaziaPhoenix
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If anyone else encounters this problem, I had a similar situation.

The layouts_v11 folder contains another xml file named fragment_main.xml. This is the one we played with in our first UI exercise (fiddling with design mode). Though I am unfamiliar with all this, I imagine that this folder and the files within are used in adapting to different screen sizes. Since my test device is a Nexus 7 tablet, the v11 fragment_main was being called in MainActivity.java, and did not contain the ListView element/id. Thus the null pointer.

Either renaming this file or changing it to do what we want it to should resolve the issue. Since I don't know how to adapt a UI to another screen size, I renamed the file. Code now progresses as expected.

@sujin5310
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In android 1.4, I get two java files mainactivity and mainactivity fragment. I couldnt find any placeholder fragment. Also I am getting an error in rootview (Rootview is never assigned). What should I do now

@KritiSharan
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For your mainactivity fragment, edit the code like this and it will work:

`public class MainActivityFragment extends Fragment {

public MainActivityFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    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));
    ArrayAdapter<String> mForecatAddapter = new ArrayAdapter<String>(getActivity(),
            R.layout.list_item_forecast, R.id.list_item_forecast_textview, weekForecast);

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

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

    return rootView;
}

}`

@Teemmy
Copy link

@Teemmy Teemmy commented on 1d42493 Apr 18, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can someone please help me out... "R.id.container" the container id belongs to where?
@OverRide
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(savedInstanceState == null){
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
/Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
/
}

@KritiSharan
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

R.id.container refers to the ID of a View in your MainActivity's layout file, which contains the fragment.

@shubham0677
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please help. Getting this exception:

" java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference"

`public static class PlaceholderFragment extends Fragment {

    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-80/83",
                "Tommorow-Rain-30/40",
                "Wed-Rain-30/40",
                "Thur-Rain-30/40",
                "Fri-Rain-30/40",
                "Sat-Rain-30/40",
                "Sun-Rain-30/40"
        };
        List<String> weekForecast=new ArrayList<String>(Arrays.asList(forecastArray));

        ArrayAdapter<String> mForecastAdapter=new ArrayAdapter<String>(getActivity(),R.layout.fragment_main, R.id.list_item_forecast_textview , weekForecast);

        ListView forecast_list=(ListView) rootView.findViewById(R.id.listview_forecast);
        forecast_list.setAdapter(mForecastAdapter);

        return rootView;
    }
}`

@nunohpinheiro
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello everyone! Can you please help me?

At this point, I am getting a null pointer exception and I cannot understand why... It's accused in the following line of MainActivity:
"listView.setAdapter(mForecastAdapter);"

Thanks in advance!

@mundre
Copy link

@mundre mundre commented on 1d42493 Sep 14, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yvasilyev92 , How have you defined your activity_main.xml ? Just these lines will throw up error:

<?xml version="1.0" encoding="utf-8"?>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.sunshine.app.MainActivity"
android:id="@+id/container">

Please sign in to comment.