Android Open Source - AURHelperDroid News R S S Frag






From Project

Back to project page AURHelperDroid.

License

The source code is released under:

Apache License

If you think the Android project AURHelperDroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.stomachion.aurhelperdroid.fragments;
//from   w  w  w .  ja  va  2 s.c  om
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import com.stomachion.aurhelperdroid.R;
import com.stomachion.aurhelperdroid.adapters.NewsAdapter;
import com.stomachion.aurhelperdroid.logic.NewsItem;
import com.stomachion.aurhelperdroid.network.InternetState;
import com.stomachion.aurhelperdroid.parser.MainRssParser;
import com.stomachion.aurhelperdroid.utils.CommonUtils;
import com.stomachion.aurhelperdroid.utils.Constants;

import java.util.ArrayList;

/**
 * User: Pedro Veloso
 */
public class NewsRSSFrag extends Fragment {

    private ArrayList<NewsItem> mNewsItems;
    private NewsAdapter mNewsAdapter;
    private View mParentView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        final View v = inflater.inflate(R.layout.news_screen, container, false);
        mParentView = v;
        new ProcessFeedFetchOnBackground().execute();
        return v;
    }


    /**
     * AsyncTask fetches the data and parses the data on a separate thread, not blocking the UI
     */
    private final class ProcessFeedFetchOnBackground extends AsyncTask<Void, Void, Integer> {

        protected Integer doInBackground(Void... ignored) {
            if (InternetState.isConnectedToInternet(getActivity())) {
                try {
                    mNewsItems = MainRssParser.parseURL(Constants.URL_NEWS_FEED);

                    return Constants.ALL_OK;
                } catch (Exception e) {
                    CommonUtils.debugFunc("Could not obtain feed from web. E.: " + e.getMessage(), Log.ERROR);
                    e.printStackTrace();
                    return Constants.ERROR_OTHER;
                }
            } else
                return Constants.ERROR_NO_INTERNET;
        }

        protected void onPostExecute(Integer result) {
            CommonUtils.debugFunc("AsyncTask result: " + result, Log.DEBUG);

            ListView listView = (ListView) mParentView.findViewById(R.id.lv_list);


            switch (result) {
                case Constants.ALL_OK:
                    drawList();
                    break;

                case Constants.ERROR_NO_INTERNET:
                    break;

                case Constants.ERROR_OTHER:
                    break;
            }
        }
    }

    /**
     * Draws the listview with news elements
     */
    private void drawList() {
        if (mNewsItems != null && mNewsItems.size() > 0) {
            ListView listView = (ListView) mParentView.findViewById(R.id.lv_list);
            // if there are more then 10 items enable Android's Fast Scroll functionality
            if (mNewsItems.size() > 10) {
                listView.setFastScrollEnabled(true);
            }

            mNewsAdapter = new NewsAdapter(getActivity(), mNewsItems);
            listView.setAdapter(mNewsAdapter);
        }
    }
}




Java Source Code List

com.stomachion.aurhelperdroid.activities.MainActivity.java
com.stomachion.aurhelperdroid.activities.PreferencesActivity.java
com.stomachion.aurhelperdroid.adapters.NewsAdapter.java
com.stomachion.aurhelperdroid.fallbacksupport.ActivityHostFragment.java
com.stomachion.aurhelperdroid.fallbacksupport.LocalActivityManagerFragment.java
com.stomachion.aurhelperdroid.fragments.AboutFrag.java
com.stomachion.aurhelperdroid.fragments.NewsRSSFrag.java
com.stomachion.aurhelperdroid.fragments.PreferencesFrag.java
com.stomachion.aurhelperdroid.fragments.SearchFrag.java
com.stomachion.aurhelperdroid.logic.NewsItem.java
com.stomachion.aurhelperdroid.network.InternetState.java
com.stomachion.aurhelperdroid.parser.MainRssParser.java
com.stomachion.aurhelperdroid.parser.RssNewsHandler.java
com.stomachion.aurhelperdroid.utils.CommonUtils.java
com.stomachion.aurhelperdroid.utils.Constants.java
com.stomachion.aurhelperdroid.utils.Prefs.java