自定义适配器
什么是适配器设计模式
将一个类的接口变换成客户端所期待的一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作。
BaseAdapter
Adapter接口的实现类
包含四个抽象方法:
int getCount()
View getView(int position,....)
Object getItem(int position)
Long getItemId(int position)
自定义Adapter案例
实现步骤
定义布局文件
准备数据集
创建自定义适配器
初始化ListView
小案例:
布局:
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lvStudent"
/>
item.xml布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:src="@drawable/image"/>
<TableLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:stretchColumns="*">
<TableRow
android:layout_height="0dp"
android:layout_weight="1.0">
<TextView
android:id="@+id/tvId"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:textSize="20sp"/>
<TextView
android:id="@+id/tvName"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:textSize="20sp"
/>
</TableRow>
<TableRow
android:layout_height="0dp"
android:layout_weight="1.0">
<TextView
android:id="@+id/tvSex"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:textSize="20sp"/>
<TextView
android:id="@+id/tvAge"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:textSize="20sp"/>
</TableRow>
</TableLayout>
</LinearLayout>
适配器:
package com.example.listview;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class StudentAdapter extends BaseAdapter {
private ArrayList<Student> stus;
private LayoutInflater inflater;
public StudentAdapter(Context context,ArrayList<Student> stus){
if(stus != null)
this.stus = stus;
else
this.stus = new ArrayList<Student>();
this.inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return stus.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = inflater.inflate(R.layout.item, null);
TextView tvId = (TextView)v.findViewById(R.id.tvId);
TextView tvName = (TextView)v.findViewById(R.id.tvName);
TextView tvSex = (TextView)v.findViewById(R.id.tvSex);
TextView tvAge = (TextView)v.findViewById(R.id.tvAge);
Student stu = stus.get(position);
tvId.setText(""+stu.getId());
tvName.setText(stu.getName());
tvSex.setText(stu.getSex());
tvAge.setText("" + stu.getAge());
return v;
}
}
学生类:
package com.example.listview;
public class Student {
private int id;
private String name;
private String sex;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Student(int id,String name,String sex,int age){
super();
this.id=id;
this.name=name;
this.sex=sex;
this.age=age;
}
public Student(){
super();
}
}
用来返回所有的信息:
package com.example.listview;
import java.util.ArrayList;
public class StudentBiz {
public static ArrayList<Student> getStudents(){
ArrayList<Student> stus = new ArrayList<>();
for(int i=1; i <=10; i++){
Student stu = new Student(i, "同学"+ i,"男",23);
stus.add(stu);
}
return stus;
}
}
主Activity:
package com.example.listview;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
public class MainActivity extends Activity {
private ListView lvStus;
private StudentAdapter adapter;
private void setupView(){
lvStus = (ListView) findViewById(R.id.lvStudent);
adapter = new StudentAdapter(this,StudentBiz.getStudents());
lvStus.setAdapter(adapter);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupView();
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理