Micha Kops avatar Micha Kops committed d8d4ea0

initial import

Comments (0)

Files changed (10)

AndroidManifest.xml

+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+      package="com.hascode.android"
+      android:versionCode="1"
+      android:versionName="1.0">
+    <application android:icon="@drawable/icon" android:label="@string/app_name">
+        <activity android:label="@string/app_name" android:name=".activity.SimpleTodoActivity">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.LAUNCHER" />
+            </intent-filter>
+        </activity>
+
+    </application>
+    <uses-sdk android:minSdkVersion="7" />
+
+</manifest> 

default.properties

+# This file is automatically generated by Android Tools.
+# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
+# 
+# This file must be checked in Version Control Systems.
+# 
+# To customize properties used by the Ant build system use,
+# "build.properties", and override values to adapt the script to your
+# project structure.
+
+# Indicates whether an apk should be generated for each density.
+split.density=false
+# Project target.
+target=android-7

gen/com/hascode/android/R.java

+/* AUTO-GENERATED FILE.  DO NOT MODIFY.
+ *
+ * This class was automatically generated by the
+ * aapt tool from the resource data it found.  It
+ * should not be modified by hand.
+ */
+
+package com.hascode.android;
+
+public final class R {
+    public static final class attr {
+    }
+    public static final class drawable {
+        public static final int icon=0x7f020000;
+    }
+    public static final class id {
+        public static final int btNewTask=0x7f050004;
+        public static final int etNewTask=0x7f050003;
+        public static final int row=0x7f050001;
+        public static final int tasklist=0x7f050002;
+        public static final int widget31=0x7f050000;
+    }
+    public static final class layout {
+        public static final int main=0x7f030000;
+    }
+    public static final class string {
+        public static final int add_button_name=0x7f040001;
+        public static final int app_name=0x7f040000;
+    }
+}
Add a comment to this file

res/drawable-hdpi/icon.png

Added
New image
Add a comment to this file

res/drawable-ldpi/icon.png

Added
New image
Add a comment to this file

res/drawable-mdpi/icon.png

Added
New image

res/layout/main.xml

+<?xml version="1.0" encoding="utf-8"?>
+<RelativeLayout
+android:id="@+id/widget31"
+android:layout_width="fill_parent"
+android:layout_height="fill_parent"
+xmlns:android="http://schemas.android.com/apk/res/android"
+>
+<TableRow
+android:id="@+id/row"
+android:layout_width="fill_parent"
+android:layout_height="wrap_content"
+android:orientation="horizontal"
+android:layout_below="@+id/tasklist"
+android:layout_alignParentLeft="true"
+>
+<EditText
+android:id="@+id/etNewTask"
+android:layout_width="200px"
+android:layout_height="wrap_content"
+android:text=""
+android:textSize="18sp"
+>
+</EditText>
+<Button
+android:id="@+id/btNewTask"
+android:layout_width="wrap_content"
+android:layout_height="wrap_content"
+android:text="@+string/add_button_name"
+>
+</Button>
+</TableRow>
+<ListView
+android:id="@+id/tasklist"
+android:layout_width="fill_parent"
+android:layout_height="wrap_content"
+android:layout_alignParentTop="true"
+android:layout_alignParentLeft="true"
+>
+</ListView>
+</RelativeLayout>

res/values/strings.xml

+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+    <string name="app_name">SimpleTodos</string>
+    <string name="add_button_name">Add new todo</string>
+</resources>

src/com/hascode/android/activity/SimpleTodoActivity.java

