
我创建了一个RSS阅读器,列出了ListvIEw中的项目.我还想在每个项目下面有一个日期,但我不知道该怎么做.我需要别人的帮助才能使Sub Item文本显示从RSS Feed检索到的pubDate.
这是我班上的代码:
public class RSSReader extends Activity implements OnItemClickListener{ public final String RSSFeedOFCHOICE = "http://app.calvaryccm.com/mobile/androID/v1/devos"; public final String tag = "RSSReader"; private RSSFeed Feed = null; /** Called when the activity is first created. */ public voID onCreate(Bundle icicle) { super.onCreate(icicle); setContentVIEw(R.layout.main); // go get our Feed! Feed = getFeed(RSSFeedOFCHOICE); // display UI Updatedisplay(); } private RSSFeed getFeed(String urlToRSSFeed) { try { // setup the url URL url = new URL(urlToRSSFeed); // create the factory SAXParserFactory factory = SAXParserFactory.newInstance(); // create a parser SAXParser parser = factory.newSAXParser(); // create the reader (scanner) XMLReader xmlreader = parser.getXMLReader(); // instantiate our handler RSSHandler theRSSHandler = new RSSHandler(); // assign our handler xmlreader.setContentHandler(theRSSHandler); // get our data via the url class inputSource is = new inputSource(url.openStream()); // perform the synchronous parse xmlreader.parse(is); // get the results - should be a fully populated RSSFeed instance, or null on error return theRSSHandler.getFeed(); } catch (Exception ee) { // if we have a problem, simply return null System.out.println(ee.getMessage()); System.out.println(ee.getStackTrace()); System.out.println(ee.getCause()); return null; } } public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); menu.add(Menu.NONE, 0, 0, "Refresh"); Log.i(tag,"onCreateOptionsMenu"); return true; } public boolean onoptionsItemSelected(MenuItem item){ switch (item.getItemID()) { case 0: Log.i(tag,"Set RSS Feed"); return true; case 1: Log.i(tag,"Refreshing RSS Feed"); return true; } return false; } private voID Updatedisplay() { TextVIEw FeedTitle = (TextVIEw) findVIEwByID(R.ID.FeedTitle); TextVIEw Feedpubdate = (TextVIEw) findVIEwByID(R.ID.Feedpubdate); ListVIEw itemList = (ListVIEw) findVIEwByID(R.ID.itemList); if (Feed == null) { FeedTitle.setText("No RSS Feed Available"); return; } if(FeedTitle != null) FeedTitle.setText(Feed.getTitle()); if(Feedpubdate != null) Feedpubdate.setText(Feed.getPubDate()); ArrayAdapter<RSSItem> adapter = new ArrayAdapter<RSSItem>(this,androID.R.layout.simple_List_item_1,Feed.getAllitems()); itemList.setAdapter(adapter); itemList.setonItemClickListener(this); itemList.setSelection(0); } @OverrIDe public voID onItemClick(AdapterVIEw parent, VIEw v, int position, long ID) { //Log.i(tag,"item clicked! [" + Feed.getItem(position).getTitle() + "]"); Intent itemintent = new Intent(this,ShowDescription.class); Bundle b = new Bundle(); b.putString("Title", Feed.getItem(position).getTitle()); b.putString("description", Feed.getItem(position).getDescription()); b.putString("link", Feed.getItem(position).getlink()); b.putString("pubdate", Feed.getItem(position).getPubDate()); itemintent.putExtra("androID.intent.extra.INTENT", b); startActivity(itemintent); }}这是我的XML:
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"androID:orIEntation="vertical"androID:layout_wIDth="fill_parent"androID:layout_height="fill_parent"> <TextVIEw androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:text="AndroID RSSReader"androID:ID="@+ID/FeedTitle"/><TextVIEw androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:text=""androID:ID="@+ID/Feedpubdate"/><ListVIEwandroID:layout_wIDth="fill_parent"androID:layout_height="fill_parent"androID:ID="@+ID/itemList"androID:fastScrollEnabled="true"/> </linearLayout>这就是Eclipse现在的样子:
这就是它的运行:
如何使Sub Item文本显示从RSS提要检索的pubDate?
解决方法:
最简单的解决方案可能是使用SimpleAdapter和androID.R.layout.simple_List_item_2预定义布局替换您正在使用的ArrayAdapter和androID.R.layout.simple_List_item_1.这个布局由两个TextVIEw组成,其ID分别为androID.R.ID.text1(“item”)和androID.R.ID.text2(“sub item”),您需要将其作为参考SimpleAdapter工作.
对于SimpleAdapter,你会注意到,除了一个Context实例和一个布局资源的ID之外,它还需要三个可能对你来说不熟悉的参数:
>一个列表<?扩展Map< String,?>>您放置希望ListVIEw显示的元素的实例.元素是Map的形式,即类似于由名称/值对形式的属性组成的结构.例如,您可以分别使用“标题”和“日期”作为每个RSS项目的标题和日期的键.
>一个字符串数组,用于将键的名称放在要在ListVIEw上显示的每个映射中.
>一个整数数组,您需要在列表项视图中放置部件的ID,以便显示前面的字符串数组中的键所引用的单个元素.例如,如果要分别在“item”和“sub item”视图中显示RSS项的标题和日期,则使用new String [] {“Title”,“date”}作为字符串参数数组和new int [] {androID.R.ID.text1,androID.R.ID.text2}作为这个参数.
一个粗略的代码示例,只是为了给你一个想法:
List<Map<String, String>> data = new ArrayList<Map<String, String>>();for (RSSItem item : Feed.getAllitems()) { Map<String, String> datum = new HashMap<String, String>(2); datum.put("Title", item.getTitle()); datum.put("date", item.getDate().toString()); data.add(datum);}SimpleAdapter adapter = new SimpleAdapter(this, data, androID.R.layout.simple_List_item_2, new String[] {"Title", "date"}, new int[] {androID.R.ID.text1, androID.R.ID.text2});itemList.setAdapter(adapter);该文档指出“映射包含每行的数据,并应包括from参数中指定的所有条目”,因此标题和日期应始终存在.
请注意,这一切都是我的头脑.我实际上没有测试过所有的代码,因此您可能会遇到一些需要调整或修复的怪癖或错误.
总结以上是内存溢出为你收集整理的java – 在Android中添加ListView子项文本全部内容,希望文章能够帮你解决java – 在Android中添加ListView子项文本所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)