0

我正在尝试构建一个显示几个 RSS 提要的 Android 应用程序,我想仅显示标题和日期的 RSS 提要,然后让用户单击它们以查看完整的故事。目前,我将 RSS 行值作为其行 Textviews 中的意图附加值传递。但是,我不能没有故事 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 中输出数据的情况下获取数据的任何想法?提前致谢!

4

1 回答 1

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..
    }
}
于 2012-04-22T21:50:14.750 回答