我有一个带有 3 个标签的 rss 提要,每个标签都从一个链接中获取 rss。rss 工作正常,但我想在图片链接的这一部分的每个提要下方添加日期和时间,并使其像这个图片链接。谁能帮助我。
这是我的完整代码:
RssTabsActivity.java
public class RssTabsActivity extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rss_tabs);
TabHost tabHost = getTabHost();
Intent artIntent = new Intent().setClass(this, RssChannelActivity.class);
artIntent.putExtra("rss-url", "http://www1.up.edu.ph/index.php/feed/");
String artTabName = getResources().getString(R.string.tab_art);
TabSpec artTabSpec = tabHost.newTabSpec(artTabName)
.setIndicator(artTabName, getResources().getDrawable(R.drawable.rss_tab_art))
.setContent(artIntent);
tabHost.addTab(artTabSpec);
Intent techIntent = new Intent().setClass(this, RssChannelActivity.class);
techIntent.putExtra("rss-url", "http://www1.up.edu.ph/index.php/feed/");
String techTabName = getResources().getString(R.string.tab_tech);
TabSpec techTabSpec = tabHost.newTabSpec(techTabName)
.setIndicator(techTabName, getResources().getDrawable(R.drawable.rss_tab_tech))
.setContent(techIntent);
tabHost.addTab(techTabSpec);
Intent sportsIntent = new Intent().setClass(this, RssChannelActivity.class);
sportsIntent.putExtra("rss-url", "http://www1.up.edu.ph/index.php/feed/");
String sportsTabName = getResources().getString(R.string.tab_sports);
TabSpec sportsTabSpec = tabHost.newTabSpec(sportsTabName)
.setIndicator(sportsTabName, getResources().getDrawable(R.drawable.rss_tab_sports))
.setContent(sportsIntent);
tabHost.addTab(sportsTabSpec);
tabHost.setCurrentTab(1);
}
}
RssChannelActivity.java
public class RssChannelActivity extends Activity {
private RssChannelActivity local;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rss_channel);
String rssUrl = (String)getIntent().getExtras().get("rss-url");
local = this;
GetRSSDataTask task = new GetRSSDataTask();
task.execute(rssUrl);
}
private class GetRSSDataTask extends AsyncTask<String, Void, List<RssItem> > {
@Override
protected List<RssItem> doInBackground(String... urls) {
try {
RssReader rssReader = new RssReader(urls[0]);
return rssReader.getItems();
} catch (Exception e) {
Log.e("RssChannelActivity", e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(List<RssItem> result) {
ListView itcItems = (ListView) findViewById(R.id.rssChannelListView);
ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem>
(local,android.R.layout.simple_list_item_1, result);
itcItems.setAdapter(adapter);
itcItems.setOnItemClickListener(new ListListener(result, local));
}
}
}
RssItem.java
public class RssItem {
private String title;
private String link;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
@Override
public String toString() {
return title;
}
}
监听器.java
public class ListListener implements OnItemClickListener {
List<RssItem> listItems;
Activity activity;
public ListListener(List<RssItem> aListItems, Activity anActivity) {
listItems = aListItems;
activity = anActivity;
}
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(listItems.get(pos).getLink()));
activity.startActivity(i);
}
}
RssParseHandler.java
public class RssParseHandler extends DefaultHandler {
private List<RssItem> rssItems;
private RssItem currentItem;
private boolean parsingTitle;
private StringBuffer currentTitleSb;
private boolean parsingLink;
public RssParseHandler() {
rssItems = new ArrayList<RssItem>();
}
public List<RssItem> getItems() {
return rssItems;
}
@Override
public void startElement(String uri, String localName, String qName, Attributes
attributes) throws SAXException {
if ("item".equals(qName)) {
currentItem = new RssItem();
} else if ("title".equals(qName)) {
parsingTitle = true;
currentTitleSb = new StringBuffer();
} else if ("link".equals(qName)) {
parsingLink = true;
}
}
@Override
public void endElement(String uri, String localName, String qName) throws
SAXException {
if ("item".equals(qName)) {
rssItems.add(currentItem);
currentItem = null;
} else if ("title".equals(qName)) {
parsingTitle = false;
if (currentItem != null) {
// Set item's title here
currentItem.setTitle(currentTitleSb.toString());
}
} else if ("link".equals(qName)) {
parsingLink = false;
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if (parsingTitle) {
if (currentItem != null) {
currentTitleSb.append(new String(ch, start, length));
}
} else if (parsingLink) {
if (currentItem != null) {
currentItem.setLink(new String(ch, start, length));
parsingLink = false;
}
}
}
}
RssReader.java
public class RssReader {
private String rssUrl;
public RssReader(String rssUrl) {
this.rssUrl = rssUrl;
}
public List<RssItem> getItems() throws Exception {
// SAX parse RSS data
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
RssParseHandler handler = new RssParseHandler();
saxParser.parse(rssUrl, handler);
return handler.getItems();
}
}
RssTabsLayout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RssTabsActivity" >
<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="15dp"
android:visibility="invisible" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/tab_art"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
<LinearLayout
android:id="@+id/tab_tech"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
<LinearLayout
android:id="@+id/tab_sports"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</RelativeLayout>
RssChannelLayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/rssChannelListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
</LinearLayout>