今天晚上做一个依据数组中元素是否为null,来设置textView的可见性,本来想用简单的用setVisibility来实现,结果好像没有什么效果,后来去看SimpleAdapter的getView()原理,发现可以bindView方法中实现这一功能,于是有了下面RemindViewAdapter类,代码基本是在SimpleAdapter中摘抄出来的。可见平时克服恐惧心理去研究sdk中的代码的重要性。
/** * 用于RemindView里数据绑定的Adapter * * @author xzz * */ public class RemindViewAdapter extends SimpleAdapter { /** * 保存map的list,map保存数据 */ private List<? extends Map<String, ?>> mData; /** * 解析的xml资源文件id */ private int mResource; /** * RemindViewAdapter所需的key数组 */ private String[] mFrom; /** * RemindViewAdapter所需的id数组 */ private int[] mTo; /** * 解析器 */ private LayoutInflater mInflater; /** * 有更新的内容的数量,没有更新的位置置null, */ private String[] mNewTextItem; /** * 属兔绑定 */ private ViewBinder mViewBinder; /** * 构造方法,对实例域进行引用复制 * * @param context * @param data * @param resource * @param from * @param to * @param newTextItem * @see android.widget.SimpleAdapter */ public RemindViewAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to, String[] newTextItem) { super(context, data, resource, from, to); mData = data; mResource = resource; mFrom = from; mTo = to; mNewTextItem = newTextItem; mInflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); } /** * @see android.widget.Adapter#getCount() */ public int getCount() { return mData.size(); } /** * @see android.widget.Adapter#getItem(int) */ public Object getItem(int position) { return mData.get(position); } /** * @see android.widget.Adapter#getItemId(int) */ public long getItemId(int position) { return position; } /** * @see android.widget.Adapter#getView(int, View, ViewGroup) */ public View getView(int position, View convertView, ViewGroup parent) { return createViewFromResource(position, convertView, parent, mResource); } /** * * 从资源文件中创建View * * @param position * 位置 * @param convertView * @param parent * @param resource * @return * @see android.widget.SimpleAdapter#createViewFromResource */ private View createViewFromResource(int position, View convertView, ViewGroup parent, int resource) { View v; if (convertView == null) { v = mInflater.inflate(resource, parent, false); } else { v = convertView; } bindView(position, v); return v; } /** * 主要是依据newItemText数组中是否为null,来设置R.id.newtextitem的可视性 * * @param position 位置 * @param view listView中item的View * @see android.widget.SimpleAdapter#bindView */ private void bindView(int position, View view) { final Map<String, ?> dataSet = mData.get(position); if (dataSet == null) { return; } final ViewBinder binder = mViewBinder; final String[] from = mFrom; final int[] to = mTo; final int count = to.length; for (int i = 0; i < count; i++) { final View v = view.findViewById(to[i]); if (v != null) { final Object data = dataSet.get(from[i]); String text = data == null ? "" : data.toString(); boolean bound = false; if (binder != null) { bound = binder.setViewValue(v, data, text); } if (!bound) { if (v instanceof TextView) { setViewText((TextView) v, text); if (from[i].equals(key[2]) && mNewTextItem[position] == null)//在这里实现依据newItemText数组中是否为null,来设置R.id.newtextitem的可视性//当然也可以在这个方法里实现自己定制的ListView中的Item v.setVisibility(View.INVISIBLE); } else if (v instanceof ImageView) { if (data instanceof Integer) { setViewImage((ImageView) v, (Integer) data); } else { setViewImage((ImageView) v, text); } } else { throw new IllegalStateException( v.getClass().getName() + " is not a " + " view that can be bounds by this SimpleAdapter"); } } } } } }