ListView在模拟微博看看时的应用
使用listview来完成模拟微博随便看看(--应老师要求完成作业--)
作业要求实现如下效果图:
如下为我自己的程序效果截图:
分析问题:
1.ListView及ListView_Item的使用
2.实体类
3.ListView自定义适配器的书写
4.ListView的绑定数据源与控件
5.继承:BaseAdapter
如下为我自己的代码:(自定义适配器)
1 package cn.edu.bzu.adapter; 2 3 import java.util.List; 4 5 import cn.edu.bzu.entity.Information; 6 import cn.edu.bzu.weibo_looking.R; 7 import android.content.Context; 8 import android.view.LayoutInflater; 9 import android.view.View; 10 import android.view.ViewGroup; 11 import android.widget.BaseAdapter; 12 import android.widget.ImageView; 13 import android.widget.TextView; 14 15 /** 16 * 自定义Adapter实现listView的显示 17 * @author monster 18 * @date : 2015-04-28 19 */ 20 public class MyAdapter extends BaseAdapter { 21 private List<Information> list; 22 private Context mContext; 23 private LayoutInflater mInflater; 24 //定义构造方法 25 public MyAdapter(List<Information> list, Context mContext) { 26 super(); 27 this.list = list; 28 this.mContext = mContext; 29 } 30 31 @Override 32 public int getCount() { 33 return list.size(); 34 } 35 36 @Override 37 public Object getItem(int position) { 38 return list.get(position); 39 } 40 41 @Override 42 public long getItemId(int position) { 43 return position; 44 } 45 46 @Override 47 public View getView(int position, View convertView, ViewGroup parent) { 48 ViewHolder holder=null; 49 if(convertView==null){ 50 convertView=mInflater.from(mContext).inflate(R.layout.listview_item,null); //布局转换成视图 51 holder=new ViewHolder(); 52 //实例化控件 53 holder.Image=(ImageView)convertView.findViewById(R.id.Iv); 54 holder.Name=(TextView)convertView.findViewById(R.id.Name); 55 holder.Time=(TextView)convertView.findViewById(R.id.Time); 56 holder.Contents=(TextView)convertView.findViewById(R.id.Contents); 57 convertView.setTag(holder); //设置setTag使得程序得以优化 58 }else{ 59 holder=(ViewHolder) convertView.getTag(); 60 } 61 //给控件赋值 62 holder.Image.setBackgroundResource(list.get(position).getImgId()); 63 holder.Name.setText(list.get(position).getName()); 64 holder.Time.setText(list.get(position).getDate()); 65 holder.Contents.setText(list.get(position).getContent()); 66 return convertView; 67 } 68 69 class ViewHolder{ 70 ImageView Image; 71 TextView Name; 72 TextView Time; 73 TextView Contents; 74 } 75 }
分析问题:
在上述自定义适配器MyAdapter代码中,getView方法最为重要,还需要getCount,getItem,getItemId等方法。
如下为MainActivity代码:
1 package cn.edu.bzu.weibo_looking; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import cn.edu.bzu.adapter.MyAdapter; 7 import cn.edu.bzu.entity.Information; 8 import android.os.Bundle; 9 import android.widget.ListView; 10 import android.app.Activity; 11 12 public class MainActivity extends Activity { 13 private ListView listView; 14 private MyAdapter adapter; 15 private List<Information> list; 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.activity_main); 20 setData();//数据源 21 initView(); 22 } 23 private void initView() { 24 // 初始化控件 25 listView=(ListView) findViewById(R.id.Lv); 26 listView.setAdapter(adapter); //绑定适配器 27 } 28 private void setData() { 29 // 设置数据源 30 list=new ArrayList<Information>(); 31 list.add(new Information(R.drawable.img1,"wang","2015-05-08","月落乌啼霜满天")); 32 list.add(new Information(R.drawable.img2, "dufu", "2015-05-08", "江枫渔火对愁眠")); 33 list.add(new Information(R.drawable.img3,"libai","2015-05-08","姑苏城外寒山寺")); 34 list.add(new Information(R.drawable.img4,"hades","2015-05-08","夜半钟声到客船")); 35 adapter=new MyAdapter(list, MainActivity.this); 36 } 37 }
分析问题:
1.数据源的建立与设置
2.控件使用(初始化)
3.适配器的绑定
时间: 2024-11-07 08:26:26