Android Open Source - GasTracker History






From Project

Back to project page GasTracker.

License

The source code is released under:

Copyright 2014 kurtzy317

If you think the Android project GasTracker 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.mindalsoblown.gastracker;
//  w ww .j a  va  2 s. c om
import java.io.FileNotFoundException;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class History extends ActionBarActivity {
  private DatabaseConnector connection;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_history);

    if (savedInstanceState == null) {
      getSupportFragmentManager().beginTransaction()
          .add(R.id.container, new PlaceholderFragment()).commit();
      
      
    }
    //initialize database connection
    try {
      connection = new DatabaseConnector(openFileInput("ID"));
    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.history, menu);
    
    //get list on window and create an array adapter for that list
    ListView lstHistory  = (ListView)findViewById(R.id.lstHistory);
    ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
    
    //get entries from database
    final List<Entry> entries = connection.getHistoryForUser();
    
    //add entries date to list
    for (Entry e : entries)
    {
        listAdapter.add(e.getDate().toString());
    }
    if (entries.size() == 0)
    {
      listAdapter.add("No Entries");
    }
    
    //set adapter to list
    lstHistory.setAdapter(listAdapter);
    
    //on click store entry ID in intent and open the detail window
    lstHistory.setOnItemClickListener(new OnItemClickListener(){

      @Override
      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
          long arg3) {
          Intent intent = new Intent(History.this, FillUpDetail.class);
          //since list is reordered in selection screen select ID from back of list
          //intent.putExtra("entryId", entries.get(entries.size() - arg2 - 1).getId());
          intent.putExtra("entryId", entries.get(arg2).getId());
            startActivity(intent);
      }
    });
    
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
      return true;
    }
    return super.onOptionsItemSelected(item);
  }

  /**
   * A placeholder fragment containing a simple view.
   */
  public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
      View rootView = inflater.inflate(R.layout.fragment_history,
          container, false);
      return rootView;
    }
  }

}




Java Source Code List

com.mindalsoblown.gastracker.AddEntry.java
com.mindalsoblown.gastracker.DatabaseConnector.java
com.mindalsoblown.gastracker.Entry.java
com.mindalsoblown.gastracker.FillUpDetail.java
com.mindalsoblown.gastracker.History.java
com.mindalsoblown.gastracker.Login.java
com.mindalsoblown.gastracker.MainMenu.java
com.mindalsoblown.gastracker.Register.java
com.mindalsoblown.gastracker.Stats.java
com.mindalsoblown.gastracker.User.java