==============================activity_catalog.xml================================
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.pets.CatalogActivity">
<ImageButton
android:layout_width="70dp" android:layout_height="70dp" android:scaleType="centerCrop"
android:layout_alignParentBottom="true" android:layout_alignParentEnd="true" android:src="@drawable/kaki" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.pets.EditorActivity">
<TextView
android:layout_width="125dp"
android:layout_height="wrap_content" android:text="Overview"
android:layout_alignParentRight="true" android:layout_width="240dp"
android:layout_height="wrap_content" android:hint="Name" />
<EditText
android:id="@+id/editBreed" android:layout_marginTop="10dp" android:layout_marginLeft="5dp"
android:layout_alignParentRight="true" android:layout_below="@id/editName" android:layout_width="240dp"
android:layout_height="wrap_content" android:hint="Breed" />
<Spinner
android:id="@+id/spinnerGender"
android:layout_alignParentRight="true" android:layout_width="240dp"
android:layout_height="30sp"
android:layout_below="@id/editBreed" android:layout_marginTop="10dp"/>
<TextView
android:layout_width="125dp"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/spinnerGender" android:text="Gender"
android:textSize="20sp"
android:layout_marginLeft="5dp" android:id="@+id/textGender" />
android:id="@+id/textMeasurement" android:layout_width="125dp"
android:layout_height="wrap_content" android:layout_below="@id/textGender" android:layout_marginTop="15dp" android:text="Measurement" android:layout_marginLeft="5dp" android:textSize="20sp" />
<EditText
android:id="@+id/editWeight" android:layout_width="240dp"
android:layout_height="wrap_content" android:layout_below="@id/spinnerGender" android:layout_alignParentRight="true" android:textAlignment="textEnd"
android:hint="kg" />
</RelativeLayout>
===========================AndroidManifest.xml=================================
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android.pets">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher" android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".CatalogActivity" android:label="Pets">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> </intent-filter>
</activity>
<activity
android:name=".EditorActivity" android:label="Add a Pet"
android:parentActivityName=".CatalogActivity"> <meta-data
android:name="android.support.PARENT_ACTIVITY" android:value=".CatalogActivity"/>
</activity> </application>
</manifest>
============================menucatalog.xml========================================
package com.example.android.pets;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity; import android.os.Bundle;
import android.view.Menu; import android.view.View;
import android.widget.ImageButton;
public class CatalogActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_catalog);
IBkaki.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View view) {
Intent intent = new Intent(CatalogActivity.this, EditorActivity.class);
startActivity(intent); }
}); }
public boolean onCreateOptionMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_catalog, menu); return true;
} }
========================CatalogActivity.java========================================
0070ackage com.example.android.pets;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity; import android.os.Bundle;
import android.view.Menu; import android.view.MenuItem; import android.view.View;
import android.widget.ImageButton;
public class CatalogActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_catalog);
ImageButton IBkaki = (ImageButton) findViewById(R.id.IBkaki); IBkaki.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(CatalogActivity.this, EditorActivity.class);
startActivity(intent); }
}); }
public boolean onCreateOptionMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_catalog, menu); return true;
}
public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) {
case R.id.InsertDummyData: return true;
case R.id.DelleteAllPets: return true;
}
return super.onOptionsItemSelected(item); }
}
===========================string.xml======================================
<resources><string name="app_name">Pets</string> <string-array name="array_gender_option"> <item>Unknown</item>
<item>Male</item> <item>Female</item> </string-array>
========================menu_catalog.xml===================================
<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android"> <item
android:id="@+id/InsertDummyData" android:title="Insert Dummy Data" /> <item
android:id="@+id/DelleteAllPets" android:title="Dellete All Pets" /> </menu>
==========================menu_editor.xml==================================
<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android"> <item
android:id="@+id/action_save" android:title="Save" /> <item
android:id="@+id/action_delete" android:title="Delete" /> </menu>
=================================EditorActivity.java=========================
package com.example.android.pets;import android.support.v4.app.NavUtils;
import android.support.v7.app.AppCompatActivity; import android.os.Bundle;
import android.text.TextUtils; import android.view.Menu; import android.view.MenuItem; import android.view.View;
import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.Spinner;
public class EditorActivity extends AppCompatActivity {
private EditText editName;
private EditText editBreed;
private Spinner spinnerGender;
private EditText editWeight;
private int nGender = 0;
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_editor);
editName = (EditText) findViewById(R.id.editName); editBreed = (EditText) findViewById(R.id.editBreed); editWeight = (EditText) findViewById(R.id.editWeight); spinnerGender = (Spinner) findViewById(R.id.spinnerGender); setupSpinner();
}
private void setupSpinner() {
ArrayAdapter genderSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.array_gender_option, android.R.layout.simple_spinner_item);
genderSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1li ne);
spinnerGender.setAdapter(genderSpinnerAdapter);
spinnerGender.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String selection = (String) adapterView.getItemAtPosition(i); if (!TextUtils.isEmpty(selection)){
if (selection.equals("Male")) { nGender = 1;
} else if (selection.equals("Female")) { nGender = 2;
} else {
nGender = 0; }
} }
@Override
public void onNothingSelected(AdapterView<?> adapterView) { nGender = 0;
} });
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_editor, menu); return true;
}
public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) {
case R.id.action_save: return true;
case R.id.action_delete: return true;
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this); return true;
}
return super.onOptionsItemSelected(item); }
=====================PetContract.java==================================
package com.example.android.pets.data;import android.provider.BaseColumns;
/**
* Created by Dince on 12/10/2017. */
public final class PetContract { private PetContract() {}
public static final class PetEntry implements BaseColumns{ public final static String TABLE_NAME = "pets";
public final static String _ID =BaseColumns._ID; public final static String COLUMN_PET_NAME ="name"; public final static String COLUMN_PET_BREED ="breed"; public final static String COLUMN_PET_GENDER ="gender"; public final static String COLUMN_PET_WEIGHT ="weight";
public static final int GENDER_UNKNOWN = 0; public static final int GENDER_MALE = 1; public static final int GENDER_FEMALE = 2; }
}
================edit potongan kode di EditorActivity.java==================
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { String selection = (String) adapterView.getItemAtPosition(i);
if (!TextUtils.isEmpty(selection)){ if (selection.equals("Male")) {
nGender = PetContract.PetEntry.GENDER_MALE; } else if (selection.equals("Female")) {
nGender = PetContract.PetEntry.GENDER_FEMALE; } else {
nGender = PetContract.PetEntry.GENDER_UNKNOWN; }
} }
selanjutnya===============================
terjadi redudansi, maka hilangkan Pet.Contract menjadi:
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { String selection = (String) adapterView.getItemAtPosition(i);
if (!TextUtils.isEmpty(selection)){ if (selection.equals("Male")) { nGender = PetEntry.GENDER_MALE; } else if (selection.equals("Female")) { nGender = PetEntry.GENDER_FEMALE; } else {
nGender = PetEntry.GENDER_UNKNOWN; }
} }
bagian import juga di edit:
import com.example.android.pets.data.PetContract.PetEntry;
===================PetDbHelper.java========================================
package com.example.android.pets.data;import android.content.Context;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper;
/**
public class PetDbHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "shelter.db"; private static final int DATABASE_VERSION = 1;
public PetDbHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, null, DATABASE_VERSION); }
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) { String SQL_CREATE_PETS_TABLE = "CREATE TABLE" + PetContract.PetEntry.TABLE_NAME + "("
+ PetContract.PetEntry._ID + "INTEGER PRIMARY KEY AUTOINCREMENT, " + PetContract.PetEntry.COLUMN_PET_NAME + "TEXT NOT NULL, "
+ PetContract.PetEntry.COLUMN_PET_BREED + "TEXT, "
+ PetContract.PetEntry.COLUMN_PET_GENDER + "INTEGER NOT NULL, " + PetContract.PetEntry.COLUMN_PET_WEIGHT + "INTEGER NOT NULL DEFAULT 0);";
sqLiteDatabase.execSQL(SQL_CREATE_PETS_TABLE); }
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {