检索 XML 个提要,对其进行解析并将其放在列表视图中

Retrieve XML feed,parse it and put it on a listview

我一直在编写此脚本以检索 XML 提要并将其显示在网络视图上(成功教程 url 此处:http://developer.android.com/training/basics/network-ops/xml.html),但是,我正在尝试将提要的条目放在列表视图中。

public class ListFeed extends ListActivity {
public static final String WIFI = "Wi-Fi";
public static final String ANY = "Any";
private static final String URL = "http://whosebug.com/feeds/tag?tagnames=android&sort=newest";

// Whether there is a Wi-Fi connection.
private static boolean wifiConnected = false;
// Whether there is a mobile connection.
private static boolean mobileConnected = false;
// Whether the display should be refreshed.
public static boolean refreshDisplay = true;
public static String sPref = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_list_feed);

    new DownloadXmlTask().execute(URL);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

private InputStream downloadUrl(String urlString) throws IOException {
    URL url = new URL(urlString);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setReadTimeout(10000 /* milliseconds */);
    conn.setConnectTimeout(15000 /* milliseconds */);
    conn.setRequestMethod("GET");
    conn.setDoInput(true);
    // Starts the query
    conn.connect();
    return conn.getInputStream();
}

   // Uploads XML from whosebug.com, parses it, and combines it with
// HTML markup. Returns HTML string.
private List loadXmlFromNetwork(String urlString) throws XmlPullParserException, IOException {
    InputStream stream = null;
    // Instantiate the parser
    WhosebugXmlParser WhosebugXmlParser = new WhosebugXmlParser();
    List<WhosebugXmlParser.Entry> entries = null;
    String title = null;
    String url = null;
    String summary = null;
    Calendar rightNow = Calendar.getInstance();
    // Checks whether the user set the preference to include summary text
    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
    boolean pref = sharedPrefs.getBoolean("summaryPref", false);

    // List to store the items
    List<String> feed = new ArrayList<String>();

    try {
        stream = downloadUrl(urlString);
        entries = WhosebugXmlParser.parse(stream);
        // Makes sure that the InputStream is closed after the app is
        // finished using it.
    } finally {
        if (stream != null) {
            stream.close();
        }
    }
    for (WhosebugXmlParser.Entry entry : entries) {
        feed.add(entry.title);
    }

    return feed;
}

    // Implementation of AsyncTask used to download XML feed from whosebug.com.
private class DownloadXmlTask extends AsyncTask<String, Void, List> {
    @Override
    protected List doInBackground(String... urls) {
        try {
            return loadXmlFromNetwork(urls[0]);
        } catch (IOException e) {
            //return getResources().getString(R.string.connection_error);
            return null;
        } catch (XmlPullParserException e) {
            //return getResources().getString(R.string.xml_error);
            return null;
        }
    }

    @Override
    protected void onPostExecute(List result) {
        setContentView(R.layout.activity_list_feed);

        //pass the data to the view
        ListView listView = (ListView) findViewById(R.id.list);
        // create adapter to match data to view
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(ListFeed.this,R.layout.activity_list_feed,result);

        setListAdapter(arrayAdapter);

    }
}

}

Feed 获取是使用 AsyncTask 完成的,我想在 onPostExecute 方法的列表视图中输出。 我在 logcat.

中收到 "Your content must have a ListView whose id attribute is 'android.R.id.list'" 消息

这里是 XML 布局文件:

`<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold" >
</ListView>`

注意:替换为 TextView 会导致同样的错误 谢谢你的建议。

这是清单文件: `

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name=".WebviewTest"
        android:label="@string/title_activity_webview_test" >
    </activity>
    <activity
        android:name=".SimpleFeed"
        android:label="@string/title_activity_simple_feed" >
    </activity>
    <activity
        android:name=".ListFeed"
        android:label="@string/title_activity_list_feed" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>`

而不是android:id="@+id/list"

你需要android:id="@android:id/list"

这是 android 系统可以使用的已经生成的 ID,而不是您自己创建的具有相同名称的 ID。

请记住 XML 中的所有 id 实际上在 Java 中由 int 值表示。因此,每次您创建一个新的 id 时,都会创建一个新的 int。在上面的示例中,android:id/list 是系统的已知值,但您创建的 id/list 是特定于您的应用程序的新数字,因此在扩展 ListActivity(这是系统代码)时,系统无法知道但不能引用您生成的特定 id