list = (ListView) findViewById(R.id.all_teacher_list); // 生成动态数组,加入数据 ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>(); for (int i = 0; i < 20; i++) { HashMap<String, Object> map = new HashMap<String, Object>(); // map.put("ItemImage", R.drawable.checked);//图像资源的ID map.put("teachername", "幼儿园按照时间可以分为宿制幼儿园"); map.put("teacherdetail", "这是一片关于幼儿知识的文章,特点是简单易读,很实用"); map.put("image", R.drawable.blue); listItem.add(map); } // 生成适配器的Item和动态数组对应的元素 SimpleAdapter listItemAdapter = new SimpleAdapter(this, listItem,// 数据源 R.layout.all_child_edu_item,// ListItem的XML实现 // 动态数组与ImageItem对应的子项 new String[] { "teachername", "teacherdetail", "image" }, // ImageItem的XML文件里面的一个ImageView,两个TextView ID new int[] { R.id.all_child_edu_name, R.id.all_child_edu_detail, R.id.all_child_edu_image }); // 添加并且显示 list.setAdapter(listItemAdapter);
如果需要将网络或文件中的图片放到listview中,需要先将图片转换为bitmap格式,然后重写setViewBinder()方法
annolistItemAdapter.setViewBinder(new ViewBinder() { public boolean setViewValue(View view, Object data, String textRepresentation) { // 判断是否为我们要处理的对象 if (view instanceof ImageView && data instanceof Bitmap) { ImageView iv = (ImageView) view; iv.setImageBitmap((Bitmap) data); return true; } else return false; } });
list.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub //点击第arg2个item的监听事件 } });
list.setOnScrollListener(new OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { // TODO Auto-generated method stub if (last == count && scrollState == this.SCROLL_STATE_IDLE) { updatevideo(); // Toast.makeText(AllSchoolNewsActivity.this, "下拉刷新", // Toast.LENGTH_LONG).show(); } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { // TODO Auto-generated method stub last = firstVisibleItem + visibleItemCount; // 减1是因为上面加了个addFooterView } });
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/steveyg/article/details/47085381