package listdemo.example.administrator.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends Activity {
private ListView listView;
private ArrayAdapter<String> arrayAdapter;
private SimpleAdapter simpleAdapter;
private List<Map<String,Object>>dataList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView= (ListView) findViewById(R.id.list_view);
/*1新建适配器new ArrayAdapt(当前上下文,当前ListView每一个列表项所对应的布局文件,数据源)android.R.layout.simple_list_item_1系统自带的,本质上是一个TextView
2 添加数据源到适配器
3 视图加载适配器*/
// String[] arr_data={"慕课网","秦时明月","天下"};
// arrayAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arr_data);
// listView.setAdapter(arrayAdapter);
/*SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
context:上下文
data:数据源List<? extends Map<String, ?>> data,一个Map所组成的List集合
每一个Map都会对应ListView列表中的一行
每一个Map(键值对)中键都要包含在from中所指定的键
resource:列表项中的布局id
from:Map中的键名
to:绑定视图中的id,与from成对应关系*/
dataList=new ArrayList<Map<String,Object>>();
simpleAdapter=new SimpleAdapter(this,getDataList(),R.layout.item_main,new String[]{"pic","txt"},new int[]{R.id.image,R.id.text});
listView.setAdapter(simpleAdapter);
}
public List<Map<String, Object>> getDataList() {
for (int i=0;i<20;i++){
Map<String,Object> map=new HashMap<String,Object>();
map.put("pic",R.mipmap.ic_launcher);
map.put("txt","慕课网"+i);
}
return dataList;
}
}