基本上我创建了一个单独的RSS,它可以工作。我使用了DOMParsing和一个用于列表视图外观的自定义适配器。但现在我决定把这个列表放在一个选项卡下,表示rss提要的特定类别(即突发新闻、教育新消息等)。这是我的密码。
public class FragmentA extends ListFragment {
ListView listView;
ArrayList<RowItem> items;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_a, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
listView = (ListView)getView().findViewById(R.id.list_item);
//listView.setOnItemClickListener(this);
// create the RowItem list - used for storing one news feed
items = new ArrayList<RowItem>();
// start the background task to get the news feed
//new DownloadXMLTask(items).execute("http://feeds.bbci.co.uk/news/rss.xml");
new DownloadXMLTask(items).execute("http://www.nasa.gov/rss/dyn/breaking_news.rss");
}
private class RowItem {
String title = null;
String description = null;
String url = null;
public RowItem(String title,String url,String description) {
this.title = title;
this.description = description;
this.url = url;
}
}
private class DownloadXMLTask extends AsyncTask<String, Void, String> {
ArrayList<RowItem> items = null;
public DownloadXMLTask(ArrayList<RowItem> items) {
this.items = items;
}
// local helper method
private String getNodeValue(Element entry, String tag) {
Element tagElement = (Element) entry.getElementsByTagName(tag).item(0);
return tagElement.getFirstChild().getNodeValue();
}
private String downloadAndParseXML(String url) {
try {
InputStream in = new java.net.URL(url).openStream();
// build the XML parser
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
// parse and get XML elements
Document dom = db.parse(in);
Element documentElement = dom.getDocumentElement();
// we want all XML children called 'item'
NodeList nodes = documentElement.getElementsByTagName("item");
// for each 'item' do the following
for (int i = 0; i < nodes.getLength(); i++) {
Element entry = (Element) nodes.item(i);
// get the nodes from 'item' that you need
String title = getNodeValue(entry, "title");
//String description = getNodeValue(entry, "description");
String link = getNodeValue(entry, "link");
String description = getNodeValue(entry, "description");
// add them to your list
items.add(new RowItem(title,link,description));
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
protected String doInBackground(String... urls) {
if (urls.length <= 0)
return null;
return downloadAndParseXML(urls[0]);
}
protected void onPostExecute(String result) {
updateList(items);
}
}
public void updateList(ArrayList<RowItem> items) {
CustomArrayAdapter adapter = new CustomArrayAdapter(getActivity(),
R.layout.row, items);
listView.setAdapter(adapter);
}
// class used to have custom view for the list item
private class CustomArrayAdapter extends ArrayAdapter<RowItem> {
Context context;
List<RowItem> items;
private class ViewHolder {
public TextView title;
public TextView description;
}
public CustomArrayAdapter(Context context, int resource,
List<RowItem> items) {
super(context, resource, items);
this.context = context;
this.items = items;
}
@Override
public View getView(int position, View rowView, ViewGroup parent) {
// reuse the rowView if possible - for efficiency and less memory consumption
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.row, null);
// configure view holder
ViewHolder viewHolder = new ViewHolder();
viewHolder.title = (TextView) rowView.findViewById(R.id.title);
viewHolder.description = (TextView) rowView.findViewById(R.id.desc);
rowView.setTag(viewHolder);
}
// set the view
ViewHolder holder = (ViewHolder) rowView.getTag();
RowItem item = items.get(position);
holder.title.setText(item.title);
holder.description.setText(item.description);
return rowView;
}
}
}
我在updatelist方法中得到了一个NullPointer异常,该方法用于更新检索到的数据的列表(title、ulr、link)。。显然他们不是,但我现在想不出该怎么解决。
09-21 16:07:41.310: E/AndroidRuntime(3678): FATAL EXCEPTION: main
09-21 16:07:41.310: E/AndroidRuntime(3678): java.lang.NullPointerException
09-21 16:07:41.310: E/AndroidRuntime(3678): at
com.example.tabstesting.FragmentA.updateList(FragmentA.java:159)
09-21 16:07:41.310: E/AndroidRuntime(3678):at
com.example.tabstesting.FragmentA$DownloadXMLTask.onPostExecute(FragmentA.java:152)
09-21 16:07:41.310: E/AndroidRuntime(3678): at
com.example.tabstesting.FragmentA$DownloadXMLTask.onPostExecute(FragmentA.java:1)
09-21 16:07:41.310: E/AndroidRuntime(3678): at
android.os.AsyncTask.finish(AsyncTask.java:631)
09-21 16:07:41.310: E/AndroidRuntime(3678): at
android.os.AsyncTask.access$600(AsyncTask.java:177)
提前谢谢你,很抱歉代码太长。
好的,所以我想我知道你的问题是什么。
在fragment_a.xml
中,您将ListView
声明为:
<ListView
android:id="@android:id/list"
// ... rest of properties
</ListView>
第一个问题是你在说:
android:id="@android:id/list"
这将使用来自android包的ID"列表",而不是来自您自己的资源。这就是为什么在代码中无法识别它的原因。此外,您尝试访问id为list_item
的元素,该元素甚至不存在于您的布局中,因此您得到了NullPointerException
,因为listView
是null
。
因此,在您获得ListView
的代码中,您应该更改:
listView = (ListView)getView().findViewById(R.id.list_item);
收件人:
listView = (ListView)getView().findViewById(android.R.id.list);