从我的MainActivity

时间:2017-07-05 18:09:52

标签: java android

我正在制作一个基本的应用程序,只是从谷歌读取RSS文件,并在列表视图中显示标题。我遇到的问题是,当我启动服务的意图后访问它时,我在IntentService中使用的静态ArrayList是空的。可能有一些非常基本的东西我在这里失踪但是在我添加更多RSS链接到服务之前,山雀曾经工作过。我试着将它们评论出来,看看某处是否存在某种超载,但没有任何改变,我想这意味着我在没有意识到或记住它的情况下改变了其他东西。

这是我的相关代码,我试图访问的ArrayList是RSSsearcher中的newsList变量。在过去,我记录了arraylist的字符串值,并确认它已在RSSsearcher类中填充,但在MainActivity类中为空。

相关代码:

RSSsearcher:

public class RSSsearcher extends IntentService {
    public static ArrayList<NewsCard> newsList = new ArrayList<>();
    private static String TAG = "RSS";

    public RSSsearcher() {
        super("RSSsearcher");
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        parseNewsLists();
        Log.i(TAG,"newsList in onHandleIntent" + newsList);
    }

    public static ArrayList<NewsCard> getNewsCards() {
        return newsList;
    }

    //helper method to create all the RSSfeed objects
    private void parseNewsLists() {
        Log.i(TAG, "the service has been started");
        //Creating the RSS feed objects
        RSSfeed topStories = new RSSfeed("https://news.google.com/news?cf=all&hl=en&pz=1&ned=us&output=rss");
        RSSfeed worldNews = new RSSfeed("https://news.google.com/news/rss/headlines/section/topic/WORLD?ned=us&hl=en");
        RSSfeed usNews = new RSSfeed("https://news.google.com/news/rss/headlines/section/topic/NATION?ned=us&hl=en");
        RSSfeed buisnessNews = new RSSfeed("https://news.google.com/news/rss/headlines/section/topic/BUSINESS?ned=us&hl=en");
        RSSfeed technologyNews = new RSSfeed("https://news.google.com/news/rss/headlines/section/topic/TECHNOLOGY?ned=us&hl=en");
        RSSfeed entertainmentNews = new RSSfeed("https://news.google.com/news/rss/headlines/section/topic/ENTERTAINMENT?ned=us&hl=en");
        RSSfeed sportsNews = new RSSfeed("https://news.google.com/news/rss/headlines/section/topic/SPORTS?ned=us&hl=en");
        RSSfeed scienceNews = new RSSfeed("https://news.google.com/news/rss/headlines/section/topic/SCIENCE?ned=us&hl=en");
        RSSfeed healthNews = new RSSfeed("https://news.google.com/news/rss/headlines/section/topic/HEALTH?ned=us&hl=en");

        try {
            //getting RSS feeds
            topStories.FileReader();
            worldNews.FileReader();
            usNews.FileReader();
            buisnessNews.FileReader();
            technologyNews.FileReader();
            entertainmentNews.FileReader();
            sportsNews.FileReader();
            scienceNews.FileReader();
            healthNews.FileReader();
        } catch (Exception e) {
            Log.i(TAG, Log.getStackTraceString(e));
        }
        //adding stories to main
        newsList.addAll(topStories.getNews());
        newsList.addAll(worldNews.getNews());
        newsList.addAll(usNews.getNews());
        newsList.addAll(buisnessNews.getNews());
        newsList.addAll(technologyNews.getNews());
        newsList.addAll(entertainmentNews.getNews());
        newsList.addAll(sportsNews.getNews());
        newsList.addAll(scienceNews.getNews());
        newsList.addAll(healthNews.getNews());
    }

}

MainActivity:

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

        //TODO remove and have actual tags show up
        TagCard onecard = new TagCard("TestCard");
        TagCard twocard = new TagCard("TestCard2");

        dummyTags.add(onecard);
        dummyTags.add(twocard);

        mainListView = (ListView) findViewById(R.id.main_content_list);
        drawerLayout = (DrawerLayout) findViewById(R.id.main_layout);
        tagDrawerList = (ListView) findViewById(R.id.tag_drawer);

        //setting an intent for the RSSsearcher to fetch the news
        //TODO make this happen every 15 minutes or so
        Intent intent = new Intent(this, RSSsearcher.class);
        startService(intent);

        NewsCard card = new NewsCard("Testing", "linkeroni");

        //adding all the NewsCard objects to this classes newsList
        newsList.addAll(RSSsearcher.getNewsCards());

        MainAdapter adapter = new MainAdapter(this, newsList);
        mainListView.setAdapter(adapter);

        Log.i(TAG, "This is the array from main activity" + newsList.toString());

        actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.open_drawer, R.string.close_drawer){

            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
                getActionBar().setTitle("Closed");
            }

            public void onDrawerOpened(View view){
                super.onDrawerOpened(view);
                getActionBar().setTitle("Open");
            }
        };

        //TODO removed dummytags and add actual tag implementation
        tagDrawerList.setAdapter(new TagAdapter(this,dummyTags));

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

    }

提前谢谢!

1 个答案:

答案 0 :(得分:0)

我认为你打电话时

newsList.addAll(RSSsearcher.getNewsCards());

意图仍在执行中