Android Open Source - downtown Multi Selection Adapter






From Project

Back to project page downtown.

License

The source code is released under:

GNU General Public License

If you think the Android project downtown 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 org.dklisiaris.downtown.adapters;
/*  w ww . j  av a  2  s. c o m*/
import org.dklisiaris.downtown.R;
import org.dklisiaris.downtown.db.Company;
import org.dklisiaris.downtown.helper.ImageLoader;


import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public final class MultiSelectionAdapter extends BaseAdapter
{
  private LayoutInflater inflater;
  private ArrayList<Company> items;
  private Context context;
  private SparseArray<String> mapHash;
  // all our checked indexes go here
  private HashSet<Integer> checkedItems;
  private ImageLoader iLoader;
  
  
  // multi selection mode flag
  private boolean multiMode;
  
  public MultiSelectionAdapter(Context context, ArrayList<Company> items, SparseArray<String> hash)
  {
    this.inflater = LayoutInflater.from(context);
    this.items = items;
    this.checkedItems = new HashSet<Integer>();
    this.context=context;
    mapHash=hash;
    iLoader = new ImageLoader(context);
  }
  
  public MultiSelectionAdapter(Context context, SparseArray<String> hash)  
  {    
    this(context, new ArrayList<Company>(), hash);    
  }
  
  public void enterMultiMode()
  {
    this.multiMode = true;
    this.notifyDataSetChanged();
  }
  
  public void exitMultiMode()
  {
    this.checkedItems.clear();
    this.multiMode = false;
    this.notifyDataSetChanged();
  }
  
  public void setChecked(int pos, boolean checked)
  {
  if (checked) {
      this.checkedItems.add(Integer.valueOf(pos));
  } else {
      this.checkedItems.remove(Integer.valueOf(pos));
  }
  if (this.multiMode) {
      this.notifyDataSetChanged();
  }
  }
  
  public boolean isChecked(int pos)
  {
    return this.checkedItems.contains(Integer.valueOf(pos));
  }
  
  public void toggleChecked(int pos)
  {
    final Integer v = Integer.valueOf(pos);
    if (this.checkedItems.contains(v)) {
      this.checkedItems.remove(v);      
    } else {
      this.checkedItems.add(v);
    }
    this.notifyDataSetChanged();
  }
  
  public int getCheckedItemCount()
  {
    return this.checkedItems.size();
  }
  
  // we use this convinience method for rename thingie.
  public Company getFirstCheckedItem()
  {
    for (Integer i : this.checkedItems) {
        return this.items.get(i.intValue());
  }
  return null;
  }
  
  public Set<Integer> getCheckedItems()
  {
    return this.checkedItems;
  }
  
  public void clear()
  {
    this.items.clear();
  }
  
  public void updateData(ArrayList<Company> data)
  {
    for (Company p : data) {
        this.items.add(p);
    }
    this.checkedItems.clear();
    this.notifyDataSetChanged();
  }
  
  @Override
  public int getCount()
  {
    return this.items.size();
  }
  
  @Override
  public Company getItem(int position)
  {
    return this.items.get(position);
  }
  
  @Override
  public long getItemId(int position)
  {
    return position;
  }
  
  @Override
  public boolean hasStableIds()
  {
    return true;
  }
  
  @Override
  public View getView(int position, View convertView, ViewGroup parent)
  {
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.products_row, null);
        // convert view stuff cleared, to much code - do the usual Holder stuff here
    
    }
    
        TextView name = (TextView)convertView.findViewById(R.id.name); 
        TextView address = (TextView)convertView.findViewById(R.id.address); 
        TextView area = (TextView)convertView.findViewById(R.id.area); 
        TextView tel = (TextView)convertView.findViewById(R.id.tel);
        TextView categ = (TextView)convertView.findViewById(R.id.categ); 
        TextView subcat = (TextView)convertView.findViewById(R.id.subcateg); 
        ImageView thumb_image=(ImageView)convertView.findViewById(R.id.list_image); 
                      
        Company pr = getItem(position);
        // Setting all values in listview
        name.setText(pr.getName());
        address.setText(pr.getAddress());
        area.setText(pr.getArea());
        if(pr.getTels().get(0) != null && pr.getTels().get(0) != "")
          tel.setText(pr.getTels().get(0));
        subcat.setText(mapHash.get(Integer.parseInt(pr.getSubcategories().get(0))));
        categ.setText(mapHash.get(pr.getCategory()));
        
        Drawable d=null;
        try{
          if(pr.getFirst_img()!=null){
             // load image into drawable
            d = iLoader.loadImage(pr.getFirst_img());
          }
         }catch(Exception e){e.printStackTrace();}
        
        if(d==null)d = iLoader.loadImage("Blankside.jpg");
        thumb_image.setImageDrawable(d);
                  
    // the 4 state change problem described above. We use a second selector with no pressed state color if in multi mode
    convertView
            .setBackgroundResource(this.multiMode ? R.drawable.fav_list_multimode
                    : R.drawable.tablist_selector);
    
    if (checkedItems.contains(Integer.valueOf(position))) {
        // if this item is checked - set checked state
        convertView.getBackground().setState(
                new int[] { android.R.attr.state_checked });
    } else {
        // if this item is unchecked - set unchecked state (notice the minus)
        convertView.getBackground().setState(
                new int[] { -android.R.attr.state_checked });
    }
    
    
    return convertView;
  }

}




Java Source Code List

