zoukankan      html  css  js  c++  java
  • Android BaseAdapter ListView (明星简介列表)

    1、搭建布局

     1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     android:layout_width="match_parent"
     3     android:layout_height="match_parent" >
     4 
     5     <ListView
     6         android:id="@+id/lv"
     7         android:layout_width="wrap_content"
     8         android:layout_height="match_parent" />
     9 
    10 </RelativeLayout>
    activity_main.xml
     1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     android:layout_width="match_parent"
     3     android:layout_height="match_parent" >
     4 
     5     <ImageView
     6         android:id="@+id/img"
     7         android:layout_width="wrap_content"
     8         android:layout_height="wrap_content"
     9         android:src="@drawable/ic_launcher" />
    10 
    11     <LinearLayout
    12         android:layout_width="wrap_content"
    13         android:layout_height="wrap_content"
    14         android:layout_margin="10dp"
    15         android:layout_toRightOf="@id/img"
    16         android:orientation="vertical" >
    17 
    18         <TextView
    19             android:id="@+id/name"
    20             android:layout_width="wrap_content"
    21             android:layout_height="wrap_content"
    22             android:textSize="24sp"
    23             android:text="名称" />
    24 
    25         <TextView
    26             android:id="@+id/content"
    27             android:layout_width="wrap_content"
    28             android:layout_height="wrap_content"
    29             android:layout_marginTop="10dp"
    30             android:text="简介" />
    31     </LinearLayout>
    32 
    33 </RelativeLayout>
    item_layout

    2、建立一个明星类

     1 public class SuperStar {
     2     private int img;
     3     private String name;
     4     private String content;
     5     
     6     public SuperStar(int img, String name, String content) {
     7         super();
     8         this.img = img;
     9         this.name = name;
    10         this.content = content;
    11     }
    12     @Override
    13     public String toString() {
    14         return "SuperStar [ name=" + name + ", content=" + "]";
    15     }
    16     public int getImg() {
    17         return img;
    18     }
    19     public void setImg(int img) {
    20         this.img = img;
    21     }
    22     public String getName() {
    23         return name;
    24     }
    25     public void setName(String name) {
    26         this.name = name;
    27     }
    28     public String getContent() {
    29         return content;
    30     }
    31     public void setContent(String content) {
    32         this.content = content;
    33     }
    34     
    35     
    36 }
    SuperStar.java

    3、设置适配器

     1 public class MyBaseAdapter extends BaseAdapter{
     2 
     3     private Context context;
     4     private List<SuperStar> list;
     5 
     6     public MyBaseAdapter(Context context, List<SuperStar> list) {
     7         this.context = context;
     8         this.list = list;
     9     }
    10     
    11     @Override
    12     public int getCount() {
    13         return list.size();
    14     }
    15 
    16     @Override
    17     public Object getItem(int position) {
    18         // TODO Auto-generated method stub
    19         return null;
    20     }
    21 
    22     @Override
    23     public long getItemId(int position) {
    24         // TODO Auto-generated method stub
    25         return 0;
    26     }
    27 
    28     @Override
    29     public View getView(int position, View convertView, ViewGroup parent) {
    30         ViewHolder viewHolder;
    31         if(convertView == null){
    32             convertView = View.inflate(context, R.layout.item_star, null);
    33             viewHolder = new ViewHolder(convertView);
    34             convertView.setTag(viewHolder);
    35         }else{
    36             viewHolder =  (ViewHolder) convertView.getTag();
    37         }
    38         
    39         //设置数据
    40         viewHolder.img.setImageResource(list.get(position).getImg());
    41         viewHolder.name.setText(list.get(position).getName());
    42         viewHolder.content.setText(list.get(position).getContent());
    43         
    44         return convertView;
    45     }
    46     
    47     class ViewHolder{
    48         ImageView img;
    49         TextView name;
    50         TextView content;
    51         
    52         public ViewHolder(View convertView) {
    53             img = (ImageView) convertView.findViewById(R.id.img);
    54             name = (TextView) convertView.findViewById(R.id.name);
    55             content = (TextView) convertView.findViewById(R.id.content);
    56         }
    57     }
    58 
    59 }
    MyBaseAdapter

    4、初始化数据

     1 public class MainActivity extends Activity {
     2 
     3     ListView lv;
     4 
     5     // 数据源
     6     String[] names = { "范冰冰", "杨幂", "张歆艺", "艾薇儿", "刘诗诗" };
     7     String[] contents = { "1981年9月16日生于山东青岛,华语影视女演员、歌手、制片人。毕业于上海师范大学谢晋影视艺术学院。",
     8             "中国女演员、歌手、电视剧制片人,出生于北京。毕业于北京电影学院表演系。",
     9             "中国内地女演员,出生于1981年5月29日,2005年毕业于中央戏剧学院表演系本科本。",
    10             "1984年9月27日出生于加拿大安大略省,加拿大女歌手、词曲创作者、演员。",
    11             "原名刘诗诗,中国内地影视女演员,出生于北京,毕业于北京舞蹈学院。" };
    12 
    13     int[] imgs = { R.drawable.fanbb, R.drawable.yangmi, R.drawable.zhangxinyi,
    14             R.drawable.avril, R.drawable.liushishi };
    15     
    16     List<SuperStar> list = new ArrayList<SuperStar>();
    17 
    18     @Override
    19     protected void onCreate(Bundle savedInstanceState) {
    20         super.onCreate(savedInstanceState);
    21         setContentView(R.layout.activity_main);
    22 
    23         initView();
    24         lv = (ListView) findViewById(R.id.lv);
    25 
    26         MyBaseAdapter adapter = new MyBaseAdapter(MainActivity.this, list);
    27         lv.setAdapter(adapter);
    28     }
    29 
    30     private void initView() {
    31         SuperStar fanbb = new SuperStar(R.drawable.fanbb, "范冰冰", "1981年9月16日生于山东青岛,华语影视女演员、歌手、制片人。毕业于上海师范大学谢晋影视艺术学院。");
    32         list.add(fanbb);
    33         
    34         SuperStar yangmi = new SuperStar(R.drawable.yangmi,"杨幂", "中国女演员、歌手、电视剧制片人,出生于北京。毕业于北京电影学院表演系。");
    35         list.add(yangmi);
    36         
    37         SuperStar zhangxinyi = new SuperStar(R.drawable.zhangxinyi, "张歆艺", "中国内地女演员,出生于1981年5月29日,2005年毕业于中央戏剧学院表演系本科本。");
    38         list.add(zhangxinyi);
    39         
    40         SuperStar avril = new SuperStar(R.drawable.avril, "艾薇儿", "1984年9月27日出生于加拿大安大略省,加拿大女歌手、词曲创作者、演员。");
    41         list.add(avril);
    42         
    43         SuperStar liushishi = new SuperStar(R.drawable.liushishi, "刘诗诗", "原名刘诗诗,中国内地影视女演员,出生于北京,毕业于北京舞蹈学院。");
    44         list.add(liushishi);
    45     }
    46 
    47 }
    MainActivity.java
  • 相关阅读:
    好久没有上来,发布几个日志类
    这些天很忙,写一个类似防火墙的东西在WINSOCK2 SPI上做DLL,终于把问题解决了,现提供完整C++项目下载
    Flask学习之搭建环境
    年底总结前序
    3月13日的合照一张
    我也是个张三
    读《广州的一场春梦》有感
    四期合照,纪念小杨离开
    深夜的伤感
    随感,未写先累
  • 原文地址:https://www.cnblogs.com/Claire6649/p/5968843.html
Copyright © 2011-2022 走看看