安卓开发之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

          <string-array name="city">
       <item name="bj">北京</item>
       <item name="sh">上海</item>
       <item name="sz">深圳</item>
       <item name="gz">广州</item>
       <item name="wh">武汉</item>
       <item name="xa">西安</item>
       <item name="hb">嘻嘻哈哈</item>
   </string-array>

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

        <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:orientation="horizontal" >

   <ImageView
       android:id="@+id/ivThumb"
       android:contentDescription="chenyi"
       android:layout_width="80dp"
       android:layout_height="80dp"
       android:src="@drawable/chenyi"/>
   <TextView
       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" />

</LinearLayout>

   4.在主布局中添加listview

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical" >

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

</LinearLayout>

   5.定义listview,集合,GeneralAdapter,图片资源数组变量
      ListView listview;
      List<GeneralBean> 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<GeneralBean>();
         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用法举例,创建图形文字混合列表项-创新互联
新闻来源:https://www.cdcxhl.com/article24/digece.html

成都网站建设公司_创新互联,为您提供标签优化App设计网站收录自适应网站网站营销企业建站

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都网页设计公司