安卓开发之BaseAdapter用法举例,创建图形文字混合列表项-创新互联

baseAdapter的用法
   1.创建一个数组资源类GeneralBean

公司主营业务:成都网站设计、做网站、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。成都创新互联公司是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。成都创新互联公司推出宝清免费做网站回馈大家。

        package com.example.hoyin0211.entry;

public class GeneralBean {
   private int resid;
   private String name;
   @Override
   public String toString() {
      return "GeneralBean [resid=" + resid + ", name=" + name + "]";
   }
   public GeneralBean(int resid, String name) {
      super();
      this.resid = resid;
      this.name = name;
   }
   public int getResid() {
      return resid;
   }
   public void setResid(int resid) {
      this.resid = resid;
   }
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }

}

   2.定义字符串数组资源string-array

          
       北京
       上海
       深圳
       广州
       武汉
       西安
       嘻嘻哈哈
   

   3.定义列表横向布局(ImageView,TextView)

        
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:orientation="horizontal" >

          android:id="@+id/ivThumb"
       android:contentDescription="chenyi"
       android:layout_width="80dp"
       android:layout_height="80dp"
       android:src="@drawable/chenyi"/>
          android:id="@+id/tvName"
       android:layout_width="wrap_content"
       android:layout_height="80dp"
       android:layout_marginLeft="10dp"
       android:text="陈毅"
       android:textSize="20sp"
       android:gravity="center_vertical" />



   4.在主布局中添加listview

           android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical" >

          android:id="@+id/mlvTest"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:divider="#ccc"
       android:dividerHeight="2dp"/>



   5.定义listview,集合,GeneralAdapter,图片资源数组变量
      ListView listview;
      List mGeneralBeans;
      GeneralAdapter mAdapter;
      int[] resid = {R.drawable.zhude,R.drawable.....};
   6.将资源中的字符串数组转换成java中的字符串数组
      private void initData(){
         String[] names=getResources().getStringArray(R.array.city);
         mGenerals = new ArrayList();
         for(int i = 0 ; i < names.length; i++){
            GeneralBean bean = new GeneralBean(Resid[i],names[i]);
            mGenerals.add(bean);
         }
      }
   7.创建BaseAdapter适配器
      class GeneralAdapter extends BaseAdapter{
         public int getCount(){
            retuen mGenerals.size();
         }

         public GeneralBean getItem(int position){
            return mGenerals.get(position);
         }

         public long getItemId(int position){
            return position;
         }

         public View getView(int position,View convertView,ViewGroup parent){
            //拿到listviewitem布局,转换成view类型的对象
            View layout = View.inflate(MainActivity.this,R.layout.item_general,null);
            //找到p_w_picpathview
            ImageView ivThube = (ImageView) layout.findViewById(R.id.ivThumb);
            TextView tvName = (TextView) layout.findViewById(R.id.tvName);
            //获取下标为position的图片
            GeneralBean bean = mGenerals.get(position);
            //显示图片
            ivThumb.setImageResource(bean.getResid());
            //显示姓名
            tvName.setText(bean.getName());
            return layout;
         }
      }

   8.关联适配器
      listview = (ListView) findViewById(R.id.mlvTest);
      mAdapter = new GeneralAdapter();
      listview.setAdapter(mAdapter);

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


文章标题:安卓开发之BaseAdapter用法举例,创建图形文字混合列表项-创新互联
本文网址:http://myzitong.com/article/digece.html