Android Open Source - android-todo To Do Activity






From Project

Back to project page android-todo.

License

The source code is released under:

Eclipse Public License - v 1.0 THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM CONSTITUTES RECI...

If you think the Android project android-todo 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.gemmakbarlow.todo;
/*from   w w w. j  av a 2s  . c om*/
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import org.apache.commons.io.FileUtils;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class ToDoActivity extends Activity {
  private ArrayList<String> todoItems;

  /*
   *  Adapter allows for the population of items in any kind of list view in Android
   *  Translator between a piece of data & it's view representation.
   */  
  private ArrayAdapter<String> aTodoItems; // aTodoItems follows a naming convention for adapters 

  private ListView lvItems;
  private EditText etNewItem;
  protected static final String persistentFilename = "todo.txt";

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

    readItems();
    
    lvItems = (ListView)findViewById(R.id.lvItems);
    todoItems = new ArrayList<String>();
    aTodoItems = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1, todoItems);
    lvItems.setAdapter(aTodoItems);
    etNewItem = (EditText)findViewById(R.id.etNewItem);
        
    //populateArrayItems();
    setupViewListener();
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.to_do, menu);
    return true;
  }

  public void addToDoItem(View v) {
    String newItemText = etNewItem.getText().toString();
    if(newItemText.trim().length() > 0) {
      aTodoItems.add(newItemText);
      etNewItem.setText("");
      writeItems();
    }
  }


//  private void populateArrayItems() {
//    todoItems.add("Gemma's Awesome First Item");
//    todoItems.add("Gemma's Awesome Second Item");
//  }
  
  private void setupViewListener() {
    lvItems.setOnItemLongClickListener(new OnItemLongClickListener() {
      @Override
      public boolean onItemLongClick(AdapterView<?> aView, View item,
          int index, long id) {
        todoItems.remove(index);
        aTodoItems.notifyDataSetInvalidated();
        writeItems();
        return true;
      }
      
    });
  }
  
  private void readItems() {
    File filesBaseDirectory = getFilesDir();
    File persistentToDoFile = new File(filesBaseDirectory, ToDoActivity.persistentFilename);
    
    try {
      todoItems = new ArrayList<String>(FileUtils.readLines(persistentToDoFile));
    } catch (IOException e) {
      todoItems = new ArrayList<String>();
    }
  }
  
  private void writeItems() {
    File fileBaseDirectory = getFilesDir();
    File persistentToDoFile = new File (fileBaseDirectory, ToDoActivity.persistentFilename);
    
    try {
      FileUtils.writeLines(persistentToDoFile, todoItems);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}




Java Source Code List

com.gemmakbarlow.todo.ToDoActivity.java