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.

}<br />

String value_publisher = publisher.getText().toString();<br />

@SuppressWarnings("unchecked")<br />

ArrayAdapter adapter = (ArrayAdapter) getListAdapter();<br />

Book book = new Book();<br />

book.setIsbn(value_isbn);<br />

book.setTitle(value_title);<br />

book.setPrice(value_price);<br />

book.setPublisher(value_publisher);<br />

book = datasource.insertBook(book);<br />

adapter.add(book);<br />

dialog.cancel();<br />

}<br />

});<br />

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

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

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

dialog.cancel();<br />

}<br />

});<br />

dialog.show();<br />

2. โค้ดสำหรับแสดงรายละเอียดของข้อมูลในแต่ละรายการเมื่อคลิกที่ไอเท็ม (Item) นั้นๆ และ<br />

ภายในจะประกอบไปด้วยปุ่ม 3 ปุ่มคือ Edit, Delete และ Cancel ซึ่งเมื่อคลิกปุ่ม Edit ก็จะแสดง<br />

ไดอะล็อกบ็อกซ์สำหรับแก้ไขข้อมูล และเมื่อคลิกปุ่ม Delete ก็จะลบข้อมูลรายการนั้น<br />

@Override<br />

protected void onListItemClick(ListView l, View v, int position, long id) {<br />

// TODO Auto-generated method stub<br />

super.onListItemClick(l, v, position, id);<br />

showBookDetail(position); // View book by id<br />

}<br />

public void showBookDetail(final int id) {<br />

@SuppressWarnings("unchecked")<br />

final ArrayAdapter adapter = (ArrayAdapter) getListAdapter();<br />

final Book book = (Book) getListAdapter().getItem(id);<br />

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

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

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

dialog.setCancelable(true);<br />

TextView txt_isbn = (TextView) dialog.findViewById(R.id.textBookISBN);<br />

TextView txt_title = (TextView) dialog.findViewById(R.id.textBookTitle);<br />

TextView txt_price = (TextView) dialog.findViewById(R.id.textBookPrice);<br />

TextView txt_publisher = (TextView) dialog<br />

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

txt_isbn.setText("ISBN :\t" + book.getIsbn());<br />

txt_title.setText("Title :\t" + book.getTitle());<br />

txt_price.setText("Price :\t" + book.getPrice() + "");<br />

txt_publisher.setText("Publisher :\t" + book.getPublisher());<br />

236<br />

<strong>Android</strong> <strong>App</strong> <strong>Development</strong> <strong>ฉบับสมบูรณ์</strong>

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

Saved successfully!

Ooh no, something went wrong!