当前位置:   article > 正文

SimpleAdapter用法_simpleadapter的用法

simpleadapter的用法
sim_adapter=new SimpleAdapter(context, data, resource, from, to);
  • 1
  • 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;
  };

}
  • 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

运行结果:

在这里插入图片描述

如果需要对每个Map进行监听,则可以设置implement OnItemClickListener接口进行监听,但别忘记加载监听器!

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

}
  • 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

//实现监听,加载动作

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {

	Toast.makeText(this, "这是第"+(position+1)+"个应用", Toast.LENGTH_SHORT).show();
}
  • 1
  • 2
  • 3
  • 4
  • 5

}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/83782
推荐阅读
相关标签
  

闽ICP备14008679号