嗨我有一個viewholder +基地適配器,但它不是工作BaseAdapter + ViewHolder不工作
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder;
// get the selected entry
AI entry = (AI) mListAppInfo.get(position);
// inflate new layout if null
if(v == null) {
holder = new ViewHolder();
v = inflater.inflate(R.layout.list, null);
holder.position = position;
holder.title = (TextView)v.findViewById(R.id.tvName);
holder.icon = (ImageView)v.findViewById(R.id.ivIcon);
holder.iconimg = entry.icon;
new ThumbnailTask(position, holder)
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, null);
v.setTag(holder);
}else{
holder = (ViewHolder) v.getTag();
}
holder.title.setText(entry.title);
// return view
return v;
}
中列表中的項目不顯示任何東西我究竟做錯了什麼?
編輯這裏是全班
public class AIAdapter extends BaseAdapter {
private Context mContext;
private List<?> mListAppInfo;
private LayoutInflater inflater;
public AIAdapter(Context c, List<?> list, PackageManager pm) {
mContext = c;
mListAppInfo = list;
inflater = LayoutInflater.from(mContext);
}
@Override
public int getCount() {
return mListAppInfo.size();
}
@Override
public Object getItem(int position) {
return mListAppInfo.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@SuppressWarnings("null")
@Override
public View getView(int position, View v, ViewGroup parent) {
ViewHolder holder;
// get the selected entry
AI entry = (AI) mListAppInfo.get(position);
// inflate new layout if null
if(v == null) {
holder = new ViewHolder();
v = inflater.inflate(R.layout.list, null);
holder.position = position;
holder.title = (TextView)v.findViewById(R.id.tvName);
holder.icon = (ImageView)v.findViewById(R.id.ivIcon);
holder.iconimg = entry.icon;
new ThumbnailTask(position, holder)
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, null);
v.setTag(holder);
}else{
holder = (ViewHolder) v.getTag();
}
holder.title.setText(entry.title);
// return view
return v;
}
@SuppressWarnings("rawtypes")
private static class ThumbnailTask extends AsyncTask{
private int mPosition;
private ViewHolder mHolder;
private Rect mOldBounds = new Rect();
public ThumbnailTask(int position, ViewHolder holder) {
mPosition = position;
mHolder = holder;
}
@Override
protected Drawable doInBackground(Object... arg0) {
Drawable icon = mHolder.iconimg;
int width = 64;
int height = 64;
final int iconWidth = icon.getIntrinsicWidth();
final int iconHeight = icon.getIntrinsicHeight();
if (icon instanceof PaintDrawable) {
PaintDrawable painter = (PaintDrawable) icon;
painter.setIntrinsicWidth(width);
painter.setIntrinsicHeight(height);
}
if (width > 0 && height > 0 && (width < iconWidth || height < iconHeight)) {
final float ratio = (float) iconWidth/iconHeight;
if (iconWidth > iconHeight) {
height = (int) (width/ratio);
} else if (iconHeight > iconWidth) {
width = (int) (height * ratio);
}
final Bitmap.Config c =
icon.getOpacity() != PixelFormat.OPAQUE ?
Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
final Bitmap thumb = Bitmap.createBitmap(width, height, c);
final Canvas canvas = new Canvas(thumb);
canvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG, 0));
mOldBounds.set(icon.getBounds());
icon.setBounds(0, 0, width, height);
icon.draw(canvas);
icon.setBounds(mOldBounds);
icon = new BitmapDrawable(thumb);
}
return icon;
}
protected void onPostExecute(Drawable bitmap) {
if (mHolder.position == mPosition) {
mHolder.icon.setImageDrawable(bitmap);
}
}
}
}
class ViewHolder{
public int position;
public TextView title;
public ImageView icon;
public Drawable iconimg;
}
我不知道我做錯了。所有的entry.title和entry.icon都有數據。雖然沒有顯示在列表中。佈局列表中有一個imageview和textview。這工作沒有視圖的,但是一旦我添加視圖它不是
檢查幾件事1.你的數據源中有項目2.它進入你的getView()3.嘗試設置你的文本視圖的顏色 –
「entry.title」是否返回任何內容?你的調試器顯示你什麼? – Sam
此外,不相關,但如果每一行有不同的圖標,那麼你的縮略圖任務應該超出你的if-else塊 –