BaseAdapter
BaseAdapterActivity.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.example.lenovo.textapp4; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; 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 BaseAdapterActivity extends AppCompatActivity { ListView lv_1; List<Myclass> lm; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_base_adapter); lv_1=(ListView)findViewById(R.id.lv_1); //数据集合 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.f2,"美食2","美食2的介绍")); lm.add(new Myclass(R.drawable.f3,"美食3","美食3的介绍")); lm.add(new Myclass(R.drawable.f2,"美食2","美食2的介绍")); lm.add(new Myclass(R.drawable.f3,"美食3","美食3的介绍")); lm.add(new Myclass(R.drawable.f2,"美食2","美食2的介绍")); lm.add(new Myclass(R.drawable.f3,"美食3","美食3的介绍")); //绑定 MyBaseAdapter myBaseAdapter=new MyBaseAdapter(); lv_1.setAdapter(myBaseAdapter); } //装载数据的分类 实体类POJO类,只包含get set方法 //一般只包含属性和属性的get set方法 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; } //返回适配完成的视图 //convertView可以复用的视图 @Override public View getView(int position, View convertView, ViewGroup parent) { //适配一条数据和View //1-得到数据 Myclass myclass=lm.get(position); if (convertView==null){ Log.e("TAg","适配器 getView="+position); convertView=View.inflate(BaseAdapterActivity.this, R.layout.simpleadapter_activity, null); //2-得到视图View(Layout文件).加载Layout文件。返回View //View view=View.inflate(BaseAdapterActivity.this, R.layout.simpleadapter_activity, 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); textView.setText(myclass.getContent()); //4-返回 return convertView; } } }
activity_base_adapter
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.lenovo.textapp4.BaseAdapterActivity"> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/lv_1"> </ListView> </LinearLayout>