• Tidak ada hasil yang ditemukan

23000 getting database connection

N/A
N/A
Protected

Academic year: 2018

Membagikan "23000 getting database connection"

Copied!
7
0
0

Teks penuh

(1)

INSERT DUMMY DATA

* Created by Dince on 1/1/2018. */

public class PetDbHelper extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "shelterlagi.db"; private static final int DATABASE_VERSION = 1;

public PetDbHelper(Context context) {

super(context, DATABASE_NAME, null, DATABASE_VERSION); }

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);

}

<?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.petslagi.CatalogActivity"> <ImageButton

android:layout_width="70dp" android:layout_height="70dp" android:src="@drawable/kaki"

android:layout_alignParentRight="true" android:layout_alignParentBottom="true" android:scaleType="centerCrop"

android:id="@+id/IBKaki"/> <TextView

android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/text_view_pet"/> </RelativeLayout>

(2)

============================CatalogActivity.java==================

================

Tambahkan method displayDatabaseInfo()

(jika this nya merah, klik dan alt + enter)

package com.example.android.petslagi;

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

private void displayDatabaseInfo() {

PetDbHelper mDBHelper = new PetDbHelper(this);

SQLiteDatabase sqLiteDatabase = mDBHelper.getReadableDatabase();

Cursor cursor = sqLiteDatabase.rawQuery("SELECT * FROM " + PetContract.PetEntry.TABLE_NAME,

null);

public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.menu_catalog, menu); return true;

}

public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) {

(3)
(4)

INSERT DUMMY PET FROM MENU ITEM

=============================CatalogActivity.java=================

====================

Tambahkan insertPet()

public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) {

return super.onOptionsItemSelected(item); }

========================menu_catalog.xml=======================

============

Beri show action

<?xml version="1.0" encoding="utf-8"?>

<menu 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" tools:context=".CatalogActivity">

<item

public class CatalogActivity extends AppCompatActivity { private PetDbHelper mDbHelper;

@Override

(5)

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_catalog);

ImageButton IBKaki = (ImageButton) findViewById(R.id.IBKaki); IBKaki.setOnClickListener(new View.OnClickListener() {

@Override

private void displayDatabaseInfo() {

PetDbHelper mDBHelper = new PetDbHelper(this);

SQLiteDatabase sqLiteDatabase = mDBHelper.getReadableDatabase();

Cursor cursor = sqLiteDatabase.rawQuery("SELECT * FROM " + PetContract.PetEntry.TABLE_NAME,

null);

public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.menu_catalog, menu); return true;

}

public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) {

return super.onOptionsItemSelected(item); }

private void inserPet() {

SQLiteDatabase sqLiteDatabase = mDbHelper.getWritableDatabase(); ContentValues values = new ContentValues();

values.put(PetContract.PetEntry.COLUMN_PET_NAME, "Toto"); values.put(PetContract.PetEntry.COLUMN_PET_BREED, "Terrier");

values.put(PetContract.PetEntry.COLUMN_PET_GENDER, PetContract.PetEntry.GENDER_MALE); values.put(PetContract.PetEntry.COLUMN_PET_WEIGHT, 7);

(6)

import android.support.v4.app.NavUtils;

public class EditorActivity extends AppCompatActivity { private EditText editName;

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);

spinnerGender = (Spinner) findViewById(R.id.spinnerGender); editWeight = (EditText) findViewById(R.id.editSpinner); 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_1line); spinnerGender.setAdapter(genderSpinnerAdapter);

spinnerGender.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override

public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.menu_editor, menu); return true;

}

(7)

switch (item.getItemId()) { case R.id.action_save: insertPet(); return true;

case R.id.action_delete: return true;

case android.R.id.home:

NavUtils.navigateUpFromSameTask(this); return true;

}

return super.onOptionsItemSelected(item); }

private void insertPet() {

DataHelper mDBHelper = new DataHelper(this);

SQLiteDatabase sqLiteDatabase = mDBHelper.getWritableDatabase();

sqLiteDatabase.execSQL("insert into pets1 values (1, '"+editName.getText().toString()+"', '"+editBreed.getText().toString()+"',"+nGender+", "+editWeight.getText().toString()+")");

hapus(); }

private void hapus() { editWeight.setText(""); editBreed.setText(""); editName.setText(""); }

Referensi

Dokumen terkait

private static final int APPSTATUS_UNINITED = -1; private static final int APPSTATUS_INIT_APP = 0; private static final int APPSTATUS_INIT_QCAR = 1;

byte double implements private threadsafe byvalue else import protected throw case extends instanceof public throws catch false int return transient car final interface short true

Example: MixedCounter public class MixedCounter { private static int sharedCounterValue = 0; private int counterValue = 0; public int count { sharedCounterValue++; return

import java.util.*; public class DotComBust { private GameHelper helper = new GameHelper; private ArrayList dotComsList = new ArrayList; private int numOfGuesses = 0; private

import java.util.ArrayList; public class StartupBust { private GameHelper helper = new GameHelper; private ArrayList startups = new ArrayList; private int numOfGuesses = 0;

Catatan lanjutan • Jika metode public bisa di-override menjadi metode protected atau private, maka obyek subkelas tidak akan dapat merespon terhadp pemanggilan metode yang sama pada

Link with Database using String Connection Example Private Sub Button2_ClickByVal sender As System.Object, ByVal e As System.EventArgs Handles Button2.Click Try cmd = New