import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class BlogsAdapter extends BaseAdapter {
private ArrayList<Blogs> list;
private Context context;
public BlogsAdapter(ArrayList<Blogs> list, Context context) {
super();
this.list = list;
this.context = context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@SuppressLint("ViewHolder")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View inflate = View.inflate(context, R.layout.item, null);
TextView title = (TextView) inflate.findViewById(R.id.title);
TextView body = (TextView) inflate.findViewById(R.id.body);
TextView author = (TextView) inflate.findViewById(R.id.author);
TextView pubDate = (TextView) inflate.findViewById(R.id.pubDate);
TextView commentCount = (TextView) inflate.findViewById(R.id.commentCount);
title.setText(list.get(position).getTitle());
body.setText(list.get(position).getBody());
author.setText(list.get(position).getAuthorname());
pubDate.setText(list.get(position).getPubDate());
commentCount.setText(list.get(position).getCommentCount());
return inflate;
}
}
bean包
package com.example.yuekao1;
public class Blogs {
private String title;
private String body;
private String pubDate;
private String authorname;
private String commentCount;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public String getPubDate() {
return pubDate;
}
public void setPubDate(String pubDate) {
this.pubDate = pubDate;
}
public String getAuthorname() {
return authorname;
}
public void setAuthorname(String authorname) {
this.authorname = authorname;
}
public String getCommentCount() {
return commentCount;
}
public void setCommentCount(String commentCount) {
this.commentCount = commentCount;
}
}
fragment布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.yuekaolianxi1.MainActivity" >
<com.example.yuekao1.XListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
XListView的item布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.yuekaolianxi1.MainActivity" >
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#000"
android:text="title"
/>
<TextView
android:id="@+id/body"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:text="内容"
/>
<TextView
android:id="@+id/author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/body"
android:text="sss"
/>
<TextView
android:id="@+id/pubDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/body"
android:text="2016"
android:layout_toRightOf="@+id/author"
android:layout_marginLeft="20dp"
/>
<TextView
android:id="@+id/commentCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/pubDate"
android:text="23"
android:layout_marginLeft="20dp"
android:layout_below="@+id/body"
/>
</RelativeLayout>