赞
踩
sim_adapter=new SimpleAdapter(context, data, resource, from, to);
context:上下文;
data:需要展示的数据源,是map组成的list集合,每个Map对应ListView中的一行,每一个Map(键——值对)中的键名必须包含在from中所指定的键;
resource:展示的布局文件,可以自己在layout设置xml文件去设置需要展示 的样式;
from:Map中的键名;
to:绑定数据视图的ID,与from成对应的关系。
public class MainActivity extends Activity { private ListView listView; private SimpleAdapter simple_adapter; 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.listView); dataList=new ArrayList< Map<String, Object>>(); //适配器设置 simple_adapter=new SimpleAdapter(this, getData(), R.layout.item, new String[] {"pic","text"}, new int[] {R.id.pic,R.id.text}); //启用适配器 listView.setAdapter(simple_adapter); } // 设置数据源 private List<Map<String, Object>> getData() { for(int i=0;i<20;i++) { Map map=new HashMap<String, Object>(); map.put("pic", R.drawable.ic_launcher); map.put("text", "慕课网"+i); dataList.add(map); } return dataList; }; }
public class MainActivity extends ActionBarActivity implements OnItemClickListener{ private GridView grid; private SimpleAdapter sim_adapter; private List<Map<String, Object>> list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); grid = (GridView) findViewById(R.id.gridView1); list=new ArrayList(); sim_adapter = new SimpleAdapter(this, getData(), R.layout.item, new String[] { "pic", "text" }, new int[] { R.id.image, R.id.text }); grid.setAdapter(sim_adapter); grid.setOnItemClickListener(this); //加载监听器 } private List<Map<String, Object>> getData() { for (int i = 0; i < 10; i++) { Map<String, Object> map = new HashMap<String, Object>(); map.put("pic", R.drawable.ic_launcher); map.put("text", "app" + (i + 1)); list.add(map); } return list; // TODO Auto-generated method stub }
//实现监听,加载动作
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
Toast.makeText(this, "这是第"+(position+1)+"个应用", Toast.LENGTH_SHORT).show();
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。