Skip to content

Commit

Permalink
创建 Contract 类
Browse files Browse the repository at this point in the history
  • Loading branch information
heyblood committed Feb 2, 2021
1 parent e8547e5 commit 113fe82
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 6 deletions.
15 changes: 9 additions & 6 deletions app/src/main/java/com/example/pets/EditorActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NavUtils;

import com.example.pets.data.PetContract.PetEntry;

/**
* Allows user to create a new pet or edit an existing one.
*/
Expand All @@ -54,10 +56,11 @@ public class EditorActivity extends AppCompatActivity {
private Spinner mGenderSpinner;

/**
* Gender of the pet. The possible values are:
* 0 for unknown gender, 1 for male, 2 for female.
* Gender of the pet. The possible valid values are in the PetContract.java file:
* {@link PetEntry#GENDER_UNKNOWN}, {@link PetEntry#GENDER_MALE}, or
* {@link PetEntry#GENDER_FEMALE}.
*/
private int mGender = 0;
private int mGender = PetEntry.GENDER_UNKNOWN;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -95,11 +98,11 @@ public void onItemSelected(AdapterView<?> parent, View view, int position, long
String selection = (String) parent.getItemAtPosition(position);
if (!TextUtils.isEmpty(selection)) {
if (selection.equals(getString(R.string.gender_male))) {
mGender = 1; // Male
mGender = PetEntry.GENDER_MALE; // Male
} else if (selection.equals(getString(R.string.gender_female))) {
mGender = 2; // Female
mGender = PetEntry.GENDER_FEMALE; // Female
} else {
mGender = 0; // Unknown
mGender = PetEntry.GENDER_UNKNOWN; // Unknown
}
}
}
Expand Down
72 changes: 72 additions & 0 deletions app/src/main/java/com/example/pets/data/PetContract.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.example.pets.data;

import android.provider.BaseColumns;


/**
* API Contract for the Pets app.
*/
public final class PetContract {

// To prevent someone from accidentally instantiating the contract class,
// give it an empty constructor.
private PetContract() {
}

/**
* Inner class that defines constant values for the pets database table.
* Each entry in the table represents a single pet.
*/
public static final class PetEntry implements BaseColumns {

/**
* Name of database table for pets
*/
public final static String TABLE_NAME = "pets";

/**
* Unique ID number for the pet (only for use in the database table).
* <p>
* Type: INTEGER
*/
public final static String _ID = BaseColumns._ID;

/**
* Name of the pet.
* <p>
* Type: TEXT
*/
public final static String COLUMN_PET_NAME = "name";

/**
* Breed of the pet.
* <p>
* Type: TEXT
*/
public final static String COLUMN_PET_BREED = "breed";

/**
* Gender of the pet.
* <p>
* The only possible values are {@link #GENDER_UNKNOWN}, {@link #GENDER_MALE},
* or {@link #GENDER_FEMALE}.
* <p>
* Type: INTEGER
*/
public final static String COLUMN_PET_GENDER = "gender";

/**
* Weight of the pet.
* <p>
* Type: INTEGER
*/
public final static String COLUMN_PET_WEIGHT = "weight";

/**
* Possible values for the gender of the pet.
*/
public static final int GENDER_UNKNOWN = 0;
public static final int GENDER_MALE = 1;
public static final int GENDER_FEMALE = 2;
}
}

0 comments on commit 113fe82

Please sign in to comment.