Android listview和adapter
ListView 和 Adapter的关系: 就是将ListView绑定的界面中的数据与adapter相适配
MainActivity.java
package com.example.listviewtest;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.widget.ListView;import java.util.ArrayList;
import java.util.List;public class MainActivity extends AppCompatActivity {private Bean bean;private List<Bean> data = new ArrayList<>();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);for (int i = 0; i <100 ; i++) {bean = new Bean();bean.setName("zjx"+i);data.add(bean);}ListView listView = findViewById(R.id.lv);listView.setAdapter(new MyAdapter(data,this));}
}
MyAdapter.java
package com.example.listviewtest;
/**todo author:张建新*todo @since :2021/12/13/0013*todo 作用:*/import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;import java.util.ArrayList;
import java.util.List;public class MyAdapter extends BaseAdapter {private List<Bean> data = new ArrayList<>();private Context context;public MyAdapter(List<Bean> data, Context context) {this.data = data;this.context = context;}@Overridepublic int getCount() {return data.size();}@Overridepublic Object getItem(int position) {return position;}@Overridepublic long getItemId(int position) {return 0;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {if (convertView==null){convertView = LayoutInflater.from(context).inflate(R.layout.list_view,parent,false);}TextView textView = convertView.findViewById(R.id.tv);textView.setText(data.get(position).getName());return convertView;}
}
一个固定的写java的模式 javaBean
Bean.java
package com.example.listviewtest;
/**todo author:张建新*todo @since :2021/12/13/0013*todo 作用:*/public class Bean {public String getName() {return name;}public void setName(String name) {this.name = name;}private String name ;
}
list_view.xml
<?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="match_parent"><TextViewandroid:id="@+id/tv"android:textSize="30dp"android:layout_width="wrap_content"android:layout_height="wrap_content"/></LinearLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><ListViewandroid:id="@+id/lv"android:layout_width="wrap_content"android:layout_height="wrap_content"/></LinearLayout>
Android listview和adapter相关推荐
- Android listview与adapter用法
2019独角兽企业重金招聘Python工程师标准>>> 一个ListView通常有两个职责. (1)将数据填充到布局. (2)处理用户的选择点击等操作. 第一点很好理解,ListVi ...
- [Android] ListView关于adapter多种view设置
使用的关键点是在adapter覆盖两个方法 public int getItemViewType(int position) public int getViewTypeCount() 其它的可另go ...
- Android ListView重写Adapter
在很多项目中都会用到ListView,但基本都需要进行重写Adapter,但每次都是会找好半天,写在这儿方便以后copy~~ xml定义一个ListView: <LinearLayout xml ...
- [Android]ListView的Adapter.getView()方法中延迟加载图片的优化
以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/4139998.html 举个例子吧,以好友列表为例 ListVi ...
- Android ListView 和 ***Adapter 从本地/网络获取歌曲列表
本文内容 环境 项目结构 测试数据 演示 1:SimpleAdapter 演示 2:BaseAdapter 演示 3:CustomLazyList 演示 4:CustomLazyCompleteLis ...
- listview与adapter用法
Android listview与adapter用法 listview与adapter用法 博客分类: android 一个ListView通常有两个职责. (1)将数据填充到布局. (2)处理用户的 ...
- 带有自定义适配器示例教程的Android ListView
In this tutorial we'll use a CustomAdapter that populates the custom rows of the Android ListView wi ...
- Android ListView (多个adapter 说明)
最近维护项目,项目里面使用的都是ListView 这里也对ListView 回顾一下 以下 demo 地址 ListView 在Android 里面用来实现列表的, 在xml 中创建ListView ...
- Android控件——ListView之Adapter提供数据(其二)
2019独角兽企业重金招聘Python工程师标准>>> 上一节中一些列表集合数据到手机屏幕时,通常采用ListView组件+ArrayAdapter. 虽然它能为我们提供展示数据列表 ...
最新文章
- kubernetes学习笔记之七: Ingress-nginx 部署使用
- 七十四、SpringBoot 的数据缓存cache(一)
- nand ubi -1 nand基础
- 开学了,也要开始找工作了
- pycharm中无法import已经安装的ros中的库
- leetcode面试题 16.21. 交换和(二分查找)
- [vue] v-if和v-for的优先级是什么?如果这两个同时出现时,那应该怎么优化才能得到更好的性能?
- 【HDU - 1540】 Tunnel Warfare (线段树进阶操作 区间合并+ 单点更新+ 最长覆盖区间查询 )
- java嵌套对象,java – 从嵌套流中收集一组对象
- docker 运行jar exit_使用Dockerfile为SpringBoot应用构建Docker镜像
- ubuntu dpkg
- mysql binlog 过期_mysql清理过期binlog堵塞数据库
- js或css指定元素点击时内容不可被选中
- oracle执行计划更新,请教update和delete的执行计划
- c语言编程怎么记,新手如何学习c语言
- chrome强烈推荐安装Octotree插件
- React 时间控件
- 安装打印机驱动步骤(win10)
- 2021年氯化工艺考试题库及氯化工艺考试试卷
- 51单片机无法烧录程序的N种解决办法
热门文章
- 移动端踩坑记---------ios下输入法遮挡input
- C/C++面向对象编程之多态
- 学习使用 C API 调用 ImageMagick
- 硬件速攻-ST7735 0.96LCD屏幕
- hackthebox-sauna (域渗透/bloodhound使用/mimikatz使用/secretsdump.py、psexec使用)
- Swing 嵌入Html
- ContextMenustrip 控件
- bluetooth mesh provisioning PDU学习笔记
- Python实现IMAP协议接收并解析邮件内容
- 前端工程化-VSCode插件集成脚手架和组件库