标签:
步骤:
1.ListView及ListView_Item
2.实体类的编写
3.自定义适配器的书写
4.ListView的绑定数据源与控件
代码:
package cn.edu.bzu.adapter;
import java.util.List;
import cn.edu.bzu.entity.Information;
import cn.edu.bzu.weibo.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的显示
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());
另一部分代码:
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-28","今天天气很好"));
list.add(new Information(R.drawable.img2, "卡布达", "2015-04-29", "今天又学了一门新技术"));
list.add(new Information(R.drawable.img3,"皮卡","2015-04-30","今天休息"));
adapter=new MyAdapter(list, MainActivity.this);
}
}
return convertView;
}
class ViewHolder{
ImageView Image;
TextView Name;
TextView Time;
TextView Contents;
}
}
注意:在继承BaseAdapter 需要实现getCount,getItem,getItemId,getView,以及构造方法 等方法
>这次实验让我掌握了ListView 控件的使用,加深了对技术的理解
标签:
原文地址:http://www.cnblogs.com/NB95/p/4484242.html