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

Refreshing ListView when using Loadercallbacks

Here's my main activity:

package net.biscani.dino;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.LoaderManager.LoaderCallbacks;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.Loader;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ListActivity {

    private final int MENU_NEWS = 0;
    private final int MENU_QUIT = 1;
    private final int MENU_ABOUT = 2;

    private static int LOADER_NUM = 1;
    private ProgressDialog mPd;
    private ListAdapter mAdapter;
    protected SwipeRefreshLayout mSwipeRefreshLayout;

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

        mSwipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swipeRefreshLayout);
        mSwipeRefreshLayout.setOnRefreshListener(mOnRefreshListener);
        mSwipeRefreshLayout.setColorSchemeColors(android.R.color.holo_blue_bright, android.R.color.holo_green_light, android.R.color.holo_orange_light, android.R.color.holo_red_light);

        if (isNetworkAvailable()) {
            mPd = new ProgressDialog(this);

            ListView lv = getListView();

            lv.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    String title = ((TextView) view.findViewById(R.id.title))
                            .getText().toString();
                    String content = ((TextView) view
                            .findViewById(R.id.content)).getText().toString();

                    Intent in = new Intent(getApplicationContext(),
                            ArticleActivity.class);
                    in.putExtra(GetArticles.KEY_TITLE, title);
                    in.putExtra(GetArticles.KEY_CONTENT, content);
                    startActivity(in);
                }
            });

            getLoaderManager().restartLoader(LOADER_NUM, Bundle.EMPTY,
                    mLoaderCallbacks);
        } else {
            Toast.makeText(this, "Network is unavailable!", Toast.LENGTH_LONG)
                    .show();
        }

    }

    public boolean isNetworkAvailable() {
        ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();

        boolean isAvailable = false;
        if (networkInfo != null && networkInfo.isConnected()) {
            isAvailable = true;
        }

        return isAvailable;
    }

    private LoaderCallbacks<ArrayList<HashMap<String, String>>> mLoaderCallbacks = new LoaderCallbacks<ArrayList<HashMap<String, String>>>() {

        @Override
        public Loader<ArrayList<HashMap<String, String>>> onCreateLoader(
                int id, Bundle args) {

            mPd.show();

            return new GetArticles(MainActivity.this);
        }

        @Override
        public void onLoadFinished(
                Loader<ArrayList<HashMap<String, String>>> loader,
                ArrayList<HashMap<String, String>> articles) {

            mAdapter = new ArticleAdapter(articles);

            setListAdapter(mAdapter);

            mPd.dismiss();
        }

        @Override
        public void onLoaderReset(
                Loader<ArrayList<HashMap<String, String>>> loader) {
        }
    };

    private class ArticleAdapter extends ArrayAdapter<HashMap<String, String>> {

        private ArticleAdapter(ArrayList<HashMap<String, String>> list) {
            super(MainActivity.this, R.layout.list_item, list);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = convertView;
            if (row == null) {
                LayoutInflater inflater = MainActivity.this.getLayoutInflater();
                row = inflater.inflate(R.layout.list_item, parent, false);
            }
            HashMap<String, String> article = getItem(position);
            ((TextView) row.findViewById(R.id.title)).setText(article
                    .get(GetArticles.KEY_TITLE));
            ((TextView) row.findViewById(R.id.content)).setText(Html
                    .fromHtml(article.get(GetArticles.KEY_CONTENT)));
            return row;
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, MENU_NEWS, 0, "Vijesti");
        menu.add(0, MENU_ABOUT, 0, "O aplikaciji");
        menu.add(0, MENU_QUIT, 0, "Izlaz");
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

        case MENU_NEWS:
            Intent i = new Intent(MainActivity.this, MainActivity.class);
            startActivity(i);
            return true;

        case MENU_ABOUT:
            new AlertDialog.Builder(this)
                    .setTitle("Application info")
                    .setMessage("Version: 1.0")
                    .setPositiveButton(android.R.string.yes,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    return;
                                }
                            }).show();
            return true;

        case MENU_QUIT:
            finish();
            return true;

        }
        return false;
    }

    protected OnRefreshListener mOnRefreshListener = new OnRefreshListener() {

        @Override
        public void onRefresh() {
            Toast.makeText(getBaseContext(), "We're refreshing!", Toast.LENGTH_SHORT).show();
        }

    };
}

I tried to extract the method when returning new articles, and then use it in on refresh, but it won't work. Is there any way to extract the method to stop the pull to refresh when new articles have been retrieved?

Thanks in advance.