這是咱們鬧鐘記事本的主界面
主界面的佈局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg"
android:orientation="vertical"
android:paddingLeft="3dp"
android:paddingRight="3dp" >
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="100px"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:text="歡迎使用--->>>鬧鐘記事--->>>"
android:textColor="#0000CD" >
</TextView>
<TextView
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="這是一款帶鬧鐘提醒功能的記事本軟件,但願能爲你帶來全新的體驗,請點擊 Menu鍵顯示選項菜單開始記事" />
</LinearLayout>
主界面的代碼
public class MainActivity extends ListActivity {
/** Called when the activity is first created. */
private SQLiteDatabase db;
private String noteId;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
// 調用onStart(),刷新頁面
protected void onStart() {
query();
super.onStart();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater flater = getMenuInflater();
flater.inflate(R.menu.menu1, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
int menuId = item.getItemId();
switch (menuId) {
// 新建
case R.id.news:
Intent intent = new Intent(MainActivity.this, NextActivity.class);
startActivity(intent);
break;
case R.id.delete:
// 刪除
if (noteId != null) {
db.delete(Notes.TABLENAME, Notes._ID + "=?",
new String[] { noteId });
Toast.makeText(MainActivity.this, "刪除成功!!", Toast.LENGTH_LONG)
.show();
query();
} else {
Toast.makeText(MainActivity.this, "無記錄!!", Toast.LENGTH_LONG)
.show();
}
break;
}
return super.onMenuItemSelected(featureId, item);
}
public void query() {
// 建立數據庫
db = openOrCreateDatabase(Notes.DBNAME, Context.MODE_PRIVATE, null);
int version = db.getVersion();
if (version < 1) {
db.execSQL("create table " + Notes.TABLENAME + "(" + Notes._ID
+ " integer primary key," + Notes.TITLE + " text,"
+ Notes.CONTENT + " text," + Notes.LTIME + " text)");
db.setVersion(1);
Toast.makeText(MainActivity.this, "數據庫建立成功!!", Toast.LENGTH_LONG)
.show();
}
// 查詢表信息
Cursor cursor = db.query(Notes.TABLENAME, new String[] { Notes._ID,
Notes.TITLE }, null, null, null, null, null);
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
// 提供數據
if (cursor != null) {
while (cursor.moveToNext()) {
HashMap<String, String> map = new HashMap<String, String>();
String noteId = cursor.getString(cursor
.getColumnIndex(Notes._ID));
String ltime = cursor.getString(cursor
.getColumnIndex(Notes.TITLE));
map.put("noteId", noteId);
map.put("ltime", ltime);
list.add(map);
}
// 適配器
SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, list,
R.layout.list, new String[] { "noteId", "ltime" },
new int[] { R.id.ids, R.id.ltime });
setListAdapter(adapter);
} else {
Toast.makeText(MainActivity.this, "暫無記錄!!", Toast.LENGTH_LONG)
.show();
}
ListView lv = getListView();
// 選擇監聽器
lv.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
TextView txtId = (TextView) arg1.findViewById(R.id.ids);
noteId = txtId.getText().toString();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
// 點擊監聽器
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
TextView txtId = (TextView) arg1.findViewById(R.id.ids);
noteId = txtId.getText().toString();
Cursor cursor = db.query(Notes.TABLENAME,
new String[] { Notes.CONTENT }, Notes._ID + "=?",
new String[] { noteId }, null, null, null);
if (cursor.moveToNext()) {
String content1 = cursor.getString(cursor
.getColumnIndex(Notes.CONTENT));
Bundle extras = new Bundle();
extras.putString("content1", content1);
extras.putString("noteId", noteId);
Intent intent1 = new Intent(MainActivity.this,
NextActivity.class);
intent1.putExtras(extras);
startActivity(intent1);
}
}
});
}
}