[连载 6/15] Android 从入门到精通Example 之 SQLite Database Example 2 - Extended Version

SQLite Database Example 1 的扩展,功能与前一个例子一样,增加了不同GUI 控件的调用.

 

1. RadioCheckDrop.java

public class RadioCheckDrop extends ListActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener { private TextView textArea1; private EditText field1; private EditText field2; private RadioGroup radioGroup; private Spinner spinner; private RadioButton r1, r2, r3; private CheckBox c1, c2, c3; private int radioChoice = -1; private static final String[] dropDownList = { "A", "B", "C", "D", "E", "F" }; public static String newline = System.getProperty("line.separator"); private SQLiteDatabase db; private static final String TAG = "DBExample"; private static final String DBNAME = "sample1"; private static final String TABLE1NAME = "table1"; private static final String CREATE_TABLE1 = "create table if not exists " + " table1 (id integer primary key autoincrement, " + " text1 text not null, text2 text not null);"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); field1 = (EditText) findViewById(R.id.field1); field2 = (EditText) findViewById(R.id.field2); radioGroup = (RadioGroup) findViewById(R.id.radiogroup1); radioGroup.setOnCheckedChangeListener(this); r1 = (RadioButton) findViewById(R.id.radio1); r2 = (RadioButton) findViewById(R.id.radio2); r3 = (RadioButton) findViewById(R.id.radio3); c1 = (CheckBox) findViewById(R.id.check1); c2 = (CheckBox) findViewById(R.id.check2); c3 = (CheckBox) findViewById(R.id.check3); Button saveButton = (Button) findViewById(R.id.button1); saveButton.setOnClickListener(this); Button deleteButton = (Button) findViewById(R.id.delbutton); deleteButton.setOnClickListener( new View.OnClickListener() { public void onClick(View v) { deleteAllRows(); } }); Button showAllButton = (Button) findViewById(R.id.showallbutton); showAllButton.setOnClickListener( new View.OnClickListener() { public void onClick(View v) { showAllValues(); } }); spinner = (Spinner) findViewById(R.id.spinner1); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, dropDownList); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); textArea1 = (TextView) findViewById(R.id.textarea1); openDatabase(); } private void openDatabase() { try { db = openOrCreateDatabase(DBNAME, MODE_WORLD_WRITEABLE, null); db.execSQL(CREATE_TABLE1); } catch (Exception e){ Log.i(TAG, "Error in opening or creating new DB. " + e); } } private boolean insertRow(String value1, String value2) { ContentValues values = new ContentValues(); values.put("text1", value1); values.put("text2", value2); return (db.insert(TABLE1NAME, null, values) > 0); } private boolean deleteAllRows() { boolean SUCCESS = db.delete(TABLE1NAME, "id>=0", null) > 0; //Example of a raw query. //After deletion, making a query to double check. Should have no matching results. Cursor c; c = db.rawQuery("select * from table1 order by id", null); c.moveToFirst(); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1); for(int i = 0; i < c.getCount(); i++) { adapter.add(c.getString(1)); c.moveToNext(); } setListAdapter(adapter); if (SUCCESS) { textArea1.setText("Deleted All Values Successfully!"); } return (SUCCESS); } public List<Table1Items> getItems() { ArrayList<Table1Items> dbRows = new ArrayList<Table1Items>(); try { Cursor c = db.query(TABLE1NAME, new String[] { "id", "text1", "text2" }, null, null, null, null, null); int numRows = c.getCount(); c.moveToFirst(); for (int i = 0; i < numRows; ++i) { Table1Items item = new Table1Items(); item.id = c.getLong(0); item.text1 = c.getString(1); item.text2 = c.getString(2); dbRows.add(item); c.moveToNext(); } } catch (Exception e) { Log.e(TAG, "Error in getting items from db: " + e); } return dbRows; } /* * clicked on button "Insert Values" */ public void onClick(View arg0) { boolean SUCCESS = false; SUCCESS = insertRow(field1.getText().toString(), field2.getText().toString()); field1.setText(""); field2.setText(""); //a radio value is selected -- now insert into db switch (radioChoice) { case (R.id.radio1): SUCCESS = SUCCESS && insertRow("Radio", "R1: "+r1.getText()); break; case (R.id.radio2): SUCCESS = SUCCESS && insertRow("Radio", "R2: "+r2.getText()); break; case (R.id.radio3): SUCCESS = SUCCESS && insertRow("Radio", "R3: "+r3.getText()); break; default: break; } if (c1.isChecked()) { SUCCESS = SUCCESS && insertRow("Check", "C1: "+c1.getText()); } if (c2.isChecked()) { SUCCESS = SUCCESS && insertRow("Check", "C2: "+c2.getText()); } if (c3.isChecked()) { SUCCESS = SUCCESS && insertRow("Check", "C3: "+c3.getText()); } SUCCESS = SUCCESS && insertRow("SelectBox", "opt: "+dropDownList[spinner.getSelectedItemPosition()]); if (SUCCESS) { textArea1.setText("All values inserted successfully!"); } } private void showAllValues() { textArea1.setText("Showing all values in DB"); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1); ArrayList<Table1Items> tableItems = (ArrayList<Table1Items>) getItems(); for (Iterator<Table1Items> it = tableItems.iterator(); it.hasNext(); ) { Table1Items item = it.next(); adapter.add(item.toString()); } setListAdapter(adapter); } /*** for the Radio checked event ***/ public void onCheckedChanged(RadioGroup arg0, int checkedId) { textArea1.setText("clicked radio button: "+checkedId); radioChoice = checkedId; } }

 

2. Table1Items

public class Table1Items extends Object{ public long id; public String text1; public String text2; public String toString() { return ("ID: " + id + " Text1: " + text1 + " Text2: " + text2); } }

你可能感兴趣的:([连载 6/15] Android 从入门到精通Example 之 SQLite Database Example 2 - Extended Version)