Android Open Source - dejalist Navigation Cursor Adapter






From Project

Back to project page dejalist.

License

The source code is released under:

Apache License

If you think the Android project dejalist 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 com.luboganev.dejalist.ui;
//ww w .  j  a  va 2s  . c om
import static nl.qbusict.cupboard.CupboardFactory.cupboard;
import android.content.Context;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.database.MergeCursor;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import butterknife.InjectView;
import butterknife.Views;

import com.luboganev.dejalist.R;
import com.luboganev.dejalist.data.DejalistContract.Categories;
import com.luboganev.dejalist.data.DejalistContract.Products;
import com.luboganev.dejalist.data.entities.Category;

public class NavigationCursorAdapter extends CursorAdapter {
  public static final int VIEW_TYPE_CHECKLIST = 0; 
  public static final int VIEW_TYPE_ALL_PRODUCTS = 1; 
  public static final int VIEW_TYPE_CATEGORY = 2; 
  public static final int VIEW_TYPE_ADD_CATEGORY = 3; 
  
  public static final long NAV_CHECKLIST_ITEM_ID = -101;
  public static final long NAV_ALL_PRODUCTS_ITEM_ID = -102;
  
  public static final long NAV_ADD_CATEGORY_ITEM_ID = -999;
  
  public static final int POSITION_CHECKLIST = 0;
  public static final int POSITION_ALL_PRODUCTS = 1;
  public static final int POSITION_NO_CATEGORY = 2;
  
    private LayoutInflater mInflater;
    private OnClickListener mAddCategoryClickListener;
  
  private static Cursor addMainNavigationItems(Cursor categories) {
    MatrixCursor mainNavigation = new MatrixCursor(new String[] {Categories._ID, Categories.CATEGORY_NAME, Categories.CATEGORY_COLOR});
    mainNavigation.addRow(new Object[]{NAV_CHECKLIST_ITEM_ID, "", 0});
    mainNavigation.addRow(new Object[]{NAV_ALL_PRODUCTS_ITEM_ID, "", 0});
    mainNavigation.addRow(new Object[]{Products.PRODUCT_CATEGORY_NONE_ID, "", 0});
    if(categories != null) return new MergeCursor(new Cursor[]{mainNavigation, categories});
    else return mainNavigation;
  }
  
  @Override
  public int getCount() {
    return super.getCount() + 1;
  }
  
  @Override
  public int getItemViewType(int position) {
    if(position == POSITION_CHECKLIST) return VIEW_TYPE_CHECKLIST;
    else if(position == POSITION_ALL_PRODUCTS) return VIEW_TYPE_ALL_PRODUCTS;
    else if(position == getCount() - 1) return VIEW_TYPE_ADD_CATEGORY;
    else return VIEW_TYPE_CATEGORY;
  }
  
  @Override
  public int getViewTypeCount() {
    return 4;
  }
  
  @Override
  public Object getItem(int position) {
    if(getItemViewType(position) == VIEW_TYPE_ADD_CATEGORY) {
      return null;
    }
    else {
      return super.getItem(position);
    }
  }
  
  @Override
  public long getItemId(int position) {
    if(getItemViewType(position) == VIEW_TYPE_ADD_CATEGORY) {
      return NAV_ADD_CATEGORY_ITEM_ID;
    }
    else {
      return super.getItemId(position);
    }
  }
  
  @Override
  public boolean areAllItemsEnabled() {
    return false;
  }
  
  @Override
  public boolean isEnabled(int position) {
    return getItemViewType(position) != VIEW_TYPE_ADD_CATEGORY;
  }

  public NavigationCursorAdapter(Context context, int flags, OnClickListener addCategoryClickListener) {
    super(context, addMainNavigationItems(null), flags);
    mInflater = LayoutInflater.from(context);
    mAddCategoryClickListener = addCategoryClickListener;
  }
  
  @Override
  public Cursor swapCursor(Cursor categories) {
    //change cursor internally calls swap cursor, so extra 
    // values should be added only once during swap
    return super.swapCursor(addMainNavigationItems(categories));
  }
  
  /**
   * @see android.widget.ListAdapter#getView(int, View, ViewGroup)
   */
  public View getView(int position, View convertView, ViewGroup parent) {
    int viewType = getItemViewType(position);
    if(viewType == VIEW_TYPE_CHECKLIST) {
      if (convertView == null) {
        convertView = mInflater.inflate(R.layout.navlist_item_checklist,
                parent, false);
      }
      convertView.setEnabled(isEnabled(position));
      return convertView;
    }
    else if(viewType == VIEW_TYPE_ALL_PRODUCTS) {
      if (convertView == null) {
        convertView = mInflater.inflate(R.layout.navlist_item_all_products,
            parent, false);
      }
      convertView.setEnabled(isEnabled(position));
      return convertView;
    }
    else if(viewType == VIEW_TYPE_CATEGORY) {
      return super.getView(position, convertView, parent);
    }
    else if(viewType == VIEW_TYPE_ADD_CATEGORY) {
      if (convertView == null) {
        convertView = mInflater.inflate(R.layout.navlist_item_category_add,
                parent, false);
      }
      convertView.setEnabled(isEnabled(position));
      convertView.findViewById(R.id.tv_add_category).setOnClickListener(mAddCategoryClickListener);
      return convertView;
    }
    else return null;
  }
  