com.google.maps.android.BuildConfig.java
com.google.maps.android.BuildConfig.java
com.google.maps.android.MarkerManager.java
com.google.maps.android.MathUtil.java
com.google.maps.android.PolyUtil.java
com.google.maps.android.SphericalUtil.java
com.google.maps.android.clustering.ClusterItem.java
com.google.maps.android.clustering.ClusterManager.java
com.google.maps.android.clustering.Cluster.java
com.google.maps.android.clustering.algo.Algorithm.java
com.google.maps.android.clustering.algo.GridBasedAlgorithm.java
com.google.maps.android.clustering.algo.NonHierarchicalDistanceBasedAlgorithm.java
com.google.maps.android.clustering.algo.PreCachingAlgorithmDecorator.java
com.google.maps.android.clustering.algo.StaticCluster.java
com.google.maps.android.clustering.view.ClusterRenderer.java
com.google.maps.android.clustering.view.DefaultClusterRenderer.java
com.google.maps.android.geometry.Bounds.java
com.google.maps.android.geometry.Point.java
com.google.maps.android.projection.Point.java
com.google.maps.android.projection.SphericalMercatorProjection.java
com.google.maps.android.quadtree.PointQuadTree.java
com.google.maps.android.ui.BubbleIconFactory.java
com.google.maps.android.ui.IconGenerator.java
com.google.maps.android.ui.RotationLayout.java
com.google.maps.android.ui.SquareTextView.java
com.sothree.slidinguppanel.SlidingUpPanelLayout.java
com.sothree.slidinguppanel.library.BuildConfig.java
com.sothree.slidinguppanel.library.BuildConfig.java
org.dklisiaris.downtown.Addresses.java
org.dklisiaris.downtown.BuildConfig.java
org.dklisiaris.downtown.FavsActivity.java
org.dklisiaris.downtown.GlobalData.java
org.dklisiaris.downtown.Intro.java
org.dklisiaris.downtown.MainActivity.java
org.dklisiaris.downtown.Manifest.java
org.dklisiaris.downtown.MapActivity.java
org.dklisiaris.downtown.MoreActivity.java
org.dklisiaris.downtown.Products.java
org.dklisiaris.downtown.SearchActivity.java
org.dklisiaris.downtown.Search.java
org.dklisiaris.downtown.SingleListItem.java
org.dklisiaris.downtown.Subcategories.java
org.dklisiaris.downtown.SubcatsAndFilters.java
org.dklisiaris.downtown.Tabs.java
org.dklisiaris.downtown.TestActivity.java
org.dklisiaris.downtown.WebViewActivity.java
org.dklisiaris.downtown.Websites.java
org.dklisiaris.downtown.actionbar.ActionBar.java
org.dklisiaris.downtown.actionbar.ScrollingTextView.java
org.dklisiaris.downtown.adapters.AddressFilterAdapter.java
org.dklisiaris.downtown.adapters.CustomAdapter.java
org.dklisiaris.downtown.adapters.CustomStringAdapter.java
org.dklisiaris.downtown.adapters.CustomSuggestionsAdapter.java
org.dklisiaris.downtown.adapters.MultiSelectionAdapter.java
org.dklisiaris.downtown.adapters.SubcatsAdapter.java
org.dklisiaris.downtown.db.Banner.java
org.dklisiaris.downtown.db.Category.java
org.dklisiaris.downtown.db.Company.java
org.dklisiaris.downtown.db.DBHandler.java
org.dklisiaris.downtown.db.DBInterface.java
org.dklisiaris.downtown.db.Image.java
org.dklisiaris.downtown.db.InitData.java
org.dklisiaris.downtown.db.Keyword.java
org.dklisiaris.downtown.db.Mapping.java
org.dklisiaris.downtown.db.Product.java
org.dklisiaris.downtown.db.QueryBuilder.java
org.dklisiaris.downtown.downloader.DownloadTask.java
org.dklisiaris.downtown.downloader.NotificationHelper.java
org.dklisiaris.downtown.helper.AccessAssets.java
org.dklisiaris.downtown.helper.AlertDialogManager.java
org.dklisiaris.downtown.helper.ConnectionDetector.java
org.dklisiaris.downtown.helper.FileCache.java
org.dklisiaris.downtown.helper.ImageLoader.java
org.dklisiaris.downtown.helper.InfoHelper.java
org.dklisiaris.downtown.helper.KeyboardUtil.java
org.dklisiaris.downtown.helper.MemoryCache.java
org.dklisiaris.downtown.helper.ShareHelper.java
org.dklisiaris.downtown.helper.UpdateConfirmDialog.java
org.dklisiaris.downtown.helper.UpdateHelper.java
org.dklisiaris.downtown.helper.Utils.java
org.dklisiaris.downtown.helper.XMLParser.java
org.dklisiaris.downtown.maps.AbstractMapActivity.java
org.dklisiaris.downtown.maps.CompanyMarker.java
org.dklisiaris.downtown.maps.DirectionsInfo.java
org.dklisiaris.downtown.maps.GMapV2Direction.java
org.dklisiaris.downtown.maps.Nearby.java
org.dklisiaris.downtown.maps.PopupAdapter.java
org.dklisiaris.downtown.providers.KeywordContract.java
org.dklisiaris.downtown.providers.KeywordProvider.java
org.dklisiaris.downtown.widgets.AspectRatioImageView.java
org.dklisiaris.downtown.widgets.CheckableRelativeLayout.java
org.dklisiaris.downtown.widgets.CustomScrollView.java
org.dklisiaris.downtown.widgets.FlipAnimator.java
org.dklisiaris.downtown.widgets.InertCheckBox.java
org.dklisiaris.downtown.widgets.MultiSpinner.java