android开发时为啥ViewHolder cannot be resolved to a type 急
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android开发时为啥ViewHolder cannot be resolved to a type 急相关的知识,希望对你有一定的参考价值。
参考技术A 这明显是找不到这个类,或这个类的声明的范围不够大,你使用的地方引用不到 参考技术B 没有定义ViewHolder这个类!这明显是找不到这个类,或这个类的声明的范围不够大,你使用的地方引用不到
public View getView(final int position, View convertView, ViewGroup parent)
VideoItem videoitem = null;
View view = null;
if (convertView == null)
videoitem = new VideoItem();
view = inflater.inflate(R.layout.player_videolist_item, null);
videoitem.videoname = (TextView) view.findViewById(R.id.videoname);
videoitem.nameTextView = (TextView) view
.findViewById(R.id.titleName);
videoitem.title_layout = (LinearLayout) view
.findViewById(R.id.title_layout);
videoitem.content_layout = (LinearLayout) view
.findViewById(R.id.content_layout);
view.setTag(videoitem);
else
view = convertView;
Object tag = view.getTag();
if (tag instanceof VideoItem)
videoitem = (VideoItem) view.getTag();
Object obj = partandVideos.get(position);
if (obj instanceof String)
String partname = (String) obj;
videoitem.nameTextView.setText(partname);
videoitem.nameTextView.setTextColor(normalTextColor);
videoitem.content_layout.setVisibility(View.GONE);
videoitem.title_layout.setVisibility(View.VISIBLE);
else
Video video = (Video) obj;
videoitem.title_layout.setVisibility(View.GONE);
videoitem.content_layout.setVisibility(View.VISIBLE);
videoitem.videoname.setText(video.getVideoName());
String demotype = video.getDemotype();
if (position == currPostion)
videoitem.videoname.setTextColor(currTextColor);
else
videoitem.videoname.setTextColor(normalTextColor);
if (!PageExtra.isBuy() && Constants.VIDEO_CHARGE.equals(demotype))
videoitem.videoname.setTextColor(main_videolist_no);
return view;
参考技术C 没有定义ViewHolder这个类!本回答被提问者采纳
Android简单开发之 通用Adapter ViewHolder
我们寻常使用Adapter的方式
public class BusbaseSearchApadter extends SimpleBaseApadter { private List<Busbase> busbases = new ArrayList<Busbase>(); public Context context; public void setList(List<Busbase> busbases) { this.busbases = busbases; } public BusbaseSearchApadter(Context context) { super(context); this.context = context; } @Override public int getCount() { return busbases.size(); } @Override public Object getItem(int position) { return busbases.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { Busbase busbase = busbases.get(position); ViewHolder holder = null; if(convertView == null){ holder = new ViewHolder(); convertView = LayoutInflater.from(context).inflate(R.layout.hx_item_search_bus, parent, false); holder.hx_item_bus_search_image = (ImageView)convertView.findViewById(R.id.hx_item_bus_search_image); holder.hx_item_bus_search_name = (TextView)convertView.findViewById(R.id.hx_item_bus_search_name); holder.hx_item_bus_search_mobile = (TextView)convertView.findViewById(R.id.hx_item_bus_search_mobile); convertView.setTag(holder); }else{ holder = (ViewHolder)convertView .getTag(); } bitmapUtils.display(holder.hx_item_bus_search_image, busbase.getShopLogo()); holder.hx_item_bus_search_name.setText(busbase.name); holder.hx_item_bus_search_mobile.setText(busbase.mobile); return convertView; } class ViewHolder{ ImageView hx_item_bus_search_image; TextView hx_item_bus_search_name; TextView hx_item_bus_search_mobile; }
能够看出当中 实用的代码仅仅有仅仅有getView 和ViewHolder 可是其它的代码又不得不写,太痛苦 了不得不反复的写无数adapter 和ViewHoder.
我们能够写一个通用的ViewHolder
public class ViewHolder { private final SparseArray<View> mViews; private int mPosition; private View mConvertView; protected BitmapUtils bitmapUtils; private ViewHolder(Context context, ViewGroup parent, int layoutId, int position) { bitmapUtils = new BitmapUtils(context); bitmapUtils.configDefaultLoadingImage(R.drawable.hx_loodding); bitmapUtils.configDefaultLoadFailedImage(R.drawable.hx_loodding); bitmapUtils.configDefaultBitmapConfig(Bitmap.Config.RGB_565); this.mPosition = position; this.mViews = new SparseArray<View>(); mConvertView = LayoutInflater.from(context).inflate(layoutId, parent, false); // setTag mConvertView.setTag(this); } /** * 拿到一个ViewHolder对象 * * @param context * @param convertView * @param parent * @param layoutId * @param position * @return */ public static ViewHolder get(Context context, View convertView, ViewGroup parent, int layoutId, int position) { if (convertView == null) { return new ViewHolder(context, parent, layoutId, position); } return (ViewHolder) convertView.getTag(); } public View getConvertView() { return mConvertView; } /** * 通过控件的Id获取对于的控件,假设没有则增加views * * @param viewId * @return */ public <T extends View> T getView(int viewId) { View view = mViews.get(viewId); if (view == null) { view = mConvertView.findViewById(viewId); mViews.put(viewId, view); } return (T) view; } /** * 为TextView设置字符串 * * @param viewId * @param text * @return */ public ViewHolder setText(int viewId, String text) { TextView view = getView(viewId); view.setText(text); return this; } /** * 为ImageView设置图片 * * @param viewId * @param drawableId * @return */ public ViewHolder setImageResource(int viewId, int drawableId) { ImageView view = getView(viewId); view.setImageResource(drawableId); return this; } /** * 为ImageView设置图片 * * @param viewId * @param drawableId * @return */ public ViewHolder setImageBitmap(int viewId, Bitmap bm) { ImageView view = getView(viewId); view.setImageBitmap(bm); return this; } /** * 为ImageView设置图片 * * @param viewId * @param drawableId * @return */ public ViewHolder setImageByUrl(int viewId, String url) { bitmapUtils.display((ImageView) getView(viewId), url); return this; } public int getPosition() { return mPosition; }
使用一个SparseArray将View存起来
如今Adapter这样写
public abstract class AbstuctBaseApadter<T> extends SimpleBaseApadter { protected LayoutInflater mInflater; protected Context mContext; protected List<T> mDatas; protected int mItemLayoutId; public AbstuctBaseApadter(Context context) { super(context); } public AbstuctBaseApadter(Context context, List<T> mDatas, int itemLayoutId) { this(context); this.mContext = context; this.mInflater = LayoutInflater.from(mContext); this.mDatas = mDatas; this.mItemLayoutId = itemLayoutId; } public void setList(List<T> mDatas){ this.mDatas = mDatas; } @Override public int getCount() { return mDatas.size(); } @Override public T getItem(int position) { return mDatas.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { final ViewHolder viewHolder = getViewHolder(position, convertView, parent); convert(viewHolder, getItem(position)); return viewHolder.getConvertView(); } public abstract void convert(ViewHolder helper, T item); private ViewHolder getViewHolder(int position, View convertView, ViewGroup parent) { return ViewHolder.get(mContext, convertView, parent, mItemLayoutId, position); }
如今我们仅仅须要重写 convert方法 看activity里面怎么使用
AbstuctBaseApadter abstuctBaseApadter = new AbstuctBaseApadter<Map<String,Object>>(activity,list_map,R.layout.hx_money_item) { public void convert(ViewHolder helper, Map<String, Object> item) { helper.setText(R.id.id_item_1, item.get("time").toString()); helper.setText(R.id.id_item_3, item.get("sourcename").toString()); } };
哈哈 打工改成
以上是关于android开发时为啥ViewHolder cannot be resolved to a type 急的主要内容,如果未能解决你的问题,请参考以下文章
Android简单开发之 通用Adapter ViewHolder