使用List<实体对象>来自定义适配器

本文介绍了一种自定义适配器的设计方法,用于扫描本地文件并显示文件名及路径。通过实现BaseAdapter,该适配器能有效地处理视频文件列表,并支持点击事件以播放选中的视频。

针对项目需要要进行自定义适配器来设计,主要是因为的是在扫描本地文件,要创建对象来接受文件的name和文件的path,如果使用SimpleAdapter显然是不满足要求的,这里传递的List集合形式不对,所以这里使用自定义适配器来完成。

 

public void initData(){

    final List<VideoModel> videoList=getVideoFile(allVideoList, Environment.getExternalStorageDirectory());
    MyAdapter myAdapter=new MyAdapter($.getContext(),R.layout.line,videoList);
    videoListView.setAdapter(myAdapter);

    videoListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        @Override
       
public void onItemClick(AdapterView<?> parent, View view, int position,long id)
        {
                String filepath = videoList.get(position).getVideoPath();
                Intent intent = new Intent();
                intent.putExtra("filepath",filepath);
                intent.setClass($.getContext(), PlayActivity.class);
                startActivity(intent);
        }
    });
}

 

 

class MyAdapter extends BaseAdapter{
    private Context context;//上下文
   
private int resourceId;//列表项的布局文件
   
private List<VideoModel> list3;

    public MyAdapter(Context context, int resourceId, List<VideoModel> list3) {
        this.context = context;
        this.resourceId = resourceId;
        this.list3 = list3;
    }

    @Override
   
public int getCount() {
        int count=0;
        if(list3!=null){
            return list3.size();
        }
        return count;
    }

    @Override
   
public Object getItem(int position) {
        return list3.get(position);
    }

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

    @Override
   
public View getView(int position, ViewconvertView, ViewGroup parent) {
        View view=null;
        if(convertView!=null){
            view=convertView;
        }else{
            view = LayoutInflater.from(context).inflate(resourceId, parent,false);
        }
        VideoHolder holder =(VideoHolder) view.getTag();
        if(holder==null){
            holder=new VideoHolder();
            holder.textView=(TextView)view.findViewById(R.id.file_name);
            holder.image=(ImageView)view.findViewById(R.id.icon);
            view.setTag(holder);
        }

        holder.textView.setText(list3.get(position).getVideoName().toString());
        holder.image.setImageResource(R.drawable.file);

        return view;
    }

    class VideoHolder{
        TextView textView;
        ImageView image;
    }
}

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.4.7</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>takeoutsystem</artifactId> <version>0.0.1-SNAPSHOT</version> <name>takeoutsystem</name> <description>takeoutsystem</description> <properties> <java.version>17</java.version> <!-- MyBatis 版本 --> <mybatis.version>3.5.15</mybatis.version> <mybatis-spring.version>3.0.3</mybatis-spring.version> <!-- 其他依赖版本 --> <jjwt.version>0.11.5</jjwt.version> <springdoc-openapi.version>2.8.5</springdoc-openapi.version> <thymeleaf-extras-springsecurity6.version>3.1.2.RELEASE</thymeleaf-extras-springsecurity6.version> </properties> <dependencyManagement> <dependencies> <!-- 强制所有模块使用此MyBatis版本 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis-spring.version}</version> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- Spring Boot 基础依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- 原生 MyBatis Starter --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>3.0.3</version> <exclusions> <!-- 排除旧版 MyBatis --> <exclusion> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> </exclusion> <exclusion> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> </exclusion> </exclusions> </dependency> <!-- 显式指定升级后的MyBatis版本 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis-spring.version}</version> </dependency> <!-- 数据库依赖 --> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>test</scope> </dependency> <!-- 安全与认证依赖 --> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt-api</artifactId> <version>${jjwt.version}</version> </dependency> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt-impl</artifactId> <version>${jjwt.version}</version> <scope>runtime</scope> </dependency> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt-jackson</artifactId> <version>${jjwt.version}</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity6</artifactId> <version>${thymeleaf-extras-springsecurity6.version}</version> </dependency> <!-- API文档 --> <dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId> <version>${springdoc-openapi.version}</version> </dependency> <!-- 测试依赖 - 修正后 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project> 如何添加对应的mybatisplus依赖,springboot的版本是3.4.7
最新发布
07-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值