com.example.dididoit.app.MainActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.example.dididoit.app.MainActivity.java

Source

/*
 * Did I Do It, a simple app to track daily recurring to-dos.
 *
 * Copyright (c) 2014 Adam Howard.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.example.dididoit.app;

import android.app.ListActivity;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.Editable;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends ListActivity implements NewItemDialogFragment.NewItemDialogListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: load saved adapter state instead of always creating a new one
        ArrayAdapter<Model> adapter = new DoItAdapter(this, new ArrayList<Model>());
        TextView footer = makeFooter();
        getListView().addFooterView(footer);
        setListAdapter(adapter);
    }

    private TextView makeFooter() {
        TextView foot = new TextView(this);
        foot.setText("Add new item");
        foot.setClickable(true);
        foot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogFragment dialog = new NewItemDialogFragment();
                dialog.show(dialog.getFragmentManager(), "NewItemDialogFragment");
            }
        });
        return foot;
    }

    @Override
    public void onDialogCancelClick(DialogFragment dialog) {
        // nothing to do when user cancels new item
    }

    @Override
    public void onDialogDoneClick(DialogFragment dialog) {
        Editable newItemText = ((EditText) dialog.getDialog().findViewById(R.id.item_name)).getText();
        String newItem;
        // TODO: this should be handled more gracefully by only enabling done button when there
        // is text
        if (null == newItemText) {
            return;
        } else {
            newItem = newItemText.toString();
        }
        // TODO: add to adapter
    }

    // TODO: add a way to delete items (swipe?)

}