Skip to content

Commit

Permalink
通过菜单项插入虚拟宠物
Browse files Browse the repository at this point in the history
  • Loading branch information
heyblood committed Feb 2, 2021
1 parent 65f5caf commit 40b6e83
Showing 1 changed file with 36 additions and 5 deletions.
41 changes: 36 additions & 5 deletions app/src/main/java/com/example/pets/CatalogActivity.java
Expand Up @@ -15,6 +15,7 @@
*/
package com.example.pets;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
Expand All @@ -34,6 +35,10 @@
* Displays list of pets that were entered and stored in the app.
*/
public class CatalogActivity extends AppCompatActivity {
/**
* Database helper that will provide us access to the database
*/
private PetDbHelper mDbHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -50,6 +55,10 @@ public void onClick(View view) {
}
});

// To access our database, we instantiate our subclass of SQLiteOpenHelper
// and pass the context, which is the current activity.
mDbHelper = new PetDbHelper(this);

displayDatabaseInfo();
}

Expand All @@ -67,7 +76,8 @@ public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to a click on the "Insert dummy data" menu option
case R.id.action_insert_dummy_data:
// Do nothing for now
insertPet();
displayDatabaseInfo();
return true;
// Respond to a click on the "Delete all entries" menu option
case R.id.action_delete_all_entries:
Expand All @@ -82,10 +92,6 @@ public boolean onOptionsItemSelected(MenuItem item) {
* the pets database.
*/
private void displayDatabaseInfo() {
// To access our database, we instantiate our subclass of SQLiteOpenHelper
// and pass the context, which is the current activity.
PetDbHelper mDbHelper = new PetDbHelper(this);

// Create and/or open a database to read from it
SQLiteDatabase db = mDbHelper.getReadableDatabase();

Expand All @@ -103,4 +109,29 @@ private void displayDatabaseInfo() {
cursor.close();
}
}

/**
* Helper method to insert hardcoded pet data into the database. For debugging purposes only.
*/
private void insertPet() {
// Gets the database in write mode
SQLiteDatabase db = mDbHelper.getWritableDatabase();

// Create a ContentValues object where column names are the keys,
// and Toto's pet attributes are the values.
ContentValues values = new ContentValues();
values.put(PetEntry.COLUMN_PET_NAME, "Toto");
values.put(PetEntry.COLUMN_PET_BREED, "Terrier");
values.put(PetEntry.COLUMN_PET_GENDER, PetEntry.GENDER_MALE);
values.put(PetEntry.COLUMN_PET_WEIGHT, 7);

// Insert a new row for Toto in the database, returning the ID of that new row.
// The first argument for db.insert() is the pets table name.
// The second argument provides the name of a column in which the framework
// can insert NULL in the event that the ContentValues is empty (if
// this is set to "null", then the framework will not insert a row when
// there are no values).
// The third argument is the ContentValues object containing the info for Toto.
long newRowId = db.insert(PetEntry.TABLE_NAME, null, values);
}
}

0 comments on commit 40b6e83

Please sign in to comment.