Android数据库的增删改查

Android数据库的增删改查操作,稍微修改下就可以做成一个项目。

数据库的增删改查

package irdc.ex05_05;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteCursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class EX05_05 extends Activity {
	private ToDoDB myToDoDB;
	private Cursor myCursor;
	private ListView myListView;
	private EditText myEditText;
	private int _id;
	protected final static int MENU_ADD = Menu.FIRST;
	protected final static int MENU_EDIT = Menu.FIRST + 1;
	protected final static int MENU_DELETE = Menu.FIRST + 2;

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		switch (item.getItemId()) {
		case MENU_ADD:
			this.addTodo();
			break;
		case MENU_EDIT:
			this.editTodo();
			break;
		case MENU_DELETE:
			this.deleteTodo();
			break;
		}
		return true;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(Menu.NONE, MENU_ADD, 0, R.string.strAddButton);
		menu.add(Menu.NONE, MENU_EDIT, 0, R.string.strEditButton);
		menu.add(Menu.NONE, MENU_DELETE, 0, R.string.strDeleteButton);

		return true;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		myListView = (ListView) this.findViewById(R.id.myListView);
		myEditText = (EditText) this.findViewById(R.id.myEditText);

		myToDoDB = new ToDoDB(this);
		myCursor = myToDoDB.select();

		SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
				R.layout.list, myCursor, new String[] { ToDoDB.FIELD_TEXT },
				new int[] { R.id.listTextView1 });
		myListView.setAdapter(adapter);

		myListView
				.setOnItemClickListener(new AdapterView.OnItemClickListener() {
					@Override
					public void onItemClick(AdapterView arg0, View arg1,
							int arg2, long arg3) {
						myCursor.moveToPosition(arg2);
						_id = myCursor.getInt(0);
						myEditText.setText(myCursor.getString(1));
					}
				});
		myListView
				.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
					@Override
					public void onItemSelected(AdapterView arg0, View arg1,
							int arg2, long arg3) {
						SQLiteCursor sc = (SQLiteCursor) arg0.getSelectedItem();
						_id = sc.getInt(0);
						myEditText.setText(sc.getString(1));
					}
					@Override
					public void onNothingSelected(AdapterView arg0) {

					}
				});
	}

	private void addTodo() {
		if (myEditText.getText().toString().equals(""))
			return;
		myToDoDB.insert(myEditText.getText().toString());
		myCursor.requery();
		myListView.invalidateViews();
		myEditText.setText("");
		_id = 0;
	}

	private void editTodo() {
		if (myEditText.getText().toString().equals(""))
			return;
		myToDoDB.update(_id, myEditText.getText().toString());
		myCursor.requery();
		myListView.invalidateViews();
		myEditText.setText("");
		_id = 0;
	}

	private void deleteTodo() {
		if (_id == 0)
			return;
		myToDoDB.delete(_id);
		myCursor.requery();
		myListView.invalidateViews();
		myEditText.setText("");
		_id = 0;
	}
}

数据库的类

package irdc.ex05_05;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteCursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class EX05_05 extends Activity {
	private ToDoDB myToDoDB;
	private Cursor myCursor;
	private ListView myListView;
	private EditText myEditText;
	private int _id;
	protected final static int MENU_ADD = Menu.FIRST;
	protected final static int MENU_EDIT = Menu.FIRST + 1;
	protected final static int MENU_DELETE = Menu.FIRST + 2;

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		switch (item.getItemId()) {
		case MENU_ADD:
			this.addTodo();
			break;
		case MENU_EDIT:
			this.editTodo();
			break;
		case MENU_DELETE:
			this.deleteTodo();
			break;
		}
		return true;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(Menu.NONE, MENU_ADD, 0, R.string.strAddButton);
		menu.add(Menu.NONE, MENU_EDIT, 0, R.string.strEditButton);
		menu.add(Menu.NONE, MENU_DELETE, 0, R.string.strDeleteButton);

		return true;
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		myListView = (ListView) this.findViewById(R.id.myListView);
		myEditText = (EditText) this.findViewById(R.id.myEditText);

		myToDoDB = new ToDoDB(this);
		myCursor = myToDoDB.select();

		SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
				R.layout.list, myCursor, new String[] { ToDoDB.FIELD_TEXT },
				new int[] { R.id.listTextView1 });
		myListView.setAdapter(adapter);

		myListView
				.setOnItemClickListener(new AdapterView.OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView arg0, View arg1,
							int arg2, long arg3) {
						myCursor.moveToPosition(arg2);
						_id = myCursor.getInt(0);
						myEditText.setText(myCursor.getString(1));
					}

				});
		myListView
				.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
					@Override
					public void onItemSelected(AdapterView arg0, View arg1,
							int arg2, long arg3) {
						/* getSelectedItem�Ҩ�o���OSQLiteCursor */
						SQLiteCursor sc = (SQLiteCursor) arg0.getSelectedItem();
						_id = sc.getInt(0);
						myEditText.setText(sc.getString(1));
					}
					@Override
					public void onNothingSelected(AdapterView arg0) {

					}
				});
	}

	private void addTodo() {
		if (myEditText.getText().toString().equals(""))
			return;
		myToDoDB.insert(myEditText.getText().toString());
		myCursor.requery();
		myListView.invalidateViews();
		myEditText.setText("");
		_id = 0;
	}

	private void editTodo() {
		if (myEditText.getText().toString().equals(""))
			return;
		myToDoDB.update(_id, myEditText.getText().toString());
		myCursor.requery();
		myListView.invalidateViews();
		myEditText.setText("");
		_id = 0;
	}

	private void deleteTodo() {
		if (_id == 0)
			return;
		myToDoDB.delete(_id);
		myCursor.requery();
		myListView.invalidateViews();
		myEditText.setText("");
		_id = 0;
	}

}

主界面

暂无评论
  • 1:请一针见血的评论。
  • 2:评论需要审核通过后才能显示。
  • 3:评论字数限制在1000字以内。
  • 当前字数:0
热门文章
推荐文章
随机文章
关于本站 - 广告服务 - 版权声明 - 联系我们 - 友情链接 - 网站地图 - 帮助中心