Android Open Source - HapiPodcastJ Icon Cursor Adapter






From Project

Back to project page HapiPodcastJ.

License

The source code is released under:

GNU General Public License

If you think the Android project HapiPodcastJ listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package info.xuluan.podcast.utils;
/*from w  w w  .j  a  va2s. c om*/
import android.widget.SimpleCursorAdapter;
import android.database.Cursor;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import info.xuluan.podcastj.R;

import java.util.HashMap;

public class IconCursorAdapter extends SimpleCursorAdapter {

  public static final int ICON_DEFAULT_ID = -1;
  
  public interface FieldHandler {
    public void setViewValue(IconCursorAdapter adapter, Cursor cursor,
        View v, int fromColumnId);
  }
  public static class TextFieldHandler implements FieldHandler {
    public void setViewValue(IconCursorAdapter adapter, Cursor cursor,
        View v, int fromColumnId) {
      //Normal text column, just display what's in the database
      String text = cursor.getString(fromColumnId);

      if (text == null) {
        text = "";
      }

      if (v instanceof TextView) {
        adapter.setViewText((TextView) v, text);
      } else if (v instanceof ImageView) {
        adapter.setViewImage((ImageView) v, text);
      }
    }
  }
  public static class IconFieldHandler implements FieldHandler {
    HashMap<Integer, Integer> mIconMap;
    public IconFieldHandler(HashMap<Integer,Integer> iconMap) {
      mIconMap = iconMap;
    }
    public void setViewValue(IconCursorAdapter adapter, Cursor cursor,
        View v, int fromColumnId) {
      //The status column gets displayed as our icon
      int status = cursor.getInt(fromColumnId);

      Integer iconI = mIconMap.get(status);
      if (iconI==null)
        iconI = mIconMap.get(ICON_DEFAULT_ID);  //look for default value in map
      int icon = (iconI!=null)?
        iconI.intValue():
        R.drawable.status_unknown;  //Use this icon when not in map and no map default.
          //This allows going back to a previous version after data has been
          //added in a new version with additional status codes.
      adapter.setViewImage2((ImageView) v, icon);
    }
  }
  public final static FieldHandler defaultTextFieldHandler = new TextFieldHandler();
  
  protected int[] mFrom2;
  protected int[] mTo2;
  protected FieldHandler[] mFieldHandlers;

  //Create a set of FieldHandlers for methods calling using the legacy constructor
  private static FieldHandler[] defaultFieldHandlers(String[] fromColumns,
      HashMap<Integer,Integer> iconMap) {
    FieldHandler[] handlers = new FieldHandler[fromColumns.length];
    for (int i=0; i<handlers.length-1; i++) {
      handlers[i] = defaultTextFieldHandler;
    }
    handlers[fromColumns.length-1] = new IconFieldHandler(iconMap);
    return handlers;
  }

  //Legacy constructor
  public IconCursorAdapter(Context context, int layout, Cursor cursor,
      String[] fromColumns, int[] to, HashMap<Integer, Integer> iconMap) {
    this(context,layout,cursor,fromColumns,to,
        defaultFieldHandlers(fromColumns,iconMap));
  }

  //Newer constructor allows custom FieldHandlers.
  //Would be better to bundle fromColumn/to/fieldHandler for each field and pass a single array
  //of those objects, but the overhead of defining that value class in Java is not worth it.
  //If this were a Scala program, that would be a one-line case class.
  public IconCursorAdapter(Context context, int layout, Cursor cursor,
      String[] fromColumns, int[] to, FieldHandler[] fieldHandlers) {
    super(context, layout, cursor, fromColumns, to);

    if (to.length<fromColumns.length){
      mTo2  = new int[fromColumns.length];
      for (int i=0; i<to.length; i++)
        mTo2[i] = to[i];
      mTo2[fromColumns.length-1] = R.id.icon;
    } else
      mTo2 = to;
    mFieldHandlers = fieldHandlers;
    if (cursor != null) {
      int i;
      int count = fromColumns.length;
      if (mFrom2 == null || mFrom2.length != count) {
        mFrom2 = new int[count];
      }
      for (i = 0; i < count; i++) {
        mFrom2[i] = cursor.getColumnIndexOrThrow(fromColumns[i]);
      }
    }
  }

