模拟新浪微博随便看看
1. 训练目标
1) 掌握ListView 控件的使用
2) 理解Adapter 的作用并掌握自定义FruitAdapter 的使用方式
2.开发步骤
1)自定义适配器MyAdapter
package
cn.edu.bzu.adapter;
import
java.util.List;
import
cn.edu.bzu.entity.Information;
import
cn.edu.bzu.weibo_looking.R;
import
android.content.Context;
import
android.view.LayoutInflater;
import
android.view.View;
import
android.view.ViewGroup;
import
android.widget.BaseAdapter;
import
android.widget.ImageView;
import
android.widget.TextView;
/**
* 自定义Adapter实现listView的显示
* @author monster
* @date : 2015-04-28
*/
public
class
MyAdapter
extends
BaseAdapter {
private
List<Information> list;
private
Context mContext;
private
LayoutInflater mInflater;
//定义构造方法
public
MyAdapter(List<Information> list, Context mContext) {
super
();
this
.list = list;
this
.mContext = mContext;
}
@Override
public
int
getCount() {
return
list.size();
}
@Override
public
Object getItem(
int
position) {
return
list.get(position);
}
@Override
public
long
getItemId(
int
position) {
return
position;
}
@Override
public
View getView(
int
position, View convertView, ViewGroup parent) {
ViewHolder holder=
null
;
if
(convertView==
null
){
convertView=mInflater.from(mContext).inflate(R.layout.listview_item,
null
);
//布局转换成视图
holder=
new
ViewHolder();
//实例化控件
holder.Image=(ImageView)convertView.findViewById(R.id.Iv);
holder.Name=(TextView)convertView.findViewById(R.id.Name);
holder.Time=(TextView)convertView.findViewById(R.id.Time);
holder.Contents=(TextView)convertView.findViewById(R.id.Contents);
convertView.setTag(holder);
//设置setTag使得程序得以优化
}
else
{
holder=(ViewHolder) convertView.getTag();
}
//给控件赋值
holder.Image.setBackgroundResource(list.get(position).getImgId());
holder.Name.setText(list.get(position).getName());
holder.Time.setText(list.get(position).getDate());
holder.Contents.setText(list.get(position).getContent());
return
convertView;
}
class
ViewHolder{
ImageView Image;
TextView Name;
TextView Time;
TextView Contents;
}
}
2)MainActivity的编写
package
cn.edu.bzu.weibo_looking;
import
java.util.ArrayList;
import
java.util.List;
import
cn.edu.bzu.adapter.MyAdapter;
import
cn.edu.bzu.entity.Information;
import
android.os.Bundle;
import
android.widget.ListView;
import
android.app.Activity;
public
class
MainActivity
extends
Activity {
private
ListView listView;
private
MyAdapter adapter;
private
List<Information> list;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setData();
//数据源
initView();
}
private
void
initView() {
// 初始化控件
listView=(ListView) findViewById(R.id.Lv);
listView.setAdapter(adapter);
//绑定适配器
}
private
void
setData() {
// 设置数据源
list=
new
ArrayList<Information>();
list.add(
new
Information(R.drawable.img1,
"小白"
,
"2015-04-06"
,
"Lalala德玛西亚"
));
list.add(
new
Information(R.drawable.img2,
"小明"
,
"2015-05-06"
,
"今天上班好累"
));
list.add(
new
Information(R.drawable.img3,
"小红"
,
"2015-05-06"
,
"交个朋友吧"
));
list.add(
new
Information(R.drawable.img4,
"小g"
,
"2015-05-06"
,
"快期中考试了,看来得好好学习了!"
));
list.add(
new
Information(R.drawable.img5,
"大白"
,
"2015-05-06"
,
"妹子,约嘛~"
));
list.add(
new
Information(R.drawable.img6,
"小Q"
,
"2015-05-06"
,
"啦啦啦"
));
list.add(
new
Information(R.drawable.img7,
"涂涂"
,
"2015-05-06"
,
"好想吃炸小丸子哦"
));
list.add(
new
Information(R.drawable.img8,
"小丸子"
,
"2015-05-06"
,
"为什么要吃我"
));
adapter=
new
MyAdapter(list, MainActivity.
this
);
}
}