Hellow Android NotePad

用的1.5版本
现在需要先启动AVD才能进行调试]
或者直接把手机连接上usb也可以进行调试 ,真爽

Notepad笔记

新建一个项目,并使用Notepadv1来创建

创建一个列表显示所有的notes
这里主要使用了ListView
在res/layout里修改notepad_list.xml为
[ccn lang="xml" tab_size="4" theme="blackboard" width="550" ]

 
 


[/ccn]
其中Listview和TextView一次只能显示一个
empty这个id是当ListAdapter没有数据的时候自动显示,或者你可以在ListView里使用setEmptyView(view)来代替此方法

在res/layout里创建一个notes_row.xml
[ccn lang="xml" tab_size="4" theme="blackboard" width="550" ]


[/ccn]
这个是用来显示一行note的数据的
这里创建了一个新的id text1在@符号之后,表示当没有数据的时候这个id会自动创建

最后修改Notepadv1.java
[ccn lang="java" tab_size="4" theme="blackboard" width="550" ]
/*
* Copyright (C) 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.android.demo.notepad1;

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SimpleCursorAdapter;

public class Notepadv1 extends ListActivity {
@SuppressWarnings("unused")
private int mNoteNumber = 1;

private NotesDbAdapter mDbHelper;

public static final int INSERT_ID = Menu.FIRST;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notepad_list);
mDbHelper = new NotesDbAdapter(this);
mDbHelper.open();
fillData();
// super.onCreate(savedInstanceState);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
boolean result = super.onCreateOptionsMenu(menu);
menu.add(0, INSERT_ID, 0, R.string.menu_insert);
return result;
// return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
// return super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case INSERT_ID:
createNote();
return true;
}
return super.onOptionsItemSelected(item);

}

private void createNote() {
String noteName = "Note " + mNoteNumber++;
mDbHelper.createNote(noteName, "");
fillData();
}

private void fillData() {
// Get all of the notes from the database and create the item list
Cursor c = mDbHelper.fetchAllNotes();
startManagingCursor(c);

String[] from = new String[] { NotesDbAdapter.KEY_TITLE };
int[] to = new int[] { R.id.text1 };

// Now create an array adapter and set it to display using our row
SimpleCursorAdapter notes = new SimpleCursorAdapter(this,
R.layout.notes_row, c, from, to);
setListAdapter(notes);
}

}
[/ccn]