在android中加载listview(标题、pubdata)中的Rss数据
我是android新手,我正在尝试将rss(xml数据)加载到listview中,我需要在listview中添加标题和pubdata…我正在listview中加载标题,但不知道如何加载pubdate..请提前帮助我..谢谢XS 这是一个代码,,,标题加载成功,需要在listview中加载日期..在单行上 试一试{在android中加载listview(标题、pubdata)中的Rss数据,android,xml,listview,rss,android-arrayadapter,Android,Xml,Listview,Rss,Android Arrayadapter,我是android新手,我正在尝试将rss(xml数据)加载到listview中,我需要在listview中添加标题和pubdata…我正在listview中加载标题,但不知道如何加载pubdate..请提前帮助我..谢谢XS 这是一个代码,,,标题加载成功,需要在listview中加载日期..在单行上 试一试{ URL=newurl(“rss链接”); XmlPullParserFactory工厂=XmlPullParserFactory.newInstance(); factory.setN
URL=newurl(“rss链接”);
XmlPullParserFactory工厂=XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp=factory.newPullParser();
setInput(getInputStream(url),“UTF_8”);
布尔值insideItem=false;
int eventType=xpp.getEventType();
while(eventType!=XmlPullParser.END_文档){
if(eventType==XmlPullParser.START_标记){
if(xpp.getName().equalsIgnoreCase(“项”)){
insideItem=真;
}else if(xpp.getName().equalsIgnoreCase(“title”)){
如果(内部项目)
headlines.add(xpp.nextText());//提取标题
}else if(xpp.getName().equalsIgnoreCase(“链接”)){
如果(内部项目)
links.add(xpp.nextText());//提取文章的链接
}else if(xpp.getName().equalsIgnoreCase(“pubDate”)){
如果(内部项目)
pd.add(xpp.nextText());//提取文章的发布日期
}
}else if(eventType==XmlPullParser.END_标记&&xpp.getName().equalsIgnoreCase(“项”)){
insideItem=假;
}
eventType=xpp.next();//移动到下一个元素
}
}捕获(格式错误){
e、 printStackTrace();
}catch(XMLPullParseRexE){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
//绑定数据
ArrayAdapter=新的ArrayAdapter(此,
android.R.layout.simple_可扩展_列表_项目_1,标题);
setListAdapter(适配器);
}
您需要创建一个CustomArrayAdapter,并将两个阵列都传递给该适配器
重写适配器中的getView
方法,该方法基于它为您提供位置创建一个包含两个文本视图的视图,并分别设置它们
有关customArrayAdapter的更多信息,请遵循以下步骤:
编码前,请阅读以下内容 您可以使用自定义适配器
listview.setAdapter (new CustomAdapter(ActivityName.this,R.layout.customlayout,pub,headlines));
然后
公共类CustomAdapter扩展了ArrayAdapter
{
ArrayList酒吧,标题;
更平的水;
公共CUstomAdapter(上下文上下文、int布局、ArrayList发布、ArrayList标题)
{
超级(上下文、布局、酒吧);
mInflater=LayoutInflater.from(上下文);
this.pub=pub;
this.headlines=标题;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视窗座;
if(convertView==null){
convertView=mInflater.充气(R.layout.list_行,父项,false);
holder=新的ViewHolder();
holder.tv1=(TextView)convertView.findViewById(R.id.textView1);
holder.tv2=(TextView)convertView.findViewById(R.id.textView2);
convertView.setTag(支架);
}
否则{
//视图已存在,请从视图中获取holder实例
holder=(ViewHolder)convertView.getTag();
}
holder.tv1.setText(pub.get(position.toString());
holder.tv2.setText(headlines.get(position.toString());
返回转换视图
}
静态类视窗夹{
文本视图tv1;
文本视图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>
解析工作正常吗?是的,Raghunandan,标题已成功加载到listview中,但我想在listview的单行中加载标题和pubDate?你以前做过吗?我们只能在listview中做吗textview@Viren只需创建一个新的xml。一个自定义数组适配器并复制上面的内容,假设pub和headlines是一个字符串数组列表,已经填充了@Raghunandan项,但我无法得到我需要的内容,在custome adapter中使用了两个textview,并且您提到了设置适配器,如listview.setAdapter(新的CustomAdapter(ActivityName.this,R.layout.customlayout,pub,headlines));带有不同xml的列表视图?加载rss java文件?中绑定了哪些内容?因此,我必须将其放在我上面的代码中,或者当我不明白时,我将此代码放在我上面的post代码中它不起作用,发生强制关闭错误……因此我不知道该怎么做我需要这是我的项目工作,如果可能,请给我发电子邮件,如果可以,……我非常感谢@Viren您在上面给定的代码中有2个文本视图,并将适配器设置为listview。list\u item.xml
有2个文本视图。您有一个自定义适配器设置为listview。您还想要什么?在我的第一篇博文代码中,我必须在创建list\u item或我的listview xml中调用什么?@Viren您必须设置布局的内容,该内容具有然后在cusotm适配器中展开一个包含2个文本视图的布局。这已经在我的帖子中显示。
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;
}
}
<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>