在Android中使用TextView获取RSS Feed行项的值

时间:2012-04-22 21:24:14

标签: android android-intent rss

我正在尝试构建一个显示几个RSS源的Android应用程序,我想显示带有标题和日期的RSS源,然后让用户点击它们来查看完整的故事。目前我正在将RSS行值作为其Text行的意图附加内容传递。但是,我不能没有TextView的故事,但仍然可以将此数据作为额外的意图传递。这是我的代码的相关部分:

public class RSSFeedView extends ListActivity implements OnItemClickListener
{
Intent intent;
private String thread;

private RSSFeed feed = null;
RSSHandler rh = new RSSHandler();
private MyRowAdapter myRowAdapter;
private ArrayList<RSSItem> items;
private final static String TAG = "MAD Assignment";
public String description;

private ProgressDialog pbarDialog;

/** Called when the activity is first created */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    intent = getIntent();
    thread = intent.getStringExtra("thread");

    StrictMode.setThreadPolicy(
    new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork()   
    // or .detectAll() for all detectable 
                .penaltyLog().build());

    items = new ArrayList<RSSItem>();
    myRowAdapter = new MyRowAdapter(this);
    setListAdapter(myRowAdapter);

    new DownloadFilesTask().execute(thread);
}


class MyRowAdapter extends ArrayAdapter<RSSItem>
{
    Activity context;

    MyRowAdapter (Activity context)
    {
        super(context, R.layout.rss, items);
        this.context=context;
    }

    public View getView(final int position, View convertView, ViewGroup parent)
    {
        View row = convertView;
        if(row == null)
        {
            LayoutInflater inflater = context.getLayoutInflater();
            row = inflater.inflate(R.layout.rss, null);
        }

        RSSItem _item = items.get(position);

        TextView title = (TextView)row.findViewById(R.id.rssTitle);
        title.setText(_item.getTitle());
        **TextView feed = (TextView)row.findViewById(R.id.itemlist);
        feed.setText(_item.getDescription());**
        //description = _item.getDescription();    TRY TO GET IT WORKING WITH JUST DATE AND TITLE!!!!!!!!
        TextView date = (TextView)row.findViewById(R.id.rssDate);
        String dateStr = ConvertToDate(_item.getPubDate());
        date.setText(dateStr);

        row.setOnClickListener(new OnItemClickListener(position));

        return (row);
    }
}

@Override
public boolean onCreateOptionsMenu (Menu menu)
{
    new MenuInflater(getApplication()).inflate(R.menu.rss_menu, menu);
    return (super.onCreateOptionsMenu(menu));
}

@Override
public boolean onOptionsItemSelected (MenuItem item)
{
    switch (item.getItemId())
    {
        case R.id.rss1:
            new DownloadFilesTask().execute(thread);
            break;
    }
    return super.onOptionsItemSelected(item);
}

private class DownloadFilesTask extends AsyncTask<String, Integer, RSSFeed>
{
    @Override
    protected RSSFeed doInBackground(String... urls)
    {
        // TODO Auto-generated method stub
        feed = new RSSFeed();
        try
        {
            feed = getFeed (urls[0]);
        }
        catch (Exception e)
        {
            feed = null;
        }
        return feed;
    }

    @Override
    protected void onCancelled()
    {
        // TODO Auto-generated method stub
        super.onCancelled();
    }

    @Override
    protected void onPostExecute(RSSFeed _feed)
    {
        // TODO Auto-generated method stub
        super.onPostExecute(_feed);
        if (_feed!=null)
        {
            hideProgressBar();
            Log.v(TAG,"onPostExecute");
            for (RSSItem _item : _feed.getAllItems())
            {
                // needed as items have to be explicitly added
                // for notifyDataSetChanged to work
                items.add(_item);
            }
            myRowAdapter.notifyDataSetChanged();
        }
        else
        {
            showError();
        }
    }

    @Override
    protected void onPreExecute()
    {
        // TODO Auto-generated method stub
        super.onPreExecute();
        showProgressBar();
    }

    @Override
    protected void onProgressUpdate(Integer... values)
    {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);
    }
}


private RSSFeed getFeed (String urlToRssFeed)
{
    try
    {
        URL url = new URL(urlToRssFeed);
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser parser = factory.newSAXParser();

        XMLReader xmlreader = parser.getXMLReader();
        RSSHandler theRssHandler = new RSSHandler();

        xmlreader.setContentHandler(theRssHandler);
        InputSource is = new InputSource(url.openStream());
        xmlreader.parse(is);
        return theRssHandler.getFeed();
    }
    catch (Exception e)
    {
        return null;
    }
}

private class OnItemClickListener implements OnClickListener
{
   private int mPosition;

    OnItemClickListener(int position)
    {
            mPosition = position;
    }


    @Override
    public void onClick(View row) 
    {
        // TODO Auto-generated method stub
        final String title = (String) ((TextView) row.findViewById(R.id.rssTitle)).getText();
        final String date = (String) ((TextView) row.findViewById(R.id.rssDate)).getText();
        **final String desc = (String) ((TextView) row.findViewById(R.id.itemlist)).getText();**
        //final String desc = description;

        Intent showDetailedIntent = new Intent(RSSFeedView.this, RSSDetailed.class);

        showDetailedIntent.putExtra("title", title);
        showDetailedIntent.putExtra("date", date);
        showDetailedIntent.putExtra("desc", desc);

        startActivity(showDetailedIntent);
    }
}

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) 
{
    // TODO Auto-generated method stub

}


}

然后在我的RSSDetailed类中,我只是提取意图。有关如何获取数据而不先在TextView中输出数据的任何想法?提前谢谢!

1 个答案:

答案 0 :(得分:1)

例如,将RSSItem传递给您的侦听器。

public View getView(final int position, View convertView, ViewGroup parent)
{
    row.setOnClickListener(new OnItemClickListener(position, _item));
}


private class OnItemClickListener implements OnClickListener
{
   private int mPosition;
   private RSSItem mItem;

    OnItemClickListener(int position, RSSItem item)
    {
            mPosition = position;
            mItem = item;
    }


    @Override
    public void onClick(View row) 
    {
        final String title = mItem.getTitle(); 
        final String desc = mItem.getDescription();
        // etc..
    }
}