API:setAdapter(Adapter) 设置适配器
ArrayAdapter
(1)layout:只能有一个<TextView>, 不能有布局
(2)数据源:String[ ]或List<String>
SimpleAdapter
(1)layout:可以用布局,包含多个视图
(2)构造方法:①String[ ]{Map集合的key} ②int[ ] {layout文件中视图的id,和Map集合的key对应}
监听器:AdapterView.OnItemClickListener:1)void onItemClick(AdapterView<?> parent, View view, int position, long id):position 被点击的数据在集合中的索引
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="com.hanqi.testapp2.TestActivity9"> 11 12 <ListView 13 android:layout_width="match_parent" 14 android:layout_height="match_parent" 15 android:id="@+id/lv_3"> 16 17 </ListView> 18 19 </LinearLayout>
1 package com.hanqi.testapp2; 2 3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 import android.util.Log; 6 import android.view.View; 7 import android.view.ViewGroup; 8 import android.widget.BaseAdapter; 9 import android.widget.ImageView; 10 import android.widget.ListView; 11 import android.widget.TextView; 12 13 import java.util.ArrayList; 14 import java.util.List; 15 16 public class TestActivity9 extends AppCompatActivity { 17 18 ListView lv_3; 19 List<MyClass> lm; 20 21 @Override 22 protected void onCreate(Bundle savedInstanceState) { 23 super.onCreate(savedInstanceState); 24 setContentView(R.layout.activity_test9); 25 26 lv_3=(ListView)findViewById(R.id.lv_3); 27 28 //数据集合 29 lm=new ArrayList<>(); 30 31 MyClass myClass=new MyClass(R.drawable.f1,"美食1","美食1的介绍"); 32 33 lm.add(myClass); 34 35 lm.add(new MyClass(R.drawable.f2,"美食2","美食2的介绍")); 36 lm.add(new MyClass(R.drawable.f3,"美食3","美食3的介绍")); 37 lm.add(new MyClass(R.drawable.f4,"美食4","美食4的介绍")); 38 lm.add(new MyClass(R.drawable.f5,"美食5","美食5的介绍")); 39 lm.add(new MyClass(R.drawable.f6,"美食6","美食6的介绍")); 40 lm.add(new MyClass(R.drawable.f7,"美食7","美食7的介绍")); 41 lm.add(new MyClass(R.drawable.f8,"美食8","美食8的介绍")); 42 lm.add(new MyClass(R.drawable.f9,"美食9","美食9的介绍")); 43 lm.add(new MyClass(R.drawable.f10,"美食10","美食10的介绍")); 44 45 MyBaseAdapter myBaseAdapter=new MyBaseAdapter(); 46 47 lv_3.setAdapter(myBaseAdapter); 48 } 49 50 //装载数据的类 实体类 POJO类 简单java图像 51 //一般只包含属性和属性的getter和setter方法 52 class MyClass 53 { 54 //img 55 private int img; 56 57 //name 58 private String name; 59 60 //content 61 private String content; 62 63 public int getImg() { 64 return img; 65 } 66 67 public void setImg(int img) { 68 this.img = img; 69 } 70 71 public String getName() { 72 return name; 73 } 74 75 public void setName(String name) { 76 this.name = name; 77 } 78 79 public String getContent() { 80 return content; 81 } 82 83 public void setContent(String content) { 84 this.content = content; 85 } 86 87 //默认构造方法 88 public MyClass() 89 { 90 91 } 92 //直接初始化属性的构造方法 93 public MyClass(int img,String name,String content) 94 { 95 this.img=img; 96 this.name=name; 97 this.content=content; 98 } 99 } 100 101 //内部类继承抽象类BaseAdapter实现4个方法 102 103 class MyBaseAdapter extends BaseAdapter 104 { 105 @Override 106 public int getCount() { 107 return lm.size(); 108 } 109 110 @Override 111 public Object getItem(int position) { 112 return lm.get(position); 113 } 114 115 @Override 116 public long getItemId(int position) { 117 return 0; 118 } 119 120 //返回适配完成的视图 121 //convertView 可以复用的视图 122 @Override 123 public View getView(int position, View convertView, ViewGroup parent) { 124 125 Log.e("TAG","适配器 getView"+position); 126 //适配一条数据和View 127 //1.得到数据 128 129 MyClass myClass=lm.get(position); 130 131 if (convertView==null) { 132 133 //2.得到 View 134 //加载layout文件 135 convertView= View.inflate(TestActivity9.this, R.layout.simple_adapter, null); 136 } 137 //3.把数据放到View里 138 139 ImageView imageView=(ImageView)convertView.findViewById(R.id.iv_2); 140 imageView.setImageResource(myClass.getImg()); 141 142 TextView textView=(TextView)convertView.findViewById(R.id.tv_7); 143 textView.setText(myClass.getName()); 144 145 TextView textView1=(TextView)convertView.findViewById(R.id.tv_8); 146 textView1.setText(myClass.getContent()); 147 148 149 return convertView; 150 } 151 } 152 }