Android Open Source - Books History Activity

From Project

Back to project page Books.


The source code is released under:

Apache License

If you think the Android project Books 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.contender.books;
//  ww w .  j av a 2  s  . c  o  m
import java.util.Date;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.database.Cursor;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

 * Implements a ListView that connects to the {@link BooksStorage#HISTORY_TABLE_NAME}
 * table and displays it. 
 * <p>
 * A very basic View that just shows the books deleted from the main books table, but
 * provides no interaction with them.
 * @author Paul Klinkenberg <>
 * @version {@value com.contender.books.MainView#BOOKS_VERSION}
public class HistoryActivity extends Activity {

  private static final String TAG = "HistoryActivity";
  private ListView listView;
  private Cursor mCursor;
  BaseAdapter adapter;

  protected void onCreate(Bundle savedInstanceState) {

   * Text converter implemented by the ListView
   * <p>
   * if the processed field is {@link} returns a string
   * to display time the formatted return(deletion) date.
  private String convText(TextView v, String text) {
    if(v.getId() == {

      Date returnDate = new Date(Long.parseLong(text));
      CharSequence format = DateFormat.format("MMM d, yyyy", returnDate);

      return format.toString();
    return text;

   * Initializes this view, loading the data from the DB and setting the sortorder.
  public void onResume() {

    SharedPreferences settings = this.getSharedPreferences(MainView.PREFS_NAME, 0);
    int sortOrder = settings.getInt(MainView.PREFS_SORT_ORDER, 0);
    String sortOrderString;
    switch (sortOrder) {
    case 0:
      sortOrderString = new String(BooksStorage.COLUMN_NAME_RETURNDATE);
    case 1:
      sortOrderString = new String(BooksStorage.COLUMN_NAME_BOOK);
    case 2:
      sortOrderString = new String(BooksStorage.COLUMN_NAME_CONTACT);
      sortOrderString = null;

    mCursor = MainView.BooksProvider.query(BooksStorage.HISTORY_TABLE_NAME, new String[] { BooksStorage.COLUMN_NAME__ID, BooksStorage.COLUMN_NAME_BOOK,
        BooksStorage.COLUMN_NAME_CONTACT, BooksStorage.COLUMN_NAME_RETURNDATE }, null, null, sortOrderString);
    if(mCursor == null) {
      Log.e(TAG, "Received empty cursor");
    adapter = new SpecialCursorAdapter(this, R.layout.overview_item, mCursor, 
        new String[] { BooksStorage.COLUMN_NAME_BOOK, BooksStorage.COLUMN_NAME_CONTACT, BooksStorage.COLUMN_NAME_RETURNDATE }, 
        new int[] {,, }, 0) {
      public void setViewText(TextView v, String text) {
        super.setViewText(v,  convText(v, text));;
    listView = (ListView) findViewById(;
  public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(, menu);
    return true;

  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.
    Intent intent;

    switch(item.getItemId()) {
      intent = new Intent(getApplicationContext(), SettingsActivity.class);
      return true;
      AlertDialog.Builder builder = new AlertDialog.Builder(this);
      Resources res = getResources();
      String aboutMessage = res.getString(R.string.about_message, MainView.BOOKS_VERSION);
      AlertDialog aboutDialog = builder.create();;
      TextView view = (TextView) aboutDialog.findViewById(;
      return true;
    return super.onOptionsItemSelected(item);


Java Source Code List