我创建了一个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 adapter = new ArrayAdapter(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:
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Android RSSReader"
android:id="@+id/feedtitle"/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/feedpubdate"/>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/itemlist"
android:fastScrollEnabled="true"/>
这就是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之外,它还需要三个可能对你来说不熟悉的参数:
>一个列表>您放置希望ListView显示的元素的实例.元素是Map的形式,即类似于由名称/值对形式的属性组成的结构.例如,您可以分别使用“标题”和“日期”作为每个RSS项目的标题和日期的键.
>一个字符串数组,用于将键的名称放在要在ListView上显示的每个映射中.
>一个整数数组,您需要在列表项视图中放置部件的ID,以便显示前面的字符串数组中的键所引用的单个元素.例如,如果要分别在“item”和“sub item”视图中显示RSS项的标题和日期,则使用new String [] {“title”,“date”}作为字符串参数数组和new int [] {android.R.id.text1,android.R.id.text2}作为这个参数.
一个粗略的代码示例,只是为了给你一个想法:
List> data = new ArrayList>();
for (RSSItem item : feed.getAllItems()) {
Map datum = new HashMap(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参数中指定的所有条目”,因此标题和日期应始终存在.
请注意,这一切都是我的头脑.我实际上没有测试过所有的代码,因此您可能会遇到一些需要调整或修复的怪癖或错误.
标签:android,java,android-layout,listview
来源: https://codeday.me/bug/20190925/1817420.html