public FavoriteItemAdapter( Context context, int textViewResourceId, List<Music> items, ListView listView) { super(context, textViewResourceId, items); mContext = context; mItems = items; mLayoutID = textViewResourceId; mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); mMode = MODE_PLAY; mListView = listView; asyncImageLoader = AsyncImageLoader.getInstance(); mDelete = false; }
@Override public View getView(int position, View convertView, ViewGroup parent) { View rowView = convertView; ViewCache viewCache; if (rowView == null) { rowView = mInflater.inflate(mLayoutID, null); viewCache = new ViewCache(rowView); Button btnDelete = viewCache.getButtonView(); btnDelete.setOnClickListener(this); rowView.setTag(viewCache); } else { viewCache = (ViewCache) rowView.getTag(); } viewCache.getButtonView().setTag(Integer.valueOf(position)); Music item = mItems.get(position); String title = (item.mTitle == null || item.mTitle.trim().isEmpty()) ? "no title" : item.mTitle; String artist = (item.mArtist == null || item.mArtist.trim().isEmpty()) ? "no name" : item.mArtist; TextView textView = viewCache.getTextView(); textView.setText(String.format("%s - %s", title, artist)); textView.setSelected(true); String imageUrl = TextUtils.isEmpty(item.mPicSmall) ? item.mPicBig : item.mPicSmall; if (imageUrl != null && !imageUrl.trim().isEmpty()) { imageUrl = Util.imageUrlCheck(imageUrl); ImageView imageView = viewCache.getImageView(); imageView.setTag(imageUrl); Drawable cachedImage = asyncImageLoader.loadDrawable( imageUrl, new ImageCallback() { public void imageLoaded(Drawable imageDrawable, String imageUrl) { ImageView imageViewByTag = null; View v = mListView.findViewWithTag(imageUrl); if (v instanceof ImageView) { imageViewByTag = (ImageView) v; } if (imageViewByTag != null) { if (imageDrawable != null) { imageViewByTag.setImageDrawable(imageDrawable); } else { imageViewByTag.setImageResource(R.drawable.img_none); } } } }); if (cachedImage == null) { imageView.setImageResource(R.drawable.img_none); } else { imageView.setImageDrawable(cachedImage); } } else { ImageView imageView = viewCache.getImageView(); imageView.setTag(imageUrl); imageView.setImageResource(R.drawable.img_none); } try { if (MusicApplication.getPlayer().getCurrentPosition() == position) { rowView.setBackgroundResource(R.drawable.bg_list_on); } else { rowView.setBackgroundResource(R.drawable.bg_list_off); } } catch (Exception e) { e.printStackTrace(); } if (mMode == MODE_PLAY) { viewCache.getButtonView().setVisibility(View.GONE); } else { viewCache.getButtonView().setVisibility(View.VISIBLE); viewCache.getButtonView().setEnabled(!mDelete); } return rowView; }