Android studio里不能运行,代码完全一样的啊
package com.apress.gerber.myapplication;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
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 ActionBarActivity {
private ListView listView;
private ArrayAdapter<String>arr_adapter;
private SimpleAdapter simp_adapter;
private List<Map<String,Object>>dataList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//setContentView(R.layout.item);
listView= (ListView) findViewById(R.id.listView);
//1、新建一个数据适配器
//ArrayAdapter(上下文,当前ListView加载的每一个列表项所对应的布局文件,数据源)
//SimpleAdapter(上下文,一个Map所组成的List集合,列表项的布局文件ID,Map中的键名,绑定数据视图中的ID·与from成对应关系)
//2、适配器加载数据源
String[]arr_data={"慕课网1","慕课网2","慕课网3","慕课网4"};
dataList = new ArrayList<>();
arr_adapter=new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,arr_data);
simp_adapter=new SimpleAdapter(this,getData(),R.layout.item,new String[]{"pic","text"}, new int[]{R.id.pic,R.id.text});
// listView.setAdapter(simp_adapter);
//3、视图(ListView)加载适配器
listView.setAdapter(simp_adapter);
}
private List<Map<String,Object>> getData()
{
for (int i=0;i<20;i++)
{
Map<String,Object>map=new HashMap<>();
map.put("pic",R.drawable.ic_launcher);
map.put("text","慕课网"+i);
dataList.add(map);
}
return dataList;
}
}