我正在为我的校报创建一个应用程序,但在尝试显示全文时遇到了问题。目前我有一个从 RSS 提要中提取的文章列表,当单击它时,它会显示文章的内容。然而,只有第一段显示在 TextView 中,无论它有多长。这让我也相信它与<p></p>
HTML 标签有关。我对 RSS 提要或解析 XML 不是很熟悉(这是我第一次尝试),并且一直在寻找方法来完成我想要完成的工作。
我的这个项目基于这个博客的一系列帖子:http ://android-er.blogspot.com/2010/04/simple-rss-reader-in-listview.html
我采用了此处提供的项目并将其添加到带有滑动模板的选项卡中,并更改了某些元素的布局方式。
免责声明:有很多评论丑陋的代码,我懒得拿出来。这主要是我在布局中不想要的东西。这是包含文章列表的片段:
public class AllStoriesFragment extends ListFragment {
/*********************************************************************
* RSS Async Task
*********************************************************************/
public class RssLoadingTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
displayRss();
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
preReadRss();
}
@Override
protected void onProgressUpdate(Void... values) {
// TODO Auto-generated method stub
//super.onProgressUpdate(values);
}
@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
readRss();
return null;
}
}
/*********************************************************************
* End RSS Async Task
*********************************************************************/
private RSSFeed myRssFeed = null;
TextView feedTitle;
TextView feedDescription;
//TextView feedPubdate;
TextView feedLink;
//TextView feedContent;
/*********************************************************************
* Custom Array Adapter
*********************************************************************/
public class MyCustomAdapter extends ArrayAdapter<RSSItem> {
public MyCustomAdapter(Context context, int textViewResourceId,
List<RSSItem> list) {
super(context, textViewResourceId, list);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//return super.getView(position, convertView, parent);
View row = convertView;
if(row==null){
LayoutInflater inflater=getActivity().getLayoutInflater();
row=inflater.inflate(R.layout.row, parent, false);
}
TextView listTitle=(TextView)row.findViewById(R.id.listtitle);
listTitle.setText(myRssFeed.getList().get(position).getTitle());
TextView listPubdate=(TextView)row.findViewById(R.id.listpubdate);
listPubdate.setText(myRssFeed.getList().get(position).getPubdate());
if (position%2 == 0){
listTitle.setBackgroundColor(0xff101010);
listPubdate.setBackgroundColor(0xff101010);
}
else{
listTitle.setBackgroundColor(0xff080808);
listPubdate.setBackgroundColor(0xff080808);
}
return row;
}
}
/*********************************************************************
* End Custom Array Adapter
*********************************************************************/
/** Called when the fragment is first created. */
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_allstories, null);;
feedTitle = (TextView)v.findViewById(R.id.feedtitle);
feedDescription = (TextView)v.findViewById(R.id.feeddescription);
//feedPubdate = (TextView)v.findViewById(R.id.feedpubdate);
feedLink = (TextView)v.findViewById(R.id.feedlink);
startReadRss();
return v ;
}
private void startReadRss(){
new RssLoadingTask().execute();
}
private void preReadRss(){
setListAdapter(null);
Toast.makeText(getActivity(), "Reading RSS, Please wait.", Toast.LENGTH_LONG).show();
}
private void readRss(){
try {
URL rssUrl = new URL("http://www.campusslate.com/feed/");
SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();
SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
XMLReader myXMLReader = mySAXParser.getXMLReader();
RSSHandler myRSSHandler = new RSSHandler();
myXMLReader.setContentHandler(myRSSHandler);
InputSource myInputSource = new InputSource(rssUrl.openStream());
myXMLReader.parse(myInputSource);
myRssFeed = myRSSHandler.getFeed();
}
catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void displayRss(){
if (myRssFeed!=null){
MyCustomAdapter adapter = new MyCustomAdapter(getActivity(), R.layout.row, myRssFeed.getList());
setListAdapter(adapter);
}
}
public void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
Intent intent = new Intent(getActivity(), ShowDetails.class);
intent.putExtra("keyPubdate", myRssFeed.getItem(position).getPubdate());
intent.putExtra("keyLink", myRssFeed.getItem(position).getLink());
intent.putExtra("keyTitle", myRssFeed.getItem(position).getTitle());
intent.putExtra("keyContent", myRssFeed.getItem(position).getContent());
startActivity(intent);
}
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add(0, 0, 0, "Reload");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case (0): startReadRss();
break;
default:
break;
}
return true;
}
}
这是单击列表中的项目时启动的活动:
package com.nick.pocketslate;
import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.text.method.ScrollingMovementMethod;
import android.widget.TextView;
public class ShowDetails extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.details);
TextView detailsTitle = (TextView)findViewById(R.id.detailstitle);
TextView detailsPubdate = (TextView)findViewById(R.id.detailspubdate);
TextView detailsLink = (TextView)findViewById(R.id.detailslink);
TextView detailsContent = (TextView)findViewById(R.id.detailscontent);
Intent intent = getIntent();
detailsTitle.setText(intent.getStringExtra("keyTitle"));
detailsPubdate.setText(intent.getStringExtra("keyPubdate"));
detailsLink.setText(intent.getStringExtra("keyLink"));
detailsContent.setText(Html.fromHtml(intent.getStringExtra("keyContent")));
detailsContent.setMovementMethod(new ScrollingMovementMethod());
}
}
在这一点上,我已经做了很多搜索并没有找到解决方案,我不确定我的代码的哪一部分可能会导致问题。如果有人可以查看并找到问题,我有一个链接可以在此处下载我的完整项目。
如果有人知道从哪里开始寻找,我会提出那部分代码。