20.11.2016 Views

Android App Development ฉบับสมบูรณ์

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

edit book<br />

Button button_edit = (Button) dialog.findViewById(R.id.buttonEdit);<br />

button_edit.setOnClickListener(new OnClickListener() {<br />

public void onClick(View v) {<br />

if (getListAdapter().getCount() > 0) {<br />

editBook(book);<br />

dialog.dismiss();<br />

}<br />

}<br />

});<br />

//delete book<br />

Button button_delete = (Button) dialog.findViewById(R.id.buttonDelete);<br />

button_delete.setOnClickListener(new OnClickListener() {<br />

public void onClick(View v) {<br />

if (getListAdapter().getCount() > 0) {<br />

datasource.deleteBook(book); // delete book<br />

adapter.remove(book);<br />

dialog.dismiss();<br />

Toast.makeText(SQLiteDatabaseActivity.this, "Delete data succeed.",<br />

Toast.LENGTH_LONG).show();<br />

}<br />

}<br />

});<br />

// close dialog<br />

Button button_cancel = (Button) dialog.findViewById(R.id.buttonClose);<br />

button_cancel.setOnClickListener(new OnClickListener() {<br />

public void onClick(View v) {<br />

dialog.dismiss();<br />

}<br />

});<br />

}<br />

dialog.show();<br />

3. โค้ดสำหรับปรับปรุงข้อมูลเมื่อคลิกปุ่ม Update ในกรณีที่ต้องการแก้ไขข้อมูล<br />

public void editBook(final Book book) {<br />

final Dialog dialog = new Dialog(SQLiteDatabaseActivity.this);<br />

dialog.setContentView(R.layout.add_book);<br />

dialog.setTitle("Edit Book");<br />

dialog.setCancelable(true);<br />

final EditText isbn = (EditText) dialog.findViewById(R.id.editTextISBN);<br />

final EditText title = (EditText) dialog<br />

.findViewById(R.id.editTextTitle);<br />

final EditText price = (EditText) dialog<br />

.findViewById(R.id.editTextPrice);<br />

final EditText publisher = (EditText) dialog<br />

.findViewById(R.id.editTextPublisher);<br />

isbn.setText(book.getIsbn());<br />

title.setText(book.getTitle());<br />

price.setText(String.valueOf(book.getPrice()));<br />

publisher.setText(book.getPublisher());<br />

// setup button<br />

Button button_save = (Button) dialog.findViewById(R.id.buttonSave);<br />

บทที ่ 11 การพัฒนาแอพพลิเคชันทำงานกับฐานข้อมูล 237

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!