android notepad设计,android之notepad
【IT168 技术文档】这个实例主要是对SDK中的Notepadv2Solution的修改和完善,因为本人在使用该sample发现一些错误和缺陷,主要是空指针异常。(1)Notepadv1.java:
package com.google.android.demo.notepad1;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.Menu.Item;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class Notepadv1 extends ListActivity {
public static final int INSERT_ID = Menu.FIRST;
private static final int EXIT_ID = 0;
private static final int DELETE_ID = Menu.FIRST + 1;
private static final int ACTIVITY_EDIT = 1;
private static final int ACTIVITY_CREATE = 0;
private NotesDbAdapter mDbHelper;
private Cursor c;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.notepad_list);
mDbHelper = new NotesDbAdapter(this);
mDbHelper.open();
fillData();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
boolean result = super.onCreateOptionsMenu(menu);
menu.add(0, INSERT_ID, R.string.menu_insert);
menu.add(0, EXIT_ID, R.string.exit_notes);
menu.add(0, DELETE_ID, R.string.menu_delete);
return result;
}
@Override
public boolean onOptionsItemSelected(Item item) {
switch (item.getId()) {
case INSERT_ID:
createNote();
return true;
case EXIT_ID:
finish();
return true;
case DELETE_ID:
mDbHelper.deleteNote(getListView().getSelectedItemId());
fillData();
return true;
}
return super.onOptionsItemSelected(item);
}
private void createNote() {
Intent i = new Intent(this, NoteEdit.class);
startSubActivity(i, ACTIVITY_CREATE);
}
private void fillData() {
// Get all of the notes from the database and create the item list
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);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
c.moveTo(position);
Intent i = new Intent(this, NoteEdit.class);
i.putExtra(NotesDbAdapter.KEY_ROWID, id);
i.putExtra(NotesDbAdapter.KEY_TITLE, c.getString(
c.getColumnIndex(NotesDbAdapter.KEY_TITLE)));
i.putExtra(NotesDbAdapter.KEY_BODY, c.getString(
c.getColumnIndex(NotesDbAdapter.KEY_BODY)));
startSubActivity(i, ACTIVITY_EDIT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, String data, Bundle extras) {
super.onActivityResult(requestCode, resultCode, data, extras);
switch(requestCode) {
case ACTIVITY_CREATE:
if(extras == null)
break;
String title = extras.getString(NotesDbAdapter.KEY_TITLE);
String body = extras.getString(NotesDbAdapter.KEY_BODY);
mDbHelper.createNote(title, body);
fillData();
break;
case ACTIVITY_EDIT:
if(extras == null)
break;
Long rowId = extras.getLong(NotesDbAdapter.KEY_ROWID);
if (rowId != null) {
String editTitle = extras.getString(NotesDbAdapter.KEY_TITLE);
String editBody = extras.getString(NotesDbAdapter.KEY_BODY);
mDbHelper.updateNote(rowId, editTitle, editBody);
}
fillData();
break;
}
}
}
说明:
此activity主要是将为创建notepad做前期工作的,此外还做收尾工作,将从后台接受过来的数据显示出来。
图示:
(2)NoteEdit.java:
package com.google.android.demo.notepad1;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class NoteEdit extends Activity {
private EditText mTitleText;
private EditText mBodyText;
private Long mRowId;
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.note_edit); mTitleText = (EditText) findViewById(R.id.title);
mBodyText = (EditText) findViewById(R.id.body);
Button confirmButton = (Button) findViewById(R.id.confirm);
mRowId = null;
Bundle extras = getIntent().getExtras();
if (extras != null) {
String title = extras.getString(NotesDbAdapter.KEY_TITLE);
String body = extras.getString(NotesDbAdapter.KEY_BODY);
mRowId = extras.getLong(NotesDbAdapter.KEY_ROWID);
if (title != null) {
mTitleText.setText(title);
}
if (body != null) {
mBodyText.setText(body);
}
} confirmButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Bundle bundle = new Bundle();
bundle.putString(NotesDbAdapter.KEY_TITLE, mTitleText.getText().toString());
bundle.putString(NotesDbAdapter.KEY_BODY, mBodyText.getText().toString());
if (mRowId != null) {
bundle.putLong(NotesDbAdapter.KEY_ROWID, mRowId);
}
setResult(RESULT_OK, null, bundle);
finish();
}
});
}
}
说明:
接受前台的命令,开始创建notepad,并将数据传递到前台。注意if (extras != null) 可以可以防止输入数据为空时的空指针异常情况。
图示:
(3)NotesDbAdapter.java:
package com.google.android.demo.notepad1;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import java.io.FileNotFoundException;
public class NotesDbAdapter {
public static final String KEY_TITLE="title";
public static final String KEY_BODY="body";
public static final String KEY_ROWID="_id";
private static final String DATABASE_CREATE =
"create table notes (_id integer primary key autoincrement, "
+ "title text not null, body text not null);";
private static final String DATABASE_NAME = "data";
private static final String DATABASE_TABLE = "notes";
private static final int DATABASE_VERSION = 2;
private SQLiteDatabase mDb;
private final Context mCtx;
public NotesDbAdapter(Context ctx) {
this.mCtx = ctx;
}
public NotesDbAdapter open() throws SQLException {
try {
mDb = mCtx.openDatabase(DATABASE_NAME, null);
} catch (FileNotFoundException e) {
try {
mDb =
mCtx.createDatabase(DATABASE_NAME, DATABASE_VERSION, 0,
null);
mDb.execSQL(DATABASE_CREATE);
} catch (FileNotFoundException e1) {
throw new SQLException("Could not create database");
}
}
return this;
}
public void close() {
mDb.close();
}
public long createNote(String title, String body) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_TITLE, title);
initialValues.put(KEY_BODY, body);
return mDb.insert(DATABASE_TABLE, null, initialValues);
}
public boolean deleteNote(long rowId) {
return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}
public Cursor fetchAllNotes() {
return mDb.query(DATABASE_TABLE, new String[] {
KEY_ROWID, KEY_TITLE, KEY_BODY}, null, null, null, null, null);
}
public Cursor fetchNote(long rowId) throws SQLException {
Cursor result = mDb.query(true, DATABASE_TABLE, new String[] {
KEY_ROWID, KEY_TITLE, KEY_BODY}, KEY_ROWID + "=" + rowId, null, null,
null, null);
if ((result.count() == 0) || !result.first()) {
throw new SQLException("No note matching ID: " + rowId);
}
return result;
}
public boolean updateNote(long rowId, String title, String body) {
ContentValues args = new ContentValues();
args.put(KEY_TITLE, title);
args.put(KEY_BODY, body);
return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
}
}
说明:
数据库和数据库操作在里面。
(4)notepad_list.xml:
android:layout_width="wrap_content"
android:layout_height="wrap_content">
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/no_notes"/>
(5)note_edi.xml:
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:layout_width="fill_parent"
android:layout_height="wrap_content">
android:layout_height="wrap_content"
android:text="@string/title" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
android:layout_height="wrap_content"
android:text="@string/body" />
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scrollbars="vertical" />
android:text="@string/confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
(6)notes_row.xml:
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
(7)strings.xml:
Notepad v1
No Notes Yet
Add Item
Exit Notepad
Delete Item
Title
Body
Confirm
Edit Note
(8)AndroidManifest.xml:
package="com.google.android.demo.notepad1">
android notepad设计,android之notepad相关推荐
- android产品设计,Android产品设计
Android产品设计,我们深信,凝聚一群人,用心在一件事上,为客户创造价值,它必将赢得客户的足够尊重和信赖. Android产品设计, 随着Android平台的扩张,引发了Android人才荒,20 ...
- android客户端设计,Android客户端设计.ppt
Android客户端设计 图16 .android.score包下各文件说明 .android.score包存放所有的功能界面,处理各种业务逻辑,是"豹考通"客户端的核心,该包下所 ...
- android 动画设计,Android loading动画设计分析
Android loading动画设计分析 时间:2017-04-20 来源:Android开发学习网 android6.0上有了很炫酷的开机动画,实现原理是什么呢?今天我们就从Loading ...
- android app 设计,Android app 设计小结
Android app设计小结 刚开始做android UI设计和切图的时候,看了好多教程和文章,最后自己总结一下,希望能帮到大家. 一.各屏幕适配--不通的dpi及屏幕尺寸 1.专用词 dpi(像素 ...
- 音频播放器android课程设计,Android课程设计:Android音乐播放器的设计与实现
内容简介: Android课程设计:Android音乐播放器的设计与实现,共21页,7729字,附源程序等. 摘要:本文主要介绍了一个基于Andriod的音乐播放器的设计与实现.主要包括可行性分析,需 ...
- android新材料设计,android - 如何实现新材料BottomAppBar为BottomNavigationView - SO中文参考 - www.soinside.com...
解决了 基本上,而不是试图迫使菜单的资源,我需要的布局,我用这个方法,而不是,我只是把使用"空"元素作为@dglozano建议BottomAppBar内的LinearLayout. ...
- android商城界面设计,Android购物商城界面设计
工具:Android Studio activity_main.xml android:layout_width="match_parent" android:layout_hei ...
- android组件设计,Android组件化开发路由的设计实践
调研了一下目前的路由框架,ARouter(阿里的),ActivityRouter都使用了apt技术 编译时注解,个人想法是一口吃不成胖子,先做个比较实用的. VpRouter路由框架主要应用于组件化开 ...
- android model 设计,Android model层设计
model层 在开发app的过程中,不管是使用了mvp还是mvc甚至mvvm模式,model层的设计基本都是一样的,model层可以被称为数据层,它的主要任务就是为上层提供各种的数据服务,上层完全不需 ...
最新文章
- 研究人员利用非线性原理为机器人创造出类似昆虫的步态,脑机接口也可以使用...
- 数字货币 分层确定性钱包(HD Wallets)
- destoon实现调用热门关键字的方法
- flink报错org.apache.commons.cli.Option.builder
- 转机器学习系列(9)_机器学习算法一览(附Python和R代码)
- model模型php,thinkphp的model模型的设计经验总结
- SpringMVC框架----SpringMVC的自定义类型转换器
- Iphone 视图跳转方法总结
- linux eclipse memory,Linux下安装JDK和Eclipse
- 常见的SQL面试题:经典50例
- 埃加洛尔虚拟服务器,致我终将逝去的二区:新一轮大服务器实装
- mapreduce新编程实例
- 删除google网页快照方法
- 吐槽、吐槽、吐槽!!!!!!
- 呵呵,GET2017教育科技大会VIP门票,你要不要啊?
- 黑马程序员-JS基础-移动端网页特效
- win10怎么更改账户名称_如何更改电脑系统账户名称?让你的名称看起来高大上,快来学习吧...
- 错误率的计算、离散概率模型下的统计决策举例
- 0-1整数规划的LINGO求解
- CSS基础(8)- 盒模型应用