Android Open Source - spots Checkboxes Dropdown View






From Project

Back to project page spots.

License

The source code is released under:

MIT License

If you think the Android project spots 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.dcc.matc89.spots.view;
/*ww  w  .  j a  v a  2 s  . c  o m*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;

public class CheckboxesDropdownView extends DropdownView{
  
  private ArrayList<Boolean> mCheckeds;
  private CheckBoxesAdapter mAdapter;
  private OnDropdownCheckedChangeListener mListener;

  public CheckboxesDropdownView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
  }

  public CheckboxesDropdownView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
  }

  public CheckboxesDropdownView(Context context) {
    super(context);
    init();
  }
  
  public void setOnDropdownCheckedChangeListener(OnDropdownCheckedChangeListener listener){
    mListener = listener;
  }
  
  public void setItems(List<?> items){
    mCheckeds = new ArrayList<Boolean>(items.size());
    for(int i = 0; i < items.size(); i++)
      mCheckeds.add(Boolean.FALSE);
    mAdapter.clear();
    mAdapter.addAll(items);
  }
  
  public void setCheckedAll(boolean checked){
    Boolean isChecked = checked;
    for(int i = 0; i < mCheckeds.size(); i++)
      mCheckeds.set(i, isChecked);
    mAdapter.notifyDataSetChanged();
  }
  
  public boolean[] getState(){
    boolean[] state = new boolean[mCheckeds.size()];
    for(int i = 0; i < state.length; i++)
      state[i] = mCheckeds.get(i).booleanValue();
    return state;
  }
  
  @Override
  public void setContentView(View view) {
    throw new UnsupportedOperationException("Don't use setContentView on a CheckboxesDropdownView.");
  }

  private void init(){
    super.setContentView(getListView());
  }

  private View getListView() {
    Context context = getContext();
    ListView list = new ListView(context);
    list.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    mAdapter = new CheckBoxesAdapter(context);
    list.setAdapter(mAdapter);
    return list;
  }
  
  
  private class CheckBoxesAdapter extends ArrayAdapter<Object> {

    public CheckBoxesAdapter(Context context) {
      super(context, 0, new ArrayList<Object>());
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      if(convertView == null){
        convertView = new CheckBox(getContext());
        ((CheckBox)convertView).setOnCheckedChangeListener(onCheckedChangeListener);
        convertView.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
      }
      
      convertView.setTag(null);
      ((CheckBox)convertView).setText(getItem(position).toString());
      ((CheckBox)convertView).setChecked(mCheckeds.size() > position ? mCheckeds.get(position) : false);
      convertView.setTag(position);
      return convertView;
    }
  }
  
  private CompoundButton.OnCheckedChangeListener onCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
    
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
      Integer index = (Integer) buttonView.getTag();
      if(index == null)
        return;
      mCheckeds.set(index, isChecked);
      if(mListener != null)
        mListener.onCheckedChanged(CheckboxesDropdownView.this, index, isChecked);
    }
  };
  
  public interface OnDropdownCheckedChangeListener {
    void onCheckedChanged(CheckboxesDropdownView picker, int index, boolean isChecked);
  }
}




Java Source Code List

com.dcc.matc89.spots.activity.GroupDetailActivity.java
com.dcc.matc89.spots.activity.GroupEditActivity.java
com.dcc.matc89.spots.activity.GroupListActivity.java
com.dcc.matc89.spots.activity.LoginActionBarActivity.java
com.dcc.matc89.spots.activity.LoginActivity.java
com.dcc.matc89.spots.activity.MainActivity.java
com.dcc.matc89.spots.activity.MyGroupsListActivity.java
com.dcc.matc89.spots.activity.SpotDetailActivity.java
com.dcc.matc89.spots.activity.SpotEditActivity.java
com.dcc.matc89.spots.activity.SpotListActivity.java
com.dcc.matc89.spots.activity.UserDetailActivity.java
com.dcc.matc89.spots.activity.UserListActivity.java
com.dcc.matc89.spots.model.Group.java
com.dcc.matc89.spots.model.Sport.java
com.dcc.matc89.spots.model.Spot.java
com.dcc.matc89.spots.model.StaticDatabase.java
com.dcc.matc89.spots.model.User.java
com.dcc.matc89.spots.network.FetchGroupsAsyncTask.java
com.dcc.matc89.spots.network.FetchGroups.java
com.dcc.matc89.spots.network.FetchSportsAsyncTask.java
com.dcc.matc89.spots.network.FetchSports.java
com.dcc.matc89.spots.network.FetchSpotsAsyncTask.java
com.dcc.matc89.spots.network.FetchSpots.java
com.dcc.matc89.spots.network.FetchUsersAsyncTask.java
com.dcc.matc89.spots.network.FetchUsers.java
com.dcc.matc89.spots.network.OnResultReceiver.java
com.dcc.matc89.spots.network.PostGroupsAsyncTask.java
com.dcc.matc89.spots.network.PostGroups.java
com.dcc.matc89.spots.network.PostLinkSpotGroupAsyncTask.java
com.dcc.matc89.spots.network.PostLinkSpotGroup.java
com.dcc.matc89.spots.network.PostSpotsAsyncTask.java
com.dcc.matc89.spots.network.PostSpots.java
com.dcc.matc89.spots.network.PostUsersAsyncTask.java
com.dcc.matc89.spots.network.PostUsers.java
com.dcc.matc89.spots.network.Utils.java
com.dcc.matc89.spots.social.FacebookFragment.java
com.dcc.matc89.spots.social.LoginFragment.java
com.dcc.matc89.spots.social.OnUserLoginListener.java
com.dcc.matc89.spots.view.CheckboxesDropdownView.java
com.dcc.matc89.spots.view.DropdownView.java