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