+package com.hascode.android.activity;
+
+import java.util.List;
+
+import android.app.Activity;
+import android.os.Bundle;
+import android.util.Log;
+import android.view.View;
+import android.view.View.OnClickListener;
+import android.widget.AdapterView;
+import android.widget.ArrayAdapter;
+import android.widget.Button;
+import android.widget.EditText;
+import android.widget.ListView;
+import android.widget.TextView;
+import android.widget.AdapterView.OnItemClickListener;
+
+import com.hascode.android.R;
+import com.hascode.android.persistence.TodoProvider;
+
+public class SimpleTodoActivity extends Activity {
+	public static final String APP_TAG = "com.hascode.android.simple-todos";
+	private ListView taskView;
+	private Button btNewTask;
+	private EditText etNewTask;
+	private TodoProvider provider;
+
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see android.app.Activity#onCreate(android.os.Bundle)
+	 */
+	@Override
+	public void onCreate(Bundle bundle) {
+		super.onCreate(bundle);
+		setContentView(R.layout.main);
+		provider = new TodoProvider(this);
+		taskView = (ListView) findViewById(R.id.tasklist);
+		btNewTask = (Button) findViewById(R.id.btNewTask);
+		etNewTask = (EditText) findViewById(R.id.etNewTask);
+		btNewTask.setOnClickListener(handleNewTaskEvent);
+		renderTodos();
+	}
+
+	private OnClickListener handleNewTaskEvent = new OnClickListener() {
+		@Override
+		public void onClick(View view) {
+			Log.d(APP_TAG, "add task click received");
+			provider.addTask(etNewTask.getText().toString());
+			renderTodos();
+		}
+	};
+
+	/**
+	 * renders the task list
+	 */
+	private void renderTodos() {
+		List<String> beans = provider.findAll();
+		if (!beans.isEmpty()) {
+			Log.d(APP_TAG, String.format("%d beans found", beans.size()));
+			// render the list
+			taskView.setAdapter(new ArrayAdapter<String>(this,
+					android.R.layout.simple_list_item_1, beans
+							.toArray(new String[] {})));
+
+			// dumb item deletion onclick
+			taskView.setOnItemClickListener(new OnItemClickListener() {
+				@Override
+				public void onItemClick(AdapterView<?> parent, View view,
+						int position, long id) {
+					Log.d(APP_TAG, String.format(
+							"item with id: %d and position: %d", id, position));
+					TextView v = (TextView) view;
+					provider.deleteTask(v.getText().toString());
+					renderTodos();
+
+				}
+			});
+		} else {
+			Log.d(APP_TAG, "no tasks found");
+		}
+	}
+}

src/com/hascode/android/persistence/TodoProvider.java

+package com.hascode.android.persistence;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import android.content.ContentValues;
+import android.content.Context;
+import android.database.Cursor;
+import android.database.sqlite.SQLiteDatabase;
+import android.database.sqlite.SQLiteOpenHelper;
+import android.util.Log;
+
+import com.hascode.android.activity.SimpleTodoActivity;
+
+public class TodoProvider {
+	private static final String DB_NAME = "tasks";
+	private static final String TABLE_NAME = "tasks";
+	private static final int DB_VERSION = 1;
+	private static final String DB_CREATE_QUERY = "CREATE TABLE " + TABLE_NAME
+			+ " (id integer primary key autoincrement, title text not null);";
+
+	private SQLiteDatabase storage;
+	private SQLiteOpenHelper helper;
+
+	public TodoProvider(Context ctx) {
+		helper = new SQLiteOpenHelper(ctx, DB_NAME, null, DB_VERSION) {
+			@Override
+			public void onUpgrade(SQLiteDatabase db, int oldVersion,
+					int newVersion) {
+				db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
+				onCreate(db);
+			}
+
+			@Override
+			public void onCreate(SQLiteDatabase db) {
+				db.execSQL(DB_CREATE_QUERY);
+			}
+		};
+		storage = helper.getWritableDatabase();
+	}
+
+	public List<String> findAll() {
+		Log.d(SimpleTodoActivity.APP_TAG, "findAll triggered");
+		List<String> tasks = new ArrayList<String>();
+		Cursor c = storage.query(TABLE_NAME, new String[] { "title" }, null,
+				null, null, null, null);
+		if (c != null) {
+			c.moveToFirst();
+			while (c.isAfterLast() == false) {
+				tasks.add(c.getString(0));
+				c.moveToNext();
+			}
+			c.close();
+		}
+		return tasks;
+	}
+
+	public void addTask(String title) {
+		ContentValues data = new ContentValues();
+		data.put("title", title);
+		storage.insert(TABLE_NAME, null, data);
+	}
+
+	public void deleteTask(String title) {
+		storage.delete(TABLE_NAME, "title='" + title + "'", null);
+	}
+
+	public void deleteTask(long id) {
+		storage.delete(TABLE_NAME, "id=" + id, null);
+	}
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.