Android Open Source - Android-Counter-Application Counter List Activity






From Project

Back to project page Android-Counter-Application.

License

The source code is released under:

GNU General Public License

If you think the Android project Android-Counter-Application 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 ca.ualberta.cs.artem_counter;
//from   w  w  w.java2  s  .c om
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.lang.reflect.Type;
import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class CounterListActivity extends Activity{
  private ListView counterListView;
  private EditText newCounter;
  private CounterAdapter adapter;
  private static final String FILENAME = "file.sav";

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_counter_list);  
    counterListView = (ListView) findViewById(R.id.CountersList);
    newCounter = (EditText) findViewById(R.id.edit_message);
    //Prevent keyboard from popping up automatically
    this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
    registerForContextMenu(counterListView);


    counterListView.setOnItemClickListener(new OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> arg0, View view, int position, long index) {
        CounterList.incrementSpecificCounter(position);
        adapter.notifyDataSetChanged();
      }         
    });
  }  

  @Override
  protected void onStart() {
    super.onStart();
    CounterList.setCounterList(loadFromFile());
    adapter = new CounterAdapter(this, CounterList.getCounterList());
    //Assign custom adapter to the list
    counterListView.setEmptyView( findViewById(R.id.empty_list_view));
    counterListView.setAdapter(adapter);
  }


  private ArrayList<Counter> loadFromFile() {
    ArrayList<Counter> list = new ArrayList<Counter>();
    try {
      Gson gson = new Gson();
      FileInputStream f = openFileInput(FILENAME);
      BufferedReader r = new BufferedReader(new InputStreamReader(f));
      String json = "";
      String temp = "";
      temp = r.readLine();
      while (temp != null) {
        json = json + temp;
        temp = r.readLine();
      }
      r.close();
      f.close();
      Type type = new TypeToken<ArrayList<Counter>>() {}.getType();
      list = gson.fromJson(json, type);
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return list;
  }

  private void saveInFile() {
    try {
      Gson gson = new Gson();
      String json = gson.toJson(CounterList.getCounterList());
      FileOutputStream f = openFileOutput(FILENAME, Context.MODE_PRIVATE);
      BufferedWriter w = new BufferedWriter(new OutputStreamWriter(f)); 
      w.write(json);
      w.close();
      f.close();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }


  @Override
  protected void onPause() {
    super.onPause();
    saveInFile();
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(0,0, 0, "Sort by Count");
    menu.add(0,1, 0, "How-To");
    return super.onCreateOptionsMenu(menu);
  }


  @Override
  public boolean onOptionsItemSelected(MenuItem item)
  {
    switch(item.getItemId())
    {
    case 0:
      CounterList.sort();
      adapter.notifyDataSetChanged();
      return true;
    case 1:
      AlertDialog.Builder howTo = new AlertDialog.Builder(this);
      howTo.setTitle("Welcome to MegaCount");
      howTo.setMessage("To add a counter, use the New Counter menu at the Bottom of your screen. \n \n" +
          "To increment a counter, simply tap it. \n \n" +
          "Long Press a counter to bring up the options menu.");
      // Set an EditText view to get user input 
      final TextView dialog = new TextView(this);
      howTo.setView(dialog);
      howTo.setNegativeButton("Dismiss", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
          // Canceled.
        }
      });
      howTo.show();
      return true;
    default:
      return super.onOptionsItemSelected(item);
    }
  }

  //Context Menu for long-press on counter
  @Override
  public void onCreateContextMenu(ContextMenu menu, View v,
      ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.setHeaderTitle("Options:");  
    menu.add("Info");
    menu.add("Reset");
    menu.add("Rename");
    menu.add("Delete");
  }

  //Context Menu items
  @Override
  public boolean onContextItemSelected(MenuItem item) {
    super.onContextItemSelected(item);
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    final int temp = info.position;

    if(item.getTitle()=="Info"){
      Intent intent = new Intent(this.getBaseContext(),CounterHistoryActivity.class);
      intent.putExtra("temp", temp);
      startActivity(intent);
    }
    if(item.getTitle()=="Reset"){
      CounterList.resetSpecificCounter(temp);
      adapter.notifyDataSetChanged();
    }
    if(item.getTitle()=="Delete"){
      CounterList.deleteSpecificCounter(temp);
      adapter.notifyDataSetChanged();
    }
    if(item.getTitle()=="Rename"){
      //http://developer.android.com/guide/topics/ui/dialogs.html
      AlertDialog.Builder alert = new AlertDialog.Builder(this);
      alert.setTitle("Rename");
      alert.setMessage("Enter New Name:");
      // Set an EditText view to get user input 
      final EditText input = new EditText(this);
      alert.setView(input);

      alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
          Editable value = input.getText();
          String newName = value.toString();
          CounterList.renameSpecificCounter(temp, newName);
          adapter.notifyDataSetChanged();
        }
      });

      alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
          // Canceled.
        }
      });

      alert.show();
    }
    return true;
  }

  //Handle adding a new counter
  public void addNewCounter(View v){
    CounterList.add(newCounter.getText().toString());
    newCounter.setText("");
    saveInFile();
    //The following two lines of code were taken for a StackOverflow user Mazzy
    //http://stackoverflow.com/a/8217473
    InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
    inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
    adapter.notifyDataSetChanged();
  }

}




Java Source Code List

ca.ualberta.cs.artem_counter.CounterAdapter.java
ca.ualberta.cs.artem_counter.CounterHistoryActivity.java
ca.ualberta.cs.artem_counter.CounterHistoryFragment.java
ca.ualberta.cs.artem_counter.CounterHistory.java
ca.ualberta.cs.artem_counter.CounterListActivity.java
ca.ualberta.cs.artem_counter.CounterList.java
ca.ualberta.cs.artem_counter.Counter.java