  @Override
  public View getDropDownView(int position, View convertView, ViewGroup parent) {
    return null; // does not support dropdown items due to bug in Android with different types of dropdown items 
  }

  @Override
  public void bindView(View view, Context context, Cursor cursor) {
    ViewHolder holder = (ViewHolder) view.getTag();
    Category category = cupboard().withCursor(cursor).get(Category.class);
    
    if(category._id == Products.PRODUCT_CATEGORY_NONE_ID) {
      holder.catColor.setBackgroundResource(R.drawable.no_category_color);
      holder.name.setText(R.string.nav_products_no_category);
    }
    else {
      holder.catColor.setBackgroundColor(category.color);
      holder.name.setText(category.name);
    }
  }

  @Override
  public View newView(Context context, Cursor cursor, ViewGroup parent) {
      View view = LayoutInflater.from(context).inflate(R.layout.navlist_item_category, parent, false);
      ViewHolder holder = new ViewHolder(view);
      view.setTag(holder);
      return view;
  }
  
  static class ViewHolder {
    @InjectView(R.id.tv_nav_name) TextView name;
    @InjectView(R.id.v_nav_cat_color) View catColor;

    public ViewHolder(View view) {
      Views.inject(this, view);
    }
  }
}




Java Source Code List

com.larswerkman.colorpicker.ColorPicker.java
com.larswerkman.colorpicker.OpacityBar.java
com.larswerkman.colorpicker.SVBar.java
com.larswerkman.colorpicker.SaturationBar.java
com.larswerkman.colorpicker.ValueBar.java
com.luboganev.dejalist.Utils.java
com.luboganev.dejalist.crop.CropActivity.java
com.luboganev.dejalist.crop.CropDialogSave.java
com.luboganev.dejalist.crop.CropHighlightView.java
com.luboganev.dejalist.crop.CropUtils.java
com.luboganev.dejalist.crop.CropView.java
com.luboganev.dejalist.crop.ImageViewTouchBase.java
com.luboganev.dejalist.crop.RotateBitmap.java
com.luboganev.dejalist.data.BackupIntentService.java
com.luboganev.dejalist.data.CacheManager.java
com.luboganev.dejalist.data.DejalistContract.java
com.luboganev.dejalist.data.DejalistDatabase.java
com.luboganev.dejalist.data.DejalistProvider.java
com.luboganev.dejalist.data.ProductImageFileHelper.java
com.luboganev.dejalist.data.SelectionBuilder.java
com.luboganev.dejalist.data.entities.Category.java
com.luboganev.dejalist.data.entities.Product.java
com.luboganev.dejalist.ui.AboutActivity.java
com.luboganev.dejalist.ui.CategoriesListCursorAdapter$ViewHolder$$ViewInjector.java
com.luboganev.dejalist.ui.CategoriesListCursorAdapter.java
com.luboganev.dejalist.ui.CategoryDialogFragment$$ViewInjector.java
com.luboganev.dejalist.ui.CategoryDialogFragment.java
com.luboganev.dejalist.ui.CheckableRelativeLayout.java
com.luboganev.dejalist.ui.ChecklistActionTaker.java
com.luboganev.dejalist.ui.ChecklistController.java
com.luboganev.dejalist.ui.ChecklistCursorAdapter$ViewHolder$$ViewInjector.java
com.luboganev.dejalist.ui.ChecklistCursorAdapter.java
com.luboganev.dejalist.ui.ChecklistFragment$$ViewInjector.java
com.luboganev.dejalist.ui.ChecklistFragment.java
com.luboganev.dejalist.ui.ConfirmBackResDialogFragment.java
com.luboganev.dejalist.ui.MainActivity$$ViewInjector.java
com.luboganev.dejalist.ui.MainActivity.java
com.luboganev.dejalist.ui.NavigationCursorAdapter$ViewHolder$$ViewInjector.java
com.luboganev.dejalist.ui.NavigationCursorAdapter.java
com.luboganev.dejalist.ui.ProductActivity$$ViewInjector.java
com.luboganev.dejalist.ui.ProductActivity.java
com.luboganev.dejalist.ui.ProductsGalleryActionTaker.java
com.luboganev.dejalist.ui.ProductsGalleryController.java
com.luboganev.dejalist.ui.ProductsGalleryCursorAdapter$ViewHolder$$ViewInjector.java
com.luboganev.dejalist.ui.ProductsGalleryCursorAdapter.java
com.luboganev.dejalist.ui.ProductsGalleryFragment$$ViewInjector.java
com.luboganev.dejalist.ui.ProductsGalleryFragment.java
com.luboganev.dejalist.ui.SetProductsCategoryDialogFragment.java
com.luboganev.dejalist.ui.UndoBarController.java