Back to project page AndroidSimpleTodoApp.
The source code is released under:
MIT License
If you think the Android project AndroidSimpleTodoApp listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package codepath.apps.androidsimpletodoapp; //from w w w. jav a 2s. 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.Menu; import android.view.MenuItem; 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 { protected ArrayList<String> items; protected ArrayAdapter<String> itemsAdapter; protected ListView lvItems; protected EditText etNewItem; protected final int EDIT_ITEM_REQUEST_CODE = 10; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_todo); lvItems = (ListView)findViewById(R.id.lvItems); etNewItem = (EditText)findViewById(R.id.etNewItem); deserializeItems(); itemsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items); lvItems.setAdapter(itemsAdapter); setupListViewListeners(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.todo, menu); return true; } @Override 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. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } public void onItemAdded(View v) { String itemText = etNewItem.getText().toString(); if (itemText.trim().length() > 0) { itemsAdapter.add(itemText); etNewItem.setText(""); serializeItems(); } } protected void setupListViewListeners() { lvItems.setOnItemLongClickListener(new OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long rowId) { System.out.print("Foo"); items.remove(position); itemsAdapter.notifyDataSetChanged(); serializeItems(); return true; } }); lvItems.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { editItem(position); } }); } protected void deserializeItems() { File filesDir = getFilesDir(); File todoFile = new File(filesDir, "todo.txt"); try { items = new ArrayList<String>(FileUtils.readLines(todoFile)); } catch (IOException ioe) { items = new ArrayList<String>(); ioe.printStackTrace(); } } protected void serializeItems() { File filesDir = getFilesDir(); File todoFile = new File(filesDir, "todo.txt"); try { FileUtils.writeLines(todoFile, items); } catch (IOException ioe) { ioe.printStackTrace(); } } protected void editItem(int pos) { Intent i = new Intent(TodoActivity.this, EditItemActivity.class); i.putExtra("text", items.get(pos)); i.putExtra("pos", pos); startActivityForResult(i, EDIT_ITEM_REQUEST_CODE); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK && requestCode == EDIT_ITEM_REQUEST_CODE) { String text = data.getExtras().getString("text"); int position = data.getExtras().getInt("pos"); items.set(position, text); itemsAdapter.notifyDataSetChanged(); serializeItems(); } } }