• BaseAdapter适配器


    package com.hanqi.testapp2;
    
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.ImageView;
    import android.widget.ListView;
    import android.widget.TextView;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class TestActivity9 extends AppCompatActivity {
         ListView lv_3;
         List<MyClass> lm;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_test9);
            lv_3=(ListView)findViewById(R.id.lv_3);
            //数据集合
             lm=new ArrayList<>();
            MyClass myClass=new MyClass(R.drawable.f1,"美食1","美食1的介绍");
            lm.add(myClass);
            lm.add(new MyClass(R.drawable.f2,"美食2","美食2的介绍"));
            lm.add(new MyClass(R.drawable.f3,"美食3","美食3的介绍"));
            lm.add(new MyClass(R.drawable.f4,"美食4","美食4的介绍"));
            lm.add(new MyClass(R.drawable.f5,"美食5","美食5的介绍"));
            lm.add(new MyClass(R.drawable.f6,"美食6","美食6的介绍"));
            lm.add(new MyClass(R.drawable.f7,"美食7","美食7的介绍"));
            lm.add(new MyClass(R.drawable.f8,"美食8","美食8的介绍"));
            MyBaseAdapter myBaseAdapter=new MyBaseAdapter();
            lv_3.setAdapter(myBaseAdapter);
    
        }
        //装载数据的类 实体类 POJO类,简单java对象
        //一般只包含属性和属性的getter和setter方法
        class MyClass
        {
            //img
            private int img;
            //name
            private String name;
            //content
            private  String content;
    
            public int getImg() {
                return img;
            }
    
            public void setImg(int img) {
                this.img = img;
            }
    
            public String getName() {
                return name;
            }
    
            public void setName(String name) {
                this.name = name;
            }
    
            public String getContent() {
                return content;
            }
    
            public void setContent(String content) {
                this.content = content;
            }
            public MyClass()//默认构造方法
            {
    
            }
            //直接初始化属性的构造方法
            public MyClass(int img,String name,String content)//默认构造方法
            {
                this.img=img;
                this.name=name;
                this.content=content;
            }
        }
        //内部类继承抽象类BaseAdapter实现4个方法
        class MyBaseAdapter extends BaseAdapter
        {
            @Override
            public int getCount() {
                return lm.size();
            }
    
            @Override
            public Object getItem(int position) {
                return lm.get(position);
            }
    
            @Override
            public long getItemId(int position) {
                return 0;
            }
            //返回适配完成的视图
            @Override                              //convertView可以复用的视图
            public View getView(int position, View convertView, ViewGroup parent) {
                Log.e("TAG","适配器getView="+position);
                //适配一条数据和View
                //1 得到数据
                MyClass myClass=lm.get(position);
                if (convertView==null)
                {
                    Log.e("TAG","适配器getView"+position);
                    //2 得到View
                    //加载Layout文件
                    convertView= View.inflate(TestActivity9.this, R.layout.simple_adapter1, null);
                }
    
                //3 把数据放到View里
                ImageView imageView=(ImageView)convertView.findViewById(R.id.iv_2);
                imageView.setImageResource(myClass.getImg());
                TextView textView=(TextView)convertView.findViewById(R.id.tv_7);
                textView.setText(myClass.getName());
                TextView textView1=(TextView)convertView.findViewById(R.id.tv_8);
                textView1.setText(myClass.getContent());
    
                return convertView;
            }
        }
    }

  • 相关阅读:
    HR人员基本信息、分配信息和地址信息SQL
    iframe下面的session问题
    主流NOSQL数据库之MongoDB快速入门
    CookieThemeResolver
    data binding&&conversionservice
    二进制权限管理(转)
    Spring MVC 对locale和theme的支持
    OpenSessionInViewFilter类作用
    Spring数据库访问之ORM(三)
    Spring自定义属性编辑器PropertyEditorSupport + 使用CustomEditorConfigurer注册属性编辑器
  • 原文地址:https://www.cnblogs.com/jiang2538406936/p/5505676.html
Copyright © 2020-2023  润新知