Android Open Source - get-it-done Todo Activity






From Project

Back to project page get-it-done.

License

The source code is released under:

MIT License

If you think the Android project get-it-done 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.codepath.example.getitdone;
/*from w  w w . j  av  a 2  s.co m*/
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import org.apache.commons.io.FileUtils;

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

public class TodoActivity extends Activity {
  ArrayList<String> items;
  ArrayAdapter<String> itemsAdapter;
  ListView lvItems;
  private final int REQUEST_CODE = 17;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_todo);
    lvItems = (ListView) findViewById(R.id.lvItems);
    readItems();
    items = new ArrayList<String>();
    itemsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
    lvItems.setAdapter(itemsAdapter);
    setupListViewListener();
  }
  
  private void setupListViewListener() {
    lvItems.setOnItemLongClickListener(new OnItemLongClickListener() {
      @Override
      public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long rowId) {
        items.remove(position);
        itemsAdapter.notifyDataSetChanged();
        saveItems();
        return true;
      }
    });
      
    lvItems.setOnItemClickListener(new OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> parent,View view, int position, long rowID) {
        String itemText = new String(items.get(position));
        launchEditTask(itemText, position);
      }
    });
  }
  
  public void addToDoItem(View v) {
    EditText etNewItem = (EditText) findViewById(R.id.etNewItem);
    itemsAdapter.add(etNewItem.getText().toString());
    etNewItem.setText("");
    saveItems(); //write to file
  }
  
  private void readItems() {
    File filesDir = getFilesDir();
    File todoFile = new File(filesDir, "todo.txt");
    try {
      items = new ArrayList<String>(FileUtils.readLines(todoFile));
    } catch (IOException e) {
      items = new ArrayList<String>();
      e.printStackTrace();
    }
  }
  
  private void saveItems() {
    File filesDir = getFilesDir();
    File todoFile = new File(filesDir, "todo.txt");
    try {
      FileUtils.writeLines(todoFile, items);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
    
  public void launchEditTask(String itemText, int itemPosition) {
    Intent editTask = new Intent(TodoActivity.this, EditItemActivity.class);
    editTask.putExtra("taskText", itemText);
    editTask.putExtra("taskNum", itemPosition);
    startActivityForResult(editTask, REQUEST_CODE);
  }
  
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
      String newItemText = data.getExtras().getString("editedText");
      int itemNum = data.getExtras().getInt("itemNum");
      items.set(itemNum, newItemText);
      itemsAdapter.notifyDataSetChanged();
      saveItems();
    }
  }

}




Java Source Code List

com.codepath.example.getitdone.EditItemActivity.java
com.codepath.example.getitdone.TodoActivity.java