BaseAdapter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ListView lv = findViewById(R.id.lv);
lv.setAdapter(new MyListAdapter()); // 设置 数据适配器
}

// 实现 BaseAdapter 方法
private class MyListAdapter extends BaseAdapter {

@Override
public int getCount() {
return 4;
}

@Override
public Object getItem(int position) {
return null;
}

@Override
public long getItemId(int position) {
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv;
if (convertView == null) {
tv = new TextView(MainActivity.this);
}
else {
tv = (TextView) convertView;
}
tv.setText("索引:" + position);
return tv;
}
}
}