Rss Reader应用程序没有返回标题?

时间:2014-08-25 11:47:14

标签: android asynchronous rss

我已经按照教程向我展示了如何创建一个Rss feed阅读应用程序,但在运行它时...它只在listView中显示以下LogCat:

08-25 12:33:11.507: E/RESULT(13889): [com.example.rssapplication.RssItem1@4306f848, com.example.rssapplication.RssItem1@4306fcb0, com.example.rssapplication.RssItem1@4306ff90, com.example.rssapplication.RssItem1@430702d8, com.example.rssapplication.RssItem1@43070478, com.example.rssapplication.RssItem1@430705d0, com.example.rssapplication.RssItem1@43070788, com.example.rssapplication.RssItem1@43070ae8, com.example.rssapplication.RssItem1@43070ca0, com.example.rssapplication.RssItem1@43070e50, com.example.rssapplication.RssItem1@43071028, com.example.rssapplication.RssItem1@43071208, com.example.rssapplication.RssItem1@43071398, com.example.rssapplication.RssItem1@430715b0, com.example.rssapplication.RssItem1@43071768, com.example.rssapplication.RssItem1@43071918, com.example.rssapplication.RssItem1@43071ae8, com.example.rssapplication.RssItem1@43071cd8, com.example.rssapplication.RssItem1@43071e88]

这是主类代码:

*package com.example.rssapplication;

import java.util.List;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class DirectRSS extends Activity{



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


            GetRSSDataTask task = new GetRSSDataTask();

            // Start download RSS task
            task.execute("http://www.skysports.com/rss/0,20514,11661,00.xml");



            //Set to portrait, so that every time the view changes; it does not run the DB query again...
            setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);


        }



     private class GetRSSDataTask extends AsyncTask<String, Void, List<RssItem1> > {
            @Override
            protected List<RssItem1> doInBackground(String... urls) {

                // Debug the task thread name
                Log.d("ITCRssReader", Thread.currentThread().getName());

                try {
                    // Create RSS reader
                    RssReader1 rssReader = new RssReader1(urls[0]);

                    // Parse RSS, get items
                    return rssReader.getItems();


                } catch (Exception e) {
                    Log.e("ITCRssReader", e.getMessage());
                }

                return null;
            }

            @Override
            protected void onPostExecute(List<RssItem1> result) {

                // Get a ListView from main view
                ListView itcItems = (ListView) findViewById(R.id.listView1);
                //result = RssItem1.this.getTitle();             
                // Create a list adapter
                ArrayAdapter<RssItem1> adapter = new ArrayAdapter<RssItem1>(DirectRSS.this,android.R.layout.simple_list_item_1, result);*


  // Set list adapter for the ListView
            itcItems.setAdapter(adapter);
            Log.e("RESULT", result.toString());

            // Set list view item click listener
            itcItems.setOnItemClickListener(new ListListener1(result, DirectRSS.this));
        }
    }  






 }

RssItem1:

package com.example.rssapplication;

public class RssItem1 {

    private String title;
    private String link;


    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getLink() {
        return link;
    }
    public void setLink(String link) {
        this.link = link;
    }



}

它没有提供任何错误消息,我认为它可能与“结果”有关。实际上并没有包含标题。

任何帮助将不胜感激。

我可以包含我正在使用的其他类文件,如果有人希望看到它们

1 个答案:

答案 0 :(得分:0)

现在发生的事情是ArrayAdapter从数组中调用每个RssItem1调用其toString()方法并填充ListView。由于您尚未覆盖此方法,因此它会调用toString()类的Object。您可以尝试在RssItem1类中覆盖此方法以查看其工作原理。

作为一种解决方案,您可能希望实现BaseAdapter接口,以便正确填充列表中的视图。 example

编辑: 这只是一个例子,根据你在课堂上的内容进行调整。

public class RssItem1 {
/* your code */

    @Override
    public String toString () {
         return this.title + ":" + this.description;
    }
}

对于BaseAdapter,您可以按照链接上的教程进行操作。

相关问题