Android Open Source - com.abcdabcd987.timesquared Record Activity






From Project

Back to project page com.abcdabcd987.timesquared.

License

The source code is released under:

MIT License

If you think the Android project com.abcdabcd987.timesquared 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.abcdabcd987.timesquared;
/*  ww w. j a  v a2 s  . c  o m*/
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Typeface;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

public class RecordActivity extends Activity {
    private static final String tag = "[RecordActivity]";
    private static String[] categories = {};
    private SQLiteDatabase db;
    private final DBHelper helper = new DBHelper(this, "timesquared.db", null, 3);
    private AlertDialog dialog;
    private int undoId;
    private Context appContext;
    private MenuItem actionUndo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record);

        undoId = -1;
        db = helper.getWritableDatabase();
        appContext = getApplicationContext();
        updateRecordTable();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.record, menu);
        actionUndo = menu.findItem(R.id.action_undo);
        if (undoId != -1) {
            actionUndo.setVisible(true);
        } else {
            actionUndo.setVisible(false);
        }
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_undo:
                if (undoId != -1) {
                    enableRecord(undoId);
                }
                undoId = -1;
                if (actionUndo != null) {
                    actionUndo.setVisible(false);
                }
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    public void newRecord(View view) {
        EditText thing = (EditText) findViewById(R.id.edit_thing);
        final String content = thing.getText().toString();
        if (content.equals("")) {
            return;
        }

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.category);
        if (categories.length > 0) {
            builder.setItems(categories, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface d, int which) {
                    ContentValues cv = new ContentValues();
                    cv.put("category", categories[which]);
                    cv.put("content", content);
                    _insertRecord(cv);
                }
            });
        }

        final EditText newCategory = new EditText(this);
        newCategory.setHint(this.getString(R.string.new_category));
        newCategory.setImeOptions(EditorInfo.IME_ACTION_DONE);
        newCategory.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int i, KeyEvent keyEvent) {
                if (keyEvent.getAction() == KeyEvent.ACTION_DOWN && i == KeyEvent.KEYCODE_ENTER) {
                    ContentValues cv = new ContentValues();
                    cv.put("category", newCategory.getText().toString());
                    cv.put("content", content);
                    _insertRecord(cv);
                    return true;
                }
                return false;
            }
        });
        builder.setView(newCategory);

        builder.setNegativeButton(R.string.cancel, null);
        dialog = builder.show();
    }

    public void disableRecord(int id) {
        ContentValues cv = new ContentValues();
        cv.put("status", "disabled");
        db.update("record", cv, "id=" + id, null);
        updateRecordTable();
        Toast.makeText(appContext, String.format(getString(R.string.record_deleted), id), Toast.LENGTH_SHORT).show();
    }

    public void enableRecord(int id) {
        ContentValues cv = new ContentValues();
        cv.put("status", "enabled");
        db.update("record", cv, "id=" + id, null);
        updateRecordTable();
        Toast.makeText(appContext, String.format(getString(R.string.record_undo_delete), id), Toast.LENGTH_SHORT).show();
    }

    public void updateRecordTable() {
        updateCategories();
        TableLayout table = (TableLayout) findViewById(R.id.table_record);
        table.removeAllViews();
        Cursor cursor = db.query("record", null, "status='enabled'", null, null, null, "id DESC");

        String lastDate = "";
        for (cursor.moveToFirst(); !cursor.isAfterLast(); ) {
            TableRow row = new TableRow(this);

            long oldDate = cursor.getInt(3)*1000L;
            SimpleDateFormat sdf = new SimpleDateFormat(getString(R.string.full_date_format));
            sdf.setTimeZone(TimeZone.getDefault());
            String newDate = sdf.format(oldDate);

            if (newDate.equals(lastDate)) {
                TextView tvCategory = new TextView(this);
                TextView tvContent = new TextView(this);
                ImageView ivDelete = new ImageView(this);

                tvCategory.setText(cursor.getString(1));
                tvContent.setText(cursor.getString(2));
                ivDelete.setImageDrawable(getResources().getDrawable(android.R.drawable.ic_delete));

                tvCategory.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
                tvContent.setGravity(Gravity.CENTER_VERTICAL);
                tvCategory.setPadding(10, 0, 10, 0);
                tvContent.setPadding(10, 0, 10, 0);
                ivDelete.setPadding(0, 0, 0, 0);

                final int id = cursor.getInt(0);
                ivDelete.setClickable(true);
                ivDelete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        disableRecord(id);
                        undoId = id;
                        if (actionUndo != null) {
                            actionUndo.setVisible(true);
                        }
                    }
                });

                tvCategory.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.MATCH_PARENT));
                tvContent.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT));
                ivDelete.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.MATCH_PARENT));
                row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT));

                row.addView(tvCategory);
                row.addView(tvContent);
                row.addView(ivDelete);
                cursor.moveToNext();
            } else {
                TextView tvDate = new TextView(this);
                tvDate.setTypeface(Typeface.SERIF, Typeface.BOLD);
                tvDate.setText(newDate);
                tvDate.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
                TableRow.LayoutParams params = new TableRow.LayoutParams();
                params.span = 3;
                tvDate.setLayoutParams(params);
                row.addView(tvDate);
            }
            lastDate = newDate;
            table.addView(row, TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT);
        }
    }

    public void updateCategories() {
        Cursor cursor = db.query(true, "record", new String[] {"category"}, "status='enabled'", null, null, null, null, null);
        List<String> items = new ArrayList<String>();
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            items.add(cursor.getString(0));
        }
        categories = items.toArray(new String[items.size()]);
    }

    private void _insertRecord(ContentValues cv) {
        long timestamp = System.currentTimeMillis()/1000;
        cv.put("date", timestamp);
        db.insert("record", "", cv);
        updateRecordTable();
        ((EditText) findViewById(R.id.edit_thing)).setText("");
        dialog.dismiss();
    }

}




Java Source Code List

com.abcdabcd987.timesquared.DBHelper.java
com.abcdabcd987.timesquared.RecordActivity.java