GridView动态加载数据情况下,选中状态的实现

dugujiujian 2012-01-12

GridView使用AdapterView动态加载数据情况下,无论是在onCreate、Onstart、OnResume方法中调用getChildCount()均为0,这说明数据并没有在Activity最初启动后立即加载为GridView的子View,那如果要把GridView实现为Tab风格的菜单,是必须要调用它的状态选中的,如果在Activity中调用的话就会抛nullpointerexception了,那要怎么操作呢?

/**
	 * 构造菜单Adapter
	 * @param menuNameArray 名称
	 * @param imageResourceArray 图片
	 * @return FootBarAdapter
	 */
	private FootBarAdapter getMenuAdapter(String[] menuNameArray,
			int[] imageResourceArray) {
		ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
		for (int i = 0; i < menuNameArray.length; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("itemImage", imageResourceArray[i]);
			map.put("itemText", menuNameArray[i]);
			data.add(map);
		}
		FootBarAdapter footbarAdapter = new FootBarAdapter(this, data,
				R.layout.item_menu, new String[] { "itemImage", "itemText" },
				new int[] { R.id.item_image, R.id.item_text });
		return footbarAdapter;
	}
	
	public class FootBarAdapter extends SimpleAdapter {
		public FootBarAdapter(Context context,
				List<? extends Map<String, ?>> data, int resource,
				String[] from, int[] to) {
			super(context, data, resource, from, to);
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v =  super.getView(position, convertView, parent);
			if (position == TOOLBAR_ITEM_PAGEHOME) {
				v.setBackgroundResource(R.drawable.bg_toolbar_item_pressed);
			}
			return v;
		}
	}

 我们继承了SimplerAdapter,然后Override getView方法,在里边进行选中状态的判断和置背景色为选中色。

摘自:http://blog.csdn.net/xjanker2/article/details/6267515

相关推荐

sgafdsg / 0评论 2011-01-24