如何在 Android 中的 ListView 中显示已解析的 html

How to display parsed html in a ListView in Android

我正在尝试使用 jsoup 解析来自网站的新闻标题,并将它们显示在 ListView 中。很长一段时间以来,我一直在努力解决这个问题,并且疯狂地用谷歌搜索,但我无法解决我的问题或找到可行的解决方案。我有一个自定义 class,它包含两个变量,即新闻标题和文章的 link。似乎一切都解析得很好,但我无法让我的 ListView 正确显示或根本无法显示……它不断崩溃,并且每次我遇到不同的错误时都会接缝。也许我让自己太难了。我很沮丧,无法再从逻辑上思考了......我非常感谢任何和所有提示或有用的答案。

供稿 class:

public class Feeds {

private String mNewsTitle;
private String mNewsLink;

public Feeds(String newsTitle, String newsLink){
    mNewsTitle = newsTitle;
    mNewsLink = newsLink;
 }

public String getNewsTitle(){
    return mNewsTitle;
 }

public void setNewsTitle(String newsTitle){
    mNewsTitle = newsTitle;
 }

public String getNewsLink(){
    return mNewsLink;
 }

public void setNewsLink(String newsLink){
    mNewsTitle = newsLink;
 }
}

NewsFeeds class:

public class NewsFeeds extends ListActivity {

private ArrayList<Feeds> mFeedDB = new ArrayList<Feeds>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_news_feeds);

    HtmlParser htmlThread = new HtmlParser();
    htmlThread.execute();


} // end on create

public class HtmlParser extends AsyncTask<Void, Integer, ArrayList<Feeds>> {

    private static final int NETWORK_NO_ERROR = -1;
    private static final int NETWORK_HOST_UNREACHABLE = 1;
    private static final int NETWORK_NO_ACCESS_TO_INTERNET = 2;
    private static final int NETWORK_TIME_OUT = 3;

    Integer serverError = NETWORK_NO_ERROR;

    ProgressDialog dialog;

    protected void onPreExecute() {
        // example of setting up something
        dialog = new ProgressDialog(NewsFeeds.this);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setMessage("Retrieving News Feeds");
        dialog.show();
    } // end onPreExecute

    @Override
    protected ArrayList<Feeds> doInBackground(Void... params) {
        try {
            // need http protocol
            Document doc = Jsoup.connect("http://baseball-potsdam.de/news")
                    .get();

            // get news feed titles
            Elements newsFeed = doc.getElementsByClass("gdlr-blog-title");

            // get all links
            Elements links = newsFeed.select("a[href]");


            for (Element link : links) {
                // populate ArrayList with news titles and links
                mFeedDB.add(new Feeds(link.text(), link.attr("href")));
            }

            return mFeedDB;

            // } catch (IOException e) {
            // e.printStackTrace();
        } catch (ConnectException e) {
            serverError = NETWORK_NO_ACCESS_TO_INTERNET;
            return null;
        } catch (UnknownHostException e) {
            serverError = NETWORK_HOST_UNREACHABLE;
            return null;
        } catch (SocketTimeoutException e) {
            serverError = NETWORK_TIME_OUT;
            return null;
        } catch (IOException e) {
            e.printStackTrace();
        } // end try catch
        return null;

    } // end doInBackground

    protected void onProgressUpdate(Integer... progress) {

    } // end onProgressUpdate

    protected void onPostExecute(ArrayList<Feeds> result) {
        if (result != null) {

            ListView listview = (ListView) findViewById(R.id.list_view_news_feeds);
            listview.setAdapter(new ArrayAdapter<Feeds>(NewsFeeds.this, android.R.layout.simple_list_item_1 , mFeedDB));


            if (dialog.isShowing()) {
                dialog.dismiss();
            } // end if
        } else {
            switch (serverError) {
            case NETWORK_NO_ERROR:
                Toast.makeText(NewsFeeds.this,
                        "Probably, invalid response from server",
                        Toast.LENGTH_LONG).show();
                break;
            case NETWORK_NO_ACCESS_TO_INTERNET:
                // You can customize error message (or behavior) for
                // different type of error
            case NETWORK_TIME_OUT:
            case NETWORK_HOST_UNREACHABLE:
                Toast.makeText(NewsFeeds.this, "Error in Connection",
                        Toast.LENGTH_LONG).show();
                break;
            }
        } // end if else
        } // end onPostExecute
    } // end HtmlParser class
} // end NewsFeeds

activity_news_feeds.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >   

    <ListView
        android:id="@+id/list_view_news_feeds"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:dividerHeight="0.1dp"
        android:divider="#0000CC"
    />
</LinearLayout>

NewsManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.kylehopeman.android.porcupinesnews"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <uses-permission android:name="android.permission.INTERNET" />"

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.kylehopeman.android.porcupinesnews.MainMenu"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name="com.kylehopeman.android.porcupinesnews.NewsFeeds"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.kylehopeman.android.porcupinesnews.NewsFeeds" />
             <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>      
    </application>
</manifest>

如果您在使用 ListView 时遇到错误,它似乎只在 postExecute 块中被实例化。是否可以在 onCreate() 中实例化它并在声明 mFeedDB 的位置声明它?

将我的 NewsFeeds.java 文件中的第一行更改为:

public class NewsFeeds extends ListActivity

至:

public class NewsFeeds extends Activity

错误消失了,应用程序按照我想要的方式编译和运行。