日期:2014-05-16  浏览次数:20655 次

手机数据库-------实现SQLiteOpenHelper

java:

package EX05_05.txt;

import android.R.anim;
import android.R.integer;
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 mToDoDB;
	private Cursor mCursor;
	private EditText mEditText;
	private ListView mListView;
	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_DELEST = Menu.FIRST + 2;
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		super.onOptionsItemSelected(item);
		switch (item.getItemId()) {
		case MENU_ADD:
			addTodo();
			break;

		case MENU_EDIT:
			editTodo();
			break;
		case MENU_DELEST:
			deleteTodo();
			break;
		}
		return true;
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		super.onCreateOptionsMenu(menu);
		menu.add(Menu.NONE, MENU_ADD, 0, "添加");
		menu.add(Menu.NONE, MENU_EDIT, 0, "修改");
		menu.add(Menu.NONE, MENU_DELEST, 0, "删除");
		
		return true;
	}
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        initApp();
    }

	private void initApp() {
		// TODO Auto-generated method stub
		mEditText = (EditText)findViewById(R.id.EditText01);
		mListView = (ListView)findViewById(R.id.ListView01);
		
		mToDoDB = new ToDoDB(this);
		mCursor = mToDoDB.select();
		
		SimpleCursorAdapter adapter = new SimpleCursorAdapter
			(this, android.R.layout.simple_list_item_1, 
					mCursor, new String[]{ToDoDB.FIELD_TEXT}, 
					new int[]{R.id.ListView01});
		
		mListView.setAdapter(adapter);
		
		mEditText.setOnClickListener(new EditText.OnClickListener()
		{
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mEditText.setText("");
			}
			
		});
		
		mListView.setOnItemClickListener(new ListView.OnItemClickListener()
		{

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				mCursor.moveToPosition(arg2);
				_id = mCursor.getInt(0);
				
				mEditText.setText(mCursor.getString(1)+Integer.toString(_id));
			}
			
		});
		mListView.setOnItemSelectedListener(new ListView.OnItemSelectedListener()
		{

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				SQLiteCursor sc = (SQLiteCursor)arg0.getSelectedItem();
				_id = sc.getInt(0);
				
				mEditText.setText(sc.getString(1)+Integer.toString(_id));
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
			
		});		
	}
	
	private void addTodo()
	{
		if (mEditText.getText().toString().equals("")) {
			return;
		}
		
		mToDoDB.insert(mEditText.getText().toString());
		mCursor.requery();
		mListView.invalidateViews();
		_id = 0;
		mEditText.setText("");
	}
	
	private void editTodo()
	{
		if (mEditText.getText().toString().equals("")) {
			return;
		}
		
		mToDoDB.update(_id, mEditText.getText().toString());
		mCursor.requery();
		mListView.invalidateViews();
		_id = 0;
		mEditText.setText("");
	}
	
	private void deleteTodo()
	{
		if (_id == 0) {
			return;
		}
		
		mToDoDB.delete(_id);
		mCursor.requery();
		mListView.invalidateViews();
		_id = 0;
	}
}

ToDoDB.java: