我是安卓新手,我只是想将 rss(xml 数据)加载到 listview,我需要在列表视图上添加标题和发布数据...我正在列表视图中加载标题,但不知道如何加载发布日期。请帮我同样的.....提前感谢...
这是一个代码,,,标题正在成功加载,需要在列表视图中加载日期。在单行上
尝试 {
URL url = new URL("rss link <contain title,pubDate tag> ");
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(getInputStream(url), "UTF_8");
boolean insideItem = false;
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
if (xpp.getName().equalsIgnoreCase("item")) {
insideItem = true;
} else if (xpp.getName().equalsIgnoreCase("title")) {
if (insideItem)
headlines.add(xpp.nextText()); //extract the headline
} else if (xpp.getName().equalsIgnoreCase("link")) {
if (insideItem)
links.add(xpp.nextText()); //extract the link of article
} else if (xpp.getName().equalsIgnoreCase("pubDate")) {
if (insideItem)
pd.add(xpp.nextText()); //extract the pub date of article
}
}else if(eventType==XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")){
insideItem=false;
}
eventType = xpp.next(); //move to next element
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// Binding data
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_expandable_list_item_1, headlines);
setListAdapter(adapter);
}
在编码之前,请阅读以下内容
ListView 的回收机制如何运作
http://developer.android.com/training/improving-layouts/smooth-scrolling.html
您可以使用自定义适配器
listview.setAdapter (new CustomAdapter(ActivityName.this,R.layout.customlayout,pub,headlines));
然后
public class CustomAdapter extends ArrayAdapter
{
ArrayList<String> pub,headlines;
LayoutInflater mInlfater;
public CUstomAdapter(Context context,int layout,ArrayList<String> pub,ArrayList<String> headlines)
{
super(context,layout,pub);
mInflater = LayoutInflater.from(context);
this.pub= pub;
this.headlines = headlines;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_row, parent,false);
holder = new ViewHolder();
holder.tv1 = (TextView) convertView.findViewById(R.id.textView1);
holder.tv2 = (TextView) convertView.findViewById(R.id.textView2);
convertView.setTag(holder);
}
else {
// view already exists, get the holder instance from the view
holder = (ViewHolder) convertView.getTag();
}
holder.tv1.setText(pub.get(position).toString());
holder.tv2.setText(headlines.get(position).toString());
return convertView
}
static class ViewHolder {
TextView tv1;
TextView tv2;
}
}
list_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="32dp"
android:layout_marginTop="44dp"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView1"
android:layout_alignParentRight="true"
android:layout_marginRight="64dp"
android:text="TextView" />
</RelativeLayout>
您需要创建一个 CustomArrayAdapter 并将两个数组传递给该适配器。
覆盖适配器中的getView
方法,该方法基于它为您提供位置,创建一个具有两个文本视图的视图并分别设置它们。
请按照以下步骤了解有关自定义阵列适配器的详细信息:
https://devtut.wordpress.com/2011/06/09/custom-arrayadapter-for-a-listview-android/