RSS Reader为ListView创建自定义适配器

时间:2015-02-02 08:45:15

标签: java android xml rss

我尝试解析一些xml文件(RSS)并创建一个自定义ListView来显示图像,标题和日期。唯一的问题是,当我调用RSS下载器类来下载和解析xml文件并从中创建一个适配器时,它会给出我的NullPointer异常。我猜它没有证明解析xml文件。这是我的代码。任何帮助将不胜感激。

活动片段:

public class PublicaNewsActivity  extends Fragment implements InterfaceFunc {

    public static ArrayList<PostData> listData;
    Context mContext;
    InterfaceFunc mInterface;
    ListView mListView;
    static PostItemAdapter itemAdapter;

    public enum RSSXMLTag {
        TITLE, DATE, LINK, CONTENT, GUID, IGNORETAG;
        }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.publica_news, container, false);
        mContext   = getActivity();
        mInterface = this;
         ListView listView = (ListView) view.findViewById(R.id.postListView);
         new RssDataController().execute("http://www.jurnaltv.md/rss.xml"); 
         itemAdapter = new PostItemAdapter(mContext,
                 R.layout.publica_item, listData);
         listView.setAdapter(itemAdapter);
        return view;

    }

这是PostItemAdapter:

public class PostItemAdapter extends ArrayAdapter<PostData> {
    private Activity myContext;
    private ArrayList<PostData> datas;

    static class ViewHolder {
        TextView postTitleView;
        TextView postDateView;
        ImageView postThumbView;
    }
    public PostItemAdapter(Context context, int textViewResourceId,
            ArrayList<PostData> listData) {
        super(context, textViewResourceId, listData);
        // TODO Auto-generated constructor stub
        myContext = (Activity) context;
        datas = listData;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;

        if (convertView == null) {
                           LayoutInflater inflater = myContext.getLayoutInflater();
            convertView = inflater.inflate(R.layout.publica_item, null);

            viewHolder = new ViewHolder();
            viewHolder.postThumbView = (ImageView) convertView
                    .findViewById(R.id.postThumb);
            viewHolder.postTitleView = (TextView) convertView
                    .findViewById(R.id.postTitleLabel);
            viewHolder.postDateView = (TextView) convertView
                    .findViewById(R.id.postDateLabel);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (datas.get(position).postThumbUrl == null) {
            viewHolder.postThumbView
                    .setImageResource(R.drawable.ic_launcher);
        }

        viewHolder.postTitleView.setText(datas.get(position).postTitle);
        viewHolder.postDateView.setText(datas.get(position).postDate);

        return convertView;
    }
}

和RSSDownloader:

class RssDataController extends
    AsyncTask<String, Integer, ArrayList<PostData>> {

    private RSSXMLTag currentTag;

@Override
protected ArrayList<PostData> doInBackground(String... params) {
    // TODO Auto-generated method stub
    String urlStr = params[0];
    InputStream is = null;
    ArrayList<PostData> postDataList = new ArrayList<PostData>();
    try {

        URL url = new URL(urlStr);
        HttpURLConnection connection = (HttpURLConnection) url
                .openConnection();
        connection.setReadTimeout(10 * 1000);
        connection.setConnectTimeout(10 * 1000);
        connection.setRequestMethod("GET");
        connection.setDoInput(true);
        connection.connect();
        int response = connection.getResponseCode();
        Log.d("debug", "The response is: " + response);
        is = connection.getInputStream();

        // parse xml after getting the data
        XmlPullParserFactory factory = XmlPullParserFactory
                .newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser();
        xpp.setInput(is, null);

        int eventType = xpp.getEventType();
        PostData pdData = null;
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "EEE, DD MMM yyyy HH:mm:ss");
        while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.START_DOCUMENT) {

            } else if (eventType == XmlPullParser.START_TAG) {
                if (xpp.getName().equals("item")) {
                    pdData = new PostData();
                    currentTag = RSSXMLTag.IGNORETAG;
                } else if (xpp.getName().equals("title")) {
                    currentTag = RSSXMLTag.TITLE;
                } else if (xpp.getName().equals("link")) {
                    currentTag = RSSXMLTag.LINK;
                } else if (xpp.getName().equals("pubDate")) {
                    currentTag = RSSXMLTag.DATE;
                }
            } else if (eventType == XmlPullParser.END_TAG) {
                if (xpp.getName().equals("item")) {
                    // format the data here, otherwise format data in
                    // Adapter
                    Date postDate = dateFormat.parse(pdData.postDate);
                    pdData.postDate = dateFormat.format(postDate);
                    postDataList.add(pdData);
                } else {
                    currentTag = RSSXMLTag.IGNORETAG;
                }
            } else if (eventType == XmlPullParser.TEXT) {
                String content = xpp.getText();
                content = content.trim();
                Log.d("debug", content);
                if (pdData != null) {
                    switch (currentTag) {
                    case TITLE:
                        if (content.length() != 0) {
                            if (pdData.postTitle != null) {
                                pdData.postTitle += content;
                            } else {
                                pdData.postTitle = content;
                            }
                        }
                        break;
                    case LINK:
                        if (content.length() != 0) {
                            if (pdData.postLink != null) {
                                pdData.postLink += content;
                            } else {
                                pdData.postLink = content;
                            }
                        }
                        break;
                    case DATE:
                        if (content.length() != 0) {
                            if (pdData.postDate != null) {
                                pdData.postDate += content;
                            } else {
                                pdData.postDate = content;
                            }
                        }
                        break;
                    default:
                        break;
                    }
                }
            }

            eventType = xpp.next();
        }
        Log.v("tst", String.valueOf((postDataList.size())));
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (XmlPullParserException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return postDataList;
}
@Override
public void onPostExecute(ArrayList<PostData> result) {
    // TODO Auto-generated method stub
    for (int i = 0; i < result.size(); i++) {
        PublicaNewsActivity.listData.add(result.get(i));
    }

    PublicaNewsActivity.itemAdapter.notifyDataSetChanged();
}

}

这个类最初是在本教程中创建的: http://jmsliu.com/1390/rss-reader-app-android-tutorial-1-listview-and-arrayadapter.html

1 个答案:

答案 0 :(得分:1)

ArrayList<PostData> listData is not initiated anywhere which gives null pointer exception.

将其初始化为:

  

listdata = new ArrayList listData();

执行AsyncTask之前