  @Override
  public View newView(Context context, Cursor cursor, ViewGroup parent) {
    // Log.w("VIEW", "newView");
    View v = super.newView(context, cursor, parent);
    final int[] to = mTo2;
    final int count = mFieldHandlers.length;
    final View[] holder = new View[count];
    // Log.d("ADAPTER", "count = "+count);

    for (int i = 0; i < count; i++) {
      holder[i] = v.findViewById(to[i]);
    }
    v.setTag(holder);

    return v;

  }

  public void setViewImage2(ImageView v, int value) {
    // Log.w("VIEW", "setViewImage2");
    v.setImageResource(value);
  }

  @Override
  public void bindView(View view, Context context, Cursor cursor) {
    final View[] holder = (View[]) view.getTag();
    final int count = mFieldHandlers.length;
    final int[] from = mFrom2;

    for (int i = 0; i < count; i++) {
      mFieldHandlers[i].setViewValue(this,cursor,holder[i],from[i]);
    }

  }

}




Java Source Code List

info.xuluan.podcast.AddChannelActivity.java
info.xuluan.podcast.BackupChannelsActivity.java
info.xuluan.podcast.ChannelActivity.java
info.xuluan.podcast.ChannelDetailsActivity.java
info.xuluan.podcast.ChannelsActivity.java
info.xuluan.podcast.DownloadActivity.java
info.xuluan.podcast.EpisodeDetailsActivity.java
info.xuluan.podcast.EpisodeIcons.java
info.xuluan.podcast.EpisodesActivity.java
info.xuluan.podcast.FlingGestureDetector.java
info.xuluan.podcast.Flingable.java
info.xuluan.podcast.HapiActivity.java
info.xuluan.podcast.HapiListActivity.java
info.xuluan.podcast.HapiPreferenceActivity.java
info.xuluan.podcast.HomeActivity.java
info.xuluan.podcast.MainActivity.java
info.xuluan.podcast.PlayerActivity.java
info.xuluan.podcast.PodcastBaseActivity.java
info.xuluan.podcast.PodcastTab.java
info.xuluan.podcast.Pref.java
info.xuluan.podcast.SearchActivity.java
info.xuluan.podcast.StartupActivity.java
info.xuluan.podcast.TabsHelper.java
info.xuluan.podcast.actionbar.ActionBarHelperBase.java
info.xuluan.podcast.actionbar.ActionBarHelperHoneycomb.java
info.xuluan.podcast.actionbar.ActionBarHelperICS.java
info.xuluan.podcast.actionbar.ActionBarHelper.java
info.xuluan.podcast.actionbar.SimpleMenuItem.java
info.xuluan.podcast.actionbar.SimpleMenu.java
info.xuluan.podcast.fetcher.FeedFetcher.java
info.xuluan.podcast.fetcher.Response.java
info.xuluan.podcast.parser.FeedHandler.java
info.xuluan.podcast.parser.FeedParserHandler.java
info.xuluan.podcast.parser.FeedParserListenerInterface.java
info.xuluan.podcast.parser.FeedParserListener.java
info.xuluan.podcast.parser.FeedParser.java
info.xuluan.podcast.parser.OPMLParserHandler.java
info.xuluan.podcast.parser.SearchItem.java
info.xuluan.podcast.provider.FeedItem.java
info.xuluan.podcast.provider.ItemColumns.java
info.xuluan.podcast.provider.PodcastOpenHelper.java
info.xuluan.podcast.provider.PodcastProvider.java
info.xuluan.podcast.provider.SubscriptionColumns.java
info.xuluan.podcast.provider.Subscription.java
info.xuluan.podcast.service.PlayerService.java
info.xuluan.podcast.service.PodcastService.java
info.xuluan.podcast.utils.DialogMenu.java
info.xuluan.podcast.utils.FileUtils.java
info.xuluan.podcast.utils.IconCursorAdapter.java
info.xuluan.podcast.utils.LabeledFrame.java
info.xuluan.podcast.utils.LockHandler.java
info.xuluan.podcast.utils.Log.java
info.xuluan.podcast.utils.SDCardMgr.java
info.xuluan.podcast.utils.StrUtils.java
info.xuluan.podcast.utils.ZipExporter.java
info.xuluan.podcast.utils.ZipImporter.java