2012-04-22 77 views
0

我正在嘗試構建一個Android應用程序,該應用程序顯示幾個RSS源,並且我想顯示帶有標題和日期的RSS源,然後獲得用戶點擊它們查看完整的故事。目前,我將RSS行值作爲其行文字視圖中的意圖附加值傳遞。然而,我不能沒有TextView的故事,但仍然獲得這些數據作爲一個額外的意圖。這裏是我的代碼的相關位:在Android中使用TextView獲取RSS Feed行項目的值

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中輸出數據的情況下獲取任何想法?提前致謝!

+0

你可以在你的代碼中使用textview存儲的東西,以及在哪裏閱讀它? – zapl 2012-04-22 21:32:24

+0

對不起,我還是這個網站的新手!我點擊以使相關線條變粗,但他們只在兩邊留下了2顆星。獲取值的線大約下降了三分之一,讀取值的線就在底部附近。抱歉! – 2012-04-22 21:40:18

回答

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.. 
    } 
} 
+0

非常感謝zapl - 一個快速和完美的解決方案! – 2012-04-22 21:55:37