Android Open Source - androll Roll History Activity






From Project

Back to project page androll.

License

The source code is released under:

Apache License

If you think the Android project androll 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 me.cybersensei.androll;
//from w  w  w.  ja  va  2s. c om
import java.util.ArrayList;

import android.app.AlertDialog;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.ListFragment;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;

public class RollHistoryActivity extends FragmentActivity implements RollListener {
  public static final String ROLLS_KEY = "rolls";
  public static final String SIDES_POSITION_KEY = "position";
  private ArrayList<Roll> rolls = new ArrayList<Roll>();
  private ArrayAdapter<Roll> rollsAdapter;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_roll_history);
    
    //Create ArrayAdapter
    rollsAdapter = new ArrayAdapter<Roll>(this, 
        android.R.layout.simple_list_item_1);
    
    //Fill ArrayAdapter
    fillArrayAdapter();
    
    //Get the ListFragment
    ListFragment rollHistory = (ListFragment) 
        getSupportFragmentManager().findFragmentById(R.id.f_rolls_history);
    
    //Set the ListFragment's ArrayAdapter
    rollHistory.setListAdapter(rollsAdapter);
  } //onCreate
  
  @Override
  protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putSerializable(ROLLS_KEY, rolls);
  } //onSaveInstanceState
  
  @SuppressWarnings("unchecked")
  @Override
  protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    rolls = (ArrayList<Roll>) savedInstanceState.getSerializable(ROLLS_KEY);
    fillArrayAdapter();
  } //onRestoreInstanceState

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.roll_history, menu);
    return true;
  } //onCreateOptionsMenu
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.mi_clear_history:
      //Clearing rolls
      rolls.clear();
      rollsAdapter.clear();
      rollsAdapter.notifyDataSetChanged();
      return true;
    default:
      return super.onOptionsItemSelected(item);
    } //switch
  } //onOptionsItemSelected

  @Override
  public void newRoll(Roll roll) {
    //Roll
    String rollResults = roll.roll();

    //Add the roll
    rolls.add(roll);
    rollsAdapter.add(roll);    
    rollsAdapter.notifyDataSetChanged();
    
    //Show roll Dialog
    showRollDialog(rollResults);
  } //newRoll
  
  private void fillArrayAdapter() {
    //Add all the rolls to the ArrayAdapter
    for (int i = 0; i < rolls.size(); i++) {
      rollsAdapter.add(rolls.get(i));
    } //for
  } //fillArrayAdapter
  
  private void showRollDialog(String results) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(R.string.results)
    .setMessage(results)
    .setCancelable(true)
    .create().show();
  } //showRollDialog
} //class RollHistoryActivity





Java Source Code List

me.cybersensei.androll.RollFragment.java
me.cybersensei.androll.RollHistoryActivity.java
me.cybersensei.androll.RollListener.java
me.cybersensei.androll.Roll.java
me.cybersensei.androll.RollerDialogFragment.java