Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Best way to implement a swipe to refresh in the new Stormy weather app?

I'm having issues adding in a swipe to refresh gesture on the weather app course as a little extra credit. I've got the code figured out for the XML file activity_main but I'm having issues setting up the adapter for the MainActivity.java, has anyone else been able to get this accomplished? Please help a newb out! Ben Jakuben

2 Answers

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

I have just the thing that might help from another course (later in the track): Adding a SwipeToRefresh Gesture.

Ben, thank you so much for the quick reply! Only question I have is with the changes in the new api 21, will all of this still work? Issue I'm having is 1. It gives me an error using rootView in "mSwipeRefreshLayout = (SwipeRefreshLayout)rootView.findViewbyID(R.id.swipeRefreshLayout);". Might be easier if I just paste what I have, if that's okay.

ListView mListView;
    Adapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mSwipeRefreshLayout = (SwipeRefreshLayout)rootView.findViewbyID(R.id.swipeRefreshLayout);
        mSwipeRefreshLayout.setOnRefreshListener(mOnRefreshListener);
        mSwipeRefreshLayout.setColorSchemeColors(
                R.color.swipeRefresh1,
                R.color.swipeRefresh2,
                R.color.swipeRefresh3);

        mListView = (ListView) findViewById(R.id.activity_main_listview);

        ButterKnife.inject(this);

        mProgressBar.setVisibility(View.INVISIBLE);

        final double latitude = 36.3114;
        final double longitude = -86.6533;

        mRefreshImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getForecast(latitude, longitude);
            }
        });

........ then at the bottom of the MainActivity.java I have this

    protected SwipeRefreshLayout.OnRefreshListener mOnRefreshListener = new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            Toast.makeText(getActivity());
        }
    };
}
Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Since you are calling directly from an Activity class, you don't have or need a rootView in this line of code.

mSwipeRefreshLayout = (SwipeRefreshLayout)rootView.findViewbyID(R.id.swipeRefreshLayout);

Try this instead:

mSwipeRefreshLayout = (SwipeRefreshLayout) findViewbyID(R.id.swipeRefreshLayout);