Link==> Stack Note Source Code <==Link
Splash.java:
package com.asfwan.stack.note;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class Splash extends Activity{
Intent i;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
i = new Intent();
i.setClass(this, StackNoteActivity.class);
Thread t = new Thread(){
public void run(){
try {
sleep(500);
startActivity(i);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
finish();
}
}
};
t.start();
}
}
StackNoteActivity.java:
package com.asfwan.stack.note;
import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class StackNoteActivity extends Activity implements DialogInterface.OnClickListener,OnItemClickListener{
AlertDialog ad,adTapNote;
AlertDialog.Builder builder;
EditText et;
TextView tv;
ListView lv;
ArrayAdapter
ArrayList
SQLiteDatabase db;
Cursor cursor;
boolean dbState = false;
int position;
String textForDeletion = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.stacklayout);
db = openOrCreateDatabase("StackNoteDB",0,null);
db.execSQL("create table if not exists Notes (item varchar);");
cursor = db.rawQuery("select * from Notes", null);
dbState = cursor.moveToFirst();
lv = (ListView)findViewById(R.id.listView1);
arrayList = new ArrayList
if(dbState){
while(cursor.moveToNext())
arrayList.add(0,cursor.getString(cursor.getColumnIndex("item")));
}
aa = new ArrayAdapter
lv.setAdapter(aa);
lv.setOnItemClickListener(this);
tv = (TextView)findViewById(R.id.textView1);
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ad.show();
}
});
builder = new AlertDialog.Builder(this);
et = new EditText(this);
ad = builder
.setMessage("Add a note...")
.setView(et)
.setCancelable(true)
.setPositiveButton("Add", this)
.setNegativeButton("Cancel", this)
.create();
adTapNote = (new AlertDialog.Builder(this))
.setTitle("Delete this note?")
.setPositiveButton("Yes", this)
.setNegativeButton("No", this)
.create();
}
@Override
protected void onPause() {
super.onPause();
db.close();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
db = openOrCreateDatabase("StackNoteDB", 0, null);
}
@Override
public void onClick(DialogInterface dialog, int which) {
if(which==DialogInterface.BUTTON1 && dialog == ad){
String text = et.getText().toString();
if(text.length()>0){
arrayList.add(0,text);
db.execSQL("insert into Notes values ('"+text+"')");
}
Toast.makeText(this, arrayList.size()+"", Toast.LENGTH_SHORT).show();
aa.notifyDataSetChanged();
et.setText("");
}
if(which==DialogInterface.BUTTON1 && dialog == adTapNote){
executeDeletion(true);
}
}
private void executeDeletion(boolean b) {
if(b){
arrayList.remove(position);
aa.notifyDataSetChanged();
db.execSQL("delete from Notes where item='"+textForDeletion+"'");
}
else{
textForDeletion = null;
}
}
@Override
public void onItemClick(AdapterView> arg0, View v, int pos, long arg3) {
adTapNote.show();
position = pos;
textForDeletion = ((TextView)v).getText().toString();
}
}
