Android Open Source - Android-Counter-App Counter List Adapter






From Project

Back to project page Android-Counter-App.

License

The source code is released under:

Apache License

If you think the Android project Android-Counter-App 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

/*
  Counter List Adapter//from w w w  .j  a v a2s.  com
  Idea from Jacek Milewski's code on custom ListViews
  
  The following class defines the adapter used by the main activity.
  The adapter allows the ListView to update with new views, 
  allowing for many counters to be added to the main activity.
  
  Copyright 2014 David Yee

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
 */

package ca.ualberta.cs.asn1;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.ImageButton;

/*
 * We implement a custom list adapter so that we can override the 
 * getView methods, allowing us to inflate our own views.
 * 
 * The listeners for most of the elements added here are statically 
 * defined in the XML files and are run in the CounterActivity.
 * 
 * We need to have a save model here too because we must save the 
 * EditText items as soon as they are modified.
 * 
 * We allow getting and setting of the list of counters so that the 
 * ArrayAdapter can be updated externally from this class.
 */
public class CounterListAdapter extends ArrayAdapter<CounterModel> {
  
  private List<CounterModel> counters;
  private int layout_id;
  private Context context;
  private CounterSaveModel persistentData;

  public CounterListAdapter(Context context, int layout_id, List<CounterModel> counters, String file_name) {
    super(context, layout_id, counters);
    
    this.context = context;
    this.layout_id = layout_id;
    this.counters = counters;
    this.persistentData = new CounterSaveModel(context, file_name);
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent){
    View row = convertView;
    CounterHolder holder = null;
    
    // inflate the new view defined by layout_id
    LayoutInflater inflater = ((Activity) context).getLayoutInflater();
    row = inflater.inflate(layout_id, parent, false);
    
    // initialize the counter holder
    // associate the holder with equivalent view items by ID
    // allow the views to be able to update the model
    holder = new CounterHolder();
    holder.counterModel = counters.get(position);
    
    holder.counterRemoveButton = (ImageButton)row.findViewById(R.id.counterRemove);
    holder.counterRemoveButton.setTag(holder.counterModel); // keep a reference to the counter
    
    holder.name = (AutoCompleteTextView)row.findViewById(R.id.counterName);
    holder.name.setText(holder.counterModel.getName());
    setNameTextChangeListener(holder);
    
    holder.value = (Button)row.findViewById(R.id.counterNumber);
    holder.value.setText(String.valueOf(holder.counterModel.getCount()));
    
    holder.counterStatisticButton = (ImageButton)row.findViewById(R.id.counterStatistic);
    holder.counterStatisticButton.setTag(R.id.TAG_COUNTERMODEL_ID, holder.counterModel);
    
    holder.counterIncrementButton = (Button)row.findViewById(R.id.counterIncrement);
    holder.counterIncrementButton.setTag(R.id.TAG_COUNTERMODEL_ID, holder.counterModel);
    holder.counterIncrementButton.setTag(R.id.TAG_COUNTERVALUE_ID, holder.value);
    
    row.setTag(holder);
    
    return row;
  }

  // use a static class to store references to all views set in the list item
  // the CounterModel object is also stored so that it can specifically be destroyed
  public static class CounterHolder {
    CounterModel counterModel;
    AutoCompleteTextView name;
    Button value;
    ImageButton counterRemoveButton;
    ImageButton counterStatisticButton;
    Button counterIncrementButton;
  }
  
  private void setNameTextChangeListener(final CounterHolder holder){
    holder.name.addTextChangedListener(new TextWatcher() {
      @Override
      public void onTextChanged(CharSequence ch, int arg1, int arg2, int arg3){
        holder.counterModel.setName(ch.toString()); // update the model
        persistentData.saveData(counters);
      }

      @Override
      public void afterTextChanged(Editable arg0) {
      }

      @Override
      public void beforeTextChanged(CharSequence arg0, int arg1,
          int arg2, int arg3) {
      }
    });
  }

  public List<CounterModel> getCounters() {
    return this.counters;
  }

  public void setCounters(List<CounterModel> counters) {
    this.counters = counters;
  }
  
}




Java Source Code List

ca.ualberta.cs.asn1.CounterActivity.java
ca.ualberta.cs.asn1.CounterListAdapter.java
ca.ualberta.cs.asn1.CounterModel.java
ca.ualberta.cs.asn1.CounterSaveModel.java
ca.ualberta.cs.asn1.StatisticActivity.java
ca.ualberta.cs.asn1.StatisticModel.java