基本上我创建了一个有效的RSS。我使用DOMParsing和自定义适配器进行列表视图外观。但是现在我决定我想在一个表格中代表一个特定类别的rss feed(即突发新闻,教育新品等)。这是我的代码。
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 Exception,用于更新检索到的数据列表(标题,ulr,链接)。显然它们不是,但我现在无法思考如何修复它。
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)
提前感谢您,并为长代码感到抱歉。
答案 0 :(得分:1)
好的,所以我想我知道你的问题是什么。
在fragment_a.xml
内,您将ListView
声明为:
<ListView
android:id="@android:id/list"
// ... rest of properties
</ListView>
第一个问题是你在说:
android:id="@android:id/list"
哪个会使用ID&#34;列表&#34;从包android而不是你自己的资源。这就是为什么它在您的代码中无法识别。此外,您尝试访问ID为list_item
的元素,该元素甚至不会出现在您的布局中,因此NullPointerException
因为listView
为null
而获得ListView
。
因此,在您获得listView = (ListView)getView().findViewById(R.id.list_item);
的代码中,您应该更改:
listView = (ListView)getView().findViewById(android.R.id.list);
要:
{{1}}