Android Open Source - Android-Counter-App Counter Activity






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

/*
  CounterActivity/*from w ww . j av a2  s  .  co  m*/
  
  This is the main activity for the app. CounterActivity is responsible for 
  handling the events of the application lifetime.
  
  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.ArrayList;
import java.util.Collections;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

/*
 * CounterActivity class is the main class that is run when the program starts.
 * Data that must be kept safe is done by using the CounterSaveModel. Every 
 * time this Activity class is resumed (after rotating tilt, switching apps,
 * or closing the app), the data is restored safely since data is saved 
 * immediately (f.i. on button click).
 */
public class CounterActivity extends Activity {
  private CounterListAdapter adapter;
  private ListView counterListView;
  private CounterSaveModel persistentData; // stores a list of all CounterModels
  // the following three vars are for writing back if the user zero's a counter
  private CounterSaveModel resetData;
  private int position; // position in adapter to replace
  private boolean zeroCounter = false; // flag to determine whether an update is necessary
  
  protected List<CounterModel> counters = new ArrayList<CounterModel>();
  protected List<CounterModel> newData = new ArrayList<CounterModel>();

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.counter_list_view);
    
    persistentData = new CounterSaveModel(CounterActivity.this, this.getString(R.string.file_sav));
    resetData = new CounterSaveModel(CounterActivity.this, this.getString(R.string.stat_sav));
  }
  
  @Override
  protected void onResume() {
    super.onResume();
    
    if(zeroCounter == true){
      newData = resetData.loadData();
      CounterModel old = adapter.getItem(position);
      old.setCount(newData.get(0).getCount());
      persistentData.saveData(this.counters);
      zeroCounter = false;
    }
    
    this.counters = persistentData.loadData();
    
    this.adapter = new CounterListAdapter(CounterActivity.this, 
         R.layout.counter_list,
         this.counters, this.getString(R.string.file_sav));
    counterListView = (ListView)findViewById(R.id.counterList);
    counterListView.setAdapter(adapter);
  }
  
        /*
         * addCounter is assigned to a specific view item defined in the 
         * counter_list.xml file. Since an ArrayAdapter is being used, 
         * it is as simple as just using the .add(obj) function in order 
         * to append a new CounterModel to the adapter. The adapter will 
         * redraw the GUI and a notification of data set change is not 
         * explicitly necessary.
         * 
         * The data must be saved immediately to allow for correct restore 
         * onResume.
         */
  public void addCounter(View view) {
    CounterModel obj = new CounterModel();
    adapter.add(obj);
    persistentData.saveData(adapter.getCounters());
  }
  
        /*
         * incrementCounter is assigned to a specific view item defined in the 
         * counter_list.xml file. Tags are used to obtain the specific 
         * information that the view is representing. We must update another UI 
         * element and therefore we use getTag with an ID specified in the 
         * strings.xml file.
         */
  public void incrementCounter(View view){
    CounterModel item = (CounterModel)view.getTag(R.id.TAG_COUNTERMODEL_ID);
    Button display = (Button)view.getTag(R.id.TAG_COUNTERVALUE_ID);
    item.addCount();
    display.setText(String.valueOf(item.getCount()));
    persistentData.saveData(adapter.getCounters());
  }
  
  /*
         * showStatistic is assigned to a specific view item defined in the 
         * counter_list.xml file. Tags are used to obtain the specific 
         * CounterModel the view is representing.
         * 
         * We must prepare the main activity to start another one. Since the 
         * StatisticActivity will be able to zero the counter, we must maintain 
         * the position of the CounterModel we are sending through putExtra 
         * BEFORE returning from the StatisticActivity.
         * 
         * Therefore, position is set here. We set zeroCounter as a flag so that 
         * on the next onResume we know that we should restore any changes 
         * that may have occurred to the CounterModel we sent; however, we do not 
         * want to overwrite the data repeatedly every time onResume is executed.
         * The zeroCounter flag is changed to false after it has executed once in
         * onResume.
         */  public void showStatistic(View view) {
    Intent intent = new Intent(this, StatisticActivity.class);
    CounterModel counterData = (CounterModel)view.getTag(R.id.TAG_COUNTERMODEL_ID);
    intent.putExtra("CounterModel", counterData);
    
    position = adapter.getPosition(counterData);
    zeroCounter = true;
    newData.clear();
    newData.add(counterData);
    resetData.saveData(newData);
    
    startActivity(intent);
  }

  /*
   * removeCounter is assigned to a specific view item defined in the 
   * counter_list.xml file. Tags are used to obtain the specific 
   * CounterModel the view is representing.
   */
  public void removeCounter(View view){
    CounterModel item = (CounterModel)view.getTag();
    adapter.remove(item);
    persistentData.saveData(adapter.getCounters());
  }
  
  /*
   * sortCounters is assigned to a specific view item defined in the 
   * counter_list.xml file. A comparator implementation in the 
   * CounterModel is used to allow sorting in descending order as per 
   * the specifications.
   * 
   * The data must be saved immediately after sorting so that it can 
   * be correctly restored onResume.
   * 
   * The adapter MUST be notified of data set change or a new sort 
   * will not be reflected in the GUI of the application.
   */
  public void sortCounters(View view){
      Collections.sort(adapter.getCounters(), new CounterModel());
      persistentData.saveData(adapter.getCounters());
      adapter.notifyDataSetChanged();
  }

}




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