|
我们先在XML里把界面搭建起来。
java代码: < ?xml version="1.0" encoding="utf-8"?>
<LINEARLAYOUT xmlns:android="http://schemas.android.com/apk/res/android" androidrientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LINEARLAYOUT>
<LINEARLAYOUT
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TEXTVIEW
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="标题" />
<EDITTEXT
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LINEARLAYOUT>
<TEXTVIEW
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="内容" />
<EDITTEXT
android:id="@+id/body"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scrollbars="vertical" />
<BUTTON
android:id="@+id/confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="写好了">
</BUTTON>
图片:
main代码:
package android.lesson45;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
public class MainActivity extends ListActivity {
private static final int ACTIVITY_CREATE = 0;
private static final int ACTIVITY_EDIT = 1;
//菜单项也是需要唯一标示的所以我们定义下面的常量
public static final int INSERT_ID = Menu.FIRST;
private static final int DELETE_ID = Menu.FIRST + 1;
private DbHelper dbHelper;
private SQLiteDatabase db;
private Cursor c;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 创建一个数据库工具类
dbHelper = new DbHelper(this);
// 获得数据库对象
db = dbHelper.getWritableDatabase();
// 填充数据
fillData();
// 给ListView绑定上下文菜单
registerForContextMenu(getListView());
}
// 添加新记录
private void addNote() {
Intent i = new Intent(this, EditActivity.class);
//注意看第二个参数,它给本次请求设置了个 请求代码 requestCode作为返回标示
startActivityForResult(i, ACTIVITY_CREATE);
}
// 填充数据
private void fillData() {
// 查询全部数据,返回游标对象
c = dbHelper.fetchAll(db);
// 让Activity开始管理游标对象
startManagingCursor(c);
String[] from = new String[] { DbHelper.KEY_TITLE };
int[] to = new int[] { android.R.id.text1};
// 创建一个SimpleCursorAdapter对象
SimpleCursorAdapter sca = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, c, from, to);
// 给ListActivity设置Adapter
setListAdapter(sca);
}
// 创建Options菜单
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, INSERT_ID, 0, "新建");
return super.onCreateOptionsMenu(menu);
}
// 菜单项被选中时
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Toast.makeText(this, "Option菜单项被选中", Toast.LENGTH_SHORT).show();
switch (item.getItemId()) {
case INSERT_ID:
addNote();
return true;
}
return super.onOptionsItemSelected(item);
}
//上下文菜单被创建
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, DELETE_ID, 0, "删除");
}
// 上下文菜单选项被选择
@Override
public boolean onContextItemSelected(MenuItem item) {
Toast.makeText(this, "上下文菜单项被选中", Toast.LENGTH_SHORT).show();
switch (item.getItemId()) {
// 当删除按钮被选择时
case DELETE_ID:
// AdapterContextMenuInfo里包含ListView中选中项的信息
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
// 删除Id
dbHelper.delete(db, info.id);
// 删除以后再次填充数据
fillData();
return true;
}
return super.onContextItemSelected(item);
}
// 点击某一条记录的标题后新开个窗口去编辑这条记录
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Cursor c1 = c;
c1.moveToPosition(position);
// 新建Intent
Intent i = new Intent(this, EditActivity.class);
// 把当前记录存入Extras
i.putExtra(DbHelper.KEY_ROWID, id);
i.putExtra(DbHelper.KEY_TITLE, c1.getString(c1.getColumnIndexOrThrow(DbHelper.KEY_TITLE)));
i.putExtra(DbHelper.KEY_BODY, c1.getString(c1.getColumnIndexOrThrow(DbHelper.KEY_BODY)));
// 调用另外一个Activity,给他一个请求标记,以便返回的时候知道是谁
startActivityForResult(i, ACTIVITY_EDIT);
}
// onActivityResult方法用来接收另外一个你用startActivityForResult方法开启的窗口结束时传回来的数据
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
Bundle extras = intent.getExtras();
//看是从哪里返回的
switch (requestCode) {
//从创建记录的Activity窗口返回的
case ACTIVITY_CREATE:
String title = extras.getString(DbHelper.KEY_TITLE);
String body = extras.getString(DbHelper.KEY_BODY);
dbHelper.add(db, title, body);
fillData();
break;
//从修改记录的Activity窗口返回的
case ACTIVITY_EDIT:
Long rowId = extras.getLong(DbHelper.KEY_ROWID);
if (rowId != null) {
String editTitle = extras.getString(DbHelper.KEY_TITLE);
String editBody = extras.getString(DbHelper.KEY_BODY);
dbHelper.update(db, rowId, editTitle, editBody);
}
fillData();
break;
}
}
} |
|