Android Open Source - texthem Custom Array Adapter






From Project

Back to project page texthem.

License

The source code is released under:

GNU General Public License

If you think the Android project texthem 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.danielme.blog.demo.listviewcheckbox;
//from w  w w  . ja v a2  s  .co m
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

import a2.marketingsms.R;

/**
 * Custom adapter - "View Holder Pattern".
 * 
 * @author danielme.com
 * 
 */
public class CustomArrayAdapter extends ArrayAdapter<Row>
{
  private LayoutInflater layoutInflater;

  public CustomArrayAdapter(Context context, List<Row> objects)
  {
    super(context, 0, objects);
    layoutInflater = LayoutInflater.from(context);
  }

  @Override
  public View getView(final int position, View convertView, ViewGroup parent)
  {
    // holder pattern
    Holder holder = null;
    if (convertView == null)
    {
      holder = new Holder();

      convertView = layoutInflater.inflate(R.layout.listview_row, null);
      holder.setTextViewTitle((TextView) convertView.findViewById(R.id.textViewTitle));
      holder.setTextViewSubtitle((TextView) convertView.findViewById(R.id.textViewSubtitle));
      holder.setCheckBox((CheckBox) convertView.findViewById(R.id.checkBox));
      convertView.setTag(holder);
    }
    else
    {
      holder = (Holder) convertView.getTag();
    }

    final Row row = getItem(position);
    holder.getTextViewTitle().setText(row.getTitle());
    holder.getTextViewSubtitle().setText(row.getSubtitle());
    
    holder.getCheckBox().setTag(row.getTitle());
    holder.getCheckBox().setChecked(row.isChecked());  
    final View fila= convertView;
    changeBackground(getContext(), fila, row.isChecked());    
    holder.getCheckBox().setOnCheckedChangeListener(new OnCheckedChangeListener()
    { 
      @Override
      public void onCheckedChanged(CompoundButton view, boolean isChecked)
      {
        //asegura que se modifica la Row originalmente asociado a este checkbox
        //para evitar que al reciclar la vista se reinicie el row que antes se mostraba en esta
        //fila. Es imprescindible tagear el Row antes de establecer el valor del checkbox
        if (row.getTitle().equals(view.getTag().toString()))
        {
          row.setChecked(isChecked);
          changeBackground(CustomArrayAdapter.this.getContext(), fila, isChecked);
          //desmarca todas los dems (si slo se permite un item marcado a la vez).
          /*Row row = null;
          for(int i=0 ; i<getCount() ; i++)
          {            
            if (i != position && isChecked)   
            {
              row = (Row) getOption(i);
              row.setChecked(false);                
            }
          }
          notifyDataSetChanged();*/
        }
      }
    });
    
    
    return convertView;
  }
  
  /**
   * Set the background of a row based on the value of its checkbox value. Checkbox has its own style.
   */
  @SuppressLint("NewApi")
    @SuppressWarnings("deprecation")
  private void changeBackground(Context context, View row, boolean checked)
  {
    if (row != null)
    {
      Drawable drawable;
      if (checked)
      {
        drawable = context.getResources().getDrawable(R.drawable.listview_selector_checked);
      }
      else
      {
        drawable = context.getResources().getDrawable(R.drawable.listview_selector);
      }
      int sdk = android.os.Build.VERSION.SDK_INT;
      if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
          row.setBackgroundDrawable(drawable);
      } else {
          row.setBackground(drawable);
      }
    }
  }

}

class Holder
{
  TextView textViewTitle;
  TextView textViewSubtitle;
  CheckBox checkBox;

  public TextView getTextViewTitle()
  {
    return textViewTitle;
  }

  public void setTextViewTitle(TextView textViewTitle)
  {
    this.textViewTitle = textViewTitle;
  }

  public TextView getTextViewSubtitle()
  {
    return textViewSubtitle;
  }

  public void setTextViewSubtitle(TextView textViewSubtitle)
  {
    this.textViewSubtitle = textViewSubtitle;
  }

  public CheckBox getCheckBox()
  {
    return checkBox;
  }

  public void setCheckBox(CheckBox checkBox)
  {
    this.checkBox = checkBox;
  }  

}




Java Source Code List

a2.marketingsms.ApplicationTest.java
a2.marketingsms.SelectList.java
a2.marketingsms.Texthem.java
a2.marketingsms.components.ContactImporter.java
a2.marketingsms.components.DialogHandler.java
a2.marketingsms.components.MyProgressDialog.java
a2.marketingsms.components.SMSSender.java
a2.marketingsms.model.Contact.java
a2.marketingsms.model.TemplateText.java
au.com.bytecode.opencsv.CSVIterator.java
au.com.bytecode.opencsv.CSVParserBuilder.java
au.com.bytecode.opencsv.CSVParser.java
au.com.bytecode.opencsv.CSVReaderBuilder.java
au.com.bytecode.opencsv.CSVReader.java
au.com.bytecode.opencsv.CSVWriter.java
au.com.bytecode.opencsv.ResultSetHelperService.java
au.com.bytecode.opencsv.ResultSetHelper.java
com.danielme.blog.demo.listviewcheckbox.CustomArrayAdapter.java
com.danielme.blog.demo.listviewcheckbox.DontPressWhenPressParentCheckBox.java
com.danielme.blog.demo.listviewcheckbox.Row.java
filechooser.FileArrayAdapter.java
filechooser.FileChooser.java
filechooser.Option.java
org.mozilla.universalchardet.CharsetListener.java
org.mozilla.universalchardet.Constants.java
org.mozilla.universalchardet.UniversalDetector.java
org.mozilla.universalchardet.prober.Big5Prober.java
org.mozilla.universalchardet.prober.CharsetProber.java
org.mozilla.universalchardet.prober.EUCJPProber.java
org.mozilla.universalchardet.prober.EUCKRProber.java
org.mozilla.universalchardet.prober.EUCTWProber.java
org.mozilla.universalchardet.prober.EscCharsetProber.java
org.mozilla.universalchardet.prober.GB18030Prober.java
org.mozilla.universalchardet.prober.HebrewProber.java
org.mozilla.universalchardet.prober.Latin1Prober.java
org.mozilla.universalchardet.prober.MBCSGroupProber.java
org.mozilla.universalchardet.prober.SBCSGroupProber.java
org.mozilla.universalchardet.prober.SJISProber.java
org.mozilla.universalchardet.prober.SingleByteCharsetProber.java
org.mozilla.universalchardet.prober.UTF8Prober.java
org.mozilla.universalchardet.prober.contextanalysis.EUCJPContextAnalysis.java
org.mozilla.universalchardet.prober.contextanalysis.JapaneseContextAnalysis.java
org.mozilla.universalchardet.prober.contextanalysis.SJISContextAnalysis.java
org.mozilla.universalchardet.prober.distributionanalysis.Big5DistributionAnalysis.java
org.mozilla.universalchardet.prober.distributionanalysis.CharDistributionAnalysis.java
org.mozilla.universalchardet.prober.distributionanalysis.EUCJPDistributionAnalysis.java
org.mozilla.universalchardet.prober.distributionanalysis.EUCKRDistributionAnalysis.java
org.mozilla.universalchardet.prober.distributionanalysis.EUCTWDistributionAnalysis.java
org.mozilla.universalchardet.prober.distributionanalysis.GB2312DistributionAnalysis.java
org.mozilla.universalchardet.prober.distributionanalysis.JISDistributionAnalysis.java
org.mozilla.universalchardet.prober.distributionanalysis.SJISDistributionAnalysis.java
org.mozilla.universalchardet.prober.sequence.BulgarianModel.java
org.mozilla.universalchardet.prober.sequence.CyrillicModel.java
org.mozilla.universalchardet.prober.sequence.GreekModel.java
org.mozilla.universalchardet.prober.sequence.HebrewModel.java
org.mozilla.universalchardet.prober.sequence.Ibm855Model.java
org.mozilla.universalchardet.prober.sequence.Ibm866Model.java
org.mozilla.universalchardet.prober.sequence.Koi8rModel.java
org.mozilla.universalchardet.prober.sequence.Latin5BulgarianModel.java
org.mozilla.universalchardet.prober.sequence.Latin5Model.java
org.mozilla.universalchardet.prober.sequence.Latin7Model.java
org.mozilla.universalchardet.prober.sequence.MacCyrillicModel.java
org.mozilla.universalchardet.prober.sequence.SequenceModel.java
org.mozilla.universalchardet.prober.sequence.Win1251BulgarianModel.java
org.mozilla.universalchardet.prober.sequence.Win1251Model.java
org.mozilla.universalchardet.prober.sequence.Win1253Model.java
org.mozilla.universalchardet.prober.statemachine.Big5SMModel.java
org.mozilla.universalchardet.prober.statemachine.CodingStateMachine.java
org.mozilla.universalchardet.prober.statemachine.EUCJPSMModel.java
org.mozilla.universalchardet.prober.statemachine.EUCKRSMModel.java
org.mozilla.universalchardet.prober.statemachine.EUCTWSMModel.java
org.mozilla.universalchardet.prober.statemachine.GB18030SMModel.java
org.mozilla.universalchardet.prober.statemachine.HZSMModel.java
org.mozilla.universalchardet.prober.statemachine.ISO2022CNSMModel.java
org.mozilla.universalchardet.prober.statemachine.ISO2022JPSMModel.java
org.mozilla.universalchardet.prober.statemachine.ISO2022KRSMModel.java
org.mozilla.universalchardet.prober.statemachine.PkgInt.java
org.mozilla.universalchardet.prober.statemachine.SJISSMModel.java
org.mozilla.universalchardet.prober.statemachine.SMModel.java
org.mozilla.universalchardet.prober.statemachine.UCS2BESMModel.java
org.mozilla.universalchardet.prober.statemachine.UTF8SMModel.java