Android:添加ListView子项目文本

我创build了一个列表视图中列出项目的RSS阅读器。 我也想每个项目下面有一个date,但我不知道如何做到这一点。 我需要某人的帮助,使子项目文本显示从RSS提要检索pubDate。

这是我的class级代码:

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"/> <ListView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/itemlist" android:fastScrollEnabled="true"/> </LinearLayout> 

这就是Eclipse中现在的样子:

目前ListView

这是它看起来像运行:

正在处理中

我需要某人的帮助,使子项目文本显示从RSS提要检索pubDate。

最简单的解决scheme可能是使用SimpleAdapterandroid.R.layout.simple_list_item_2预定义布局replaceArrayAdapterandroid.R.layout.simple_list_item_1 。 这个布局由两个TextView组成,分别由android.R.id.text1 (“item”)和android.R.id.text2 (“sub item”)组成,您将需要它作为参考为SimpleAdapter工作。

通过查看 SimpleAdapter 的构造函数 ,您将会注意到,除了Context实例和布局资源的ID之外,它还有三个参数对您来说可能是新的:

  • 一个List<? extends Map<String, ?>> List<? extends Map<String, ?>>实例,在其中放置要显示ListView的元素。 元素是一个Map的forms,即类似于由名称/值对forms的属性组成的结构。 例如,您可以分别使用"title""date"作为每个RSS项目的标题和date的键。
  • 一个string数组,在哪里把你想要在ListView上显示的每个地图中的键的名称。
  • 一个整数数组,您需要将这些部分的ID放在列表项视图中,您希望显示前一个string数组中的键所引用的单个元素。 例如,如果要分别在“项目”和“子项目”视图中显示RSS项目的标题和date,则可以使用new String[] { "title", "date" }作为string参数数组, 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); 

该文件指出,“地图包含每行的数据,并应包括在参数中指定的所有条目”,所以标题和date应始终存在。

请注意,这是我的头顶。 我没有真正testing过所有的代码,所以你可能会遇到一些你需要调整或修复的怪癖或错误。

你应该有一个像这样的item_list.xml文件:

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="6dp" > <TextView android:id="@+id/page_title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#D8000000" android:textSize="16sp" android:textStyle="bold" /> <TextView android:id="@+id/page_date" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/page_title" android:ellipsize="marquee" android:lines="3" android:marqueeRepeatLimit="marquee_forever" android:textColor="#D8000000" android:textSize="12sp" /> </RelativeLayout> 

并在getview方法的listadapter中:

 View row = convertView; LayoutInflater inflater = ((Activity) mContext).getLayoutInflater(); row = inflater.inflate(R.layout.item_list, parent, false); TextView listTitle = (TextView) row.findViewById(R.id.page_title); listTitle.setText(title); TextView date = (TextView) row.findViewById(R.id.page_date); date.setText( <here put your date from RSS feed>); return row; 

这应该做到这一点!

由于要将多个数据项映射到多个视图,因此不能使用ArrayAdapter。 您将不得不使用SimpleAdapter 。

另外,我注意到你正在获取主要UI线程上的RSS提要。 这将导致您的应用程序非常不能响应。 如果您不知道我在说什么,请在“ 无痛线程”文章(我认为它是任何Android开发人员必读的文章)中select一个。 你应该做的是使用一个Loader 。 他们是专为你的types的情况。 尽pipe直到API-10才引入它们,但仍然可以通过支持包在较小的API级别上使用它们。