gpt4 book ai didi

java - RSS 阅读器为 ListView 创建自定义适配器

转载 作者:行者123 更新时间:2023-11-30 02:22:36 24 4
gpt4 key购买 nike

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

Activity fragment :

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

最佳答案

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

初始化如下:

listdata = new ArrayList listData();

在执行 AsyncTask 之前

关于java - RSS 阅读器为 ListView 创建自定义适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28273859/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com