现在的位置: 首页 > 综合 > 正文

自定义Adapter开发中没有回调getView()的问题

2013年08月15日 ⁄ 综合 ⁄ 共 1972字 ⁄ 字号 评论关闭

     适配器(Adapter)是安卓开发中经常用到的一类工具,它为我们的后端数据与前端显示提供了良好的接口,常见的适配器view有:ListView、GridView等。今天在开发自定义Adapter的时候遇到了设置setAdapter()之后没有回调自定义的getView()方法来显示想要的界面。贴出代码如下:

class ListDeviceAdapter extends BaseAdapter
	{
		ChildListView childlist = null;
		private Context mContext;
		private List<Map<String, Object>> mDeviceData; // 设备信息集合

		public ListDeviceAdapter(Context context, List<Map<String, Object>> listItems)
		{
			this.mContext = context;
			this.mDeviceData = listItems;
			System.out.println("-- ListDeviceAdapter is running...--");
		}

		@Override
		public int getCount()
		{
			return 0;//return mDeviceData.size();//首先回调getCount()方法,根据其值来决定是否回调getView()
		}

		@Override
		public Object getItem(int position)
		{
			return mDeviceData.get(position);
		}

		@Override
		public long getItemId(int position)
		{
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			System.out.println("-- getView is running...--");
			if (convertView == null)
			{
				System.out.println("-- list is running...--");
				childlist = new ChildListView();
				convertView = LayoutInflater.from(mContext).inflate(R.layout.listview_control, null);
				childlist.mDeviceType = (ImageButton) convertView.findViewById(R.id.list_btn_device_type);
				childlist.mDeviceName = (TextView) convertView.findViewById(R.id.list_text_device_name);
				childlist.mDeviceControl = (ImageButton) convertView.findViewById(R.id.list_btn_device_type);
				convertView.setTag(childlist);
			} else
			{
				childlist = (ChildListView) convertView.getTag();
			}

			childlist.mDeviceType.setBackgroundResource(R.drawable.device_light);
			childlist.mDeviceName.setText(mDeviceData.get(0).get("title").toString());
			childlist.mDeviceControl.setBackgroundResource(R.drawable.device_curtain);

			childlist.mDeviceType.setOnClickListener(new OnClickListener()
			{
				public void onClick(View v)
				{

				}
			});
			childlist.mDeviceControl.setOnClickListener(new OnClickListener()
			{
				public void onClick(View v)
				{

				}
			});

			return convertView;
		}

		class ChildListView
		{
			ImageButton mDeviceType;
			TextView mDeviceName;
			ImageButton mDeviceControl;
		}
	}

经过反复检查发现自定义Adapter先是回调的getCount()方法,起初返回的是空,Adapter判断其返回值来决定是否回调getView方法。问题就在这里,简单修改其返回值,即返回传人的List数据大小,问题得到解决。

抱歉!评论已关闭.