适配器(adapter)在android中是数据和视图(View)之间的一个桥梁,通过适配器以便于数据在view视图上显示。现在主要对ArrayAdapter、SimpleAdapter、BaseAdapter进行简单的演示。
使用时具有局限性,默认情况下不支持imageview等非文本以外的内容,如果要显示的话需要重写getView方法。
private String[] itemData = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15" };
adapter = new ArrayAdapter(ArrayAdapterActiity.this, android.R.layout.simple_expandable_list_item_1, itemData);
listView.setAdapter(adapter);
package com.dimanche.demo;
import com.example.listviewwithadapter.R;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ArrayAdapterActiity extends Activity {
// 声明变量
ListView listView;
ArrayAdapter adapter;
// 声明数据源
private String[] itemData = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15" };
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_arrayadapter);
listView = (ListView) findViewById(R.id.listview);
adapter = new ArrayAdapter(ArrayAdapterActiity.this, android.R.layout.simple_expandable_list_item_1,
itemData);
listView.setAdapter(adapter);
}
}
代码已上传百度云:
链接:https://pan.baidu.com/s/1fM7Ze9lYM4HrPIkpJMQUjg
提取码:7sx5
(嘿嘿嘿,点击这里关于我哦,分享小知识):https://www.jianshu.com/p/7d19f0df5b6b