【问题标题】:Android TextView cuts off after one paragraphAndroid TextView 在一段后截断
【发布时间】:2012-12-03 19:28:17
【问题描述】:

我正在为我的校报创建一个应用程序,但在尝试显示完整文章时遇到了问题。目前我有一个从 RSS 提要中提取的文章列表,当单击它时,它会显示文章的内容。然而,只有第一段显示在 TextView 中,无论它有多长。这让我也相信它与<p></p> HTML 标签有关。我对 RSS 提要或解析 XML 不是很熟悉(这是我第一次尝试),并且一直在寻找方法来完成我想要完成的事情。

我的这个项目是基于这个博客的一系列帖子:http://android-er.blogspot.com/2010/04/simple-rss-reader-in-listview.html

我采用了此处提供的项目并将其添加到带有滑动模板的选项卡中,并更改了一些元素的布局方式。

DISCLAMER:有很多注释丑陋的代码,我懒得拿出来。这主要是我在布局中不想要的东西。 这是包含文章列表的片段:

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());

    }
}

此时我已经进行了大量搜索,但没有找到解决方案,我不确定我的代码的哪一部分可能导致问题。如果有人可以查看并找到问题,我有一个链接可以在此处下载我的完整项目。

如果有人知道从哪里开始寻找,我会贴出那段代码。

https://lh.rs/zbdYev99O1G5

【问题讨论】:

    标签: java android textview


    【解决方案1】:

    您显示的内容看起来是正确的,但是您的模型的代码在哪里 - RSSFeed?您可能希望获得一些调试输出,以显示您的 RSS 读取是否正常。也许您只得到摘要而不是全文?也许应该从它自己的链接中阅读完整的文章,而不是从删节的 RSS 提要中阅读?

    对于 HTML 内容,您通常使用LinkMovementMethod,以便正确处理内容中的链接。假设您没有设置 TextView.maxLines 属性(可能需要检查),TextViews 会自动滚动。

    此外,为简单起见,您应该直接从 Intent 添加/读取数据 - 实际上不需要通过中间 Bundle。 putExtra(String,String)getStringExtra(String) 或者您可以使用它们的 CharSequence 等效项,具体取决于 RSSReader 的确切输出内容。

    编辑:您是否正在检查 TextView 是否正常滚动显示超过 5 行的虚拟文本?您还可以尝试从外部复制您的提要中的内容数据(通过外部提要阅读器),并使用TextView.setText(CharSequence) 明确地将其放入您的 TextView 中,以检查提要本身中的某些内容是否导致您的问题。

    (Html.fromHtml 是基于 TagSoup 的,它旨在处理格式不佳的 HTML,但您始终可以尝试使用纯文本进行测试,看看是否有效。您还可以使用调试日志/LogCat检查 HTML 解析您的 RSS 提要内容之前/之后。)

    如果这些事情有问题,那么它可能是您的 TextView 布局/初始化的问题。如果这些都正常,那么您就知道问题出在您的 RSS 相关代码中。

    您可以发布详细视图的布局代码吗?另外,你用的是模拟器还是手机?有时模拟器对滚动手势的响应方式与实际设备的响应方式不同。

    编辑:如果问题出在 RSSHandler,我建议创建一个单独的/新问题,并适当标记(SAX、XML-Parser、RSS)。如果您在此处发布指向该新问题的链接,我可以尝试在那里提供帮助。您也可以先尝试寻找可能有助于使用 SAX 方法进行 XML 解析的具体答案。这是我最近一直在使用的一个很好的教程 - http://tutorials.jenkov.com/java-xml/sax.html

    【讨论】:

    • 就我从提要中获得的内容而言,它最初设计为仅提取描述元素,但我对其进行了修改以提取内容:编码元素下的完整文章。我确信我在那里拉正确的东西。我尝试了许多不同的方法来使 TextView 滚动,但我认为这不是问题,因为它只会显示一篇大文章中的 5 个句子段落,甚至不占屏幕的一半。就捆绑包而言,原作者就是这样做的,所以我从未更改过它。
    • 我通过将纯文本插入 TextView 进行了尝试,它滚动正常。所以我想问题出在RSS代码中。我现在在模拟器上测试,以后可以在手机上试试。这是我的 ShowDetails.java 的代码:pastebin.com/Y7fALE6N
    • 再次查看我的代码后,我觉得问题一定出在我的 RSSHandler 类中,但我没有看到任何明显的错误。有人可以看看吗? pastebin.com/ba1aWd4x
    • 什么都没有跳出来——我认为你将不得不添加一些调试输出,然后单步调试以找出问题所在。如果您使用的是 Eclipse,那么 Debug 视图是您的朋友……
    • 在我的 RSSHandler 中添加了一些调试代码后,似乎很多 RSSItem 和 RSSFeed 字段没有按应有的设置。我还尝试使用 CharSequences 而不是 Strings(不认为这会改变任何东西),我尝试使用 Intent 而不是捆绑包,但我不确定我这样做是否正确,你将如何实现它?
    猜你喜欢
    • 2014-12-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多