20.11.2016 Views

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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

public class SQLiteDatabaseActivity extends ListActivity {<br />

final static String TAG = "SQLite";<br />

private BookDataSource datasource;<br />

List values;<br />

@Override<br />

public void onCreate(Bundle savedInstanceState) {<br />

super.onCreate(savedInstanceState);<br />

setContentView(R.layout.main);<br />

Button add_book = (Button) findViewById(R.id.buttonAdd);<br />

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

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

addNewBook();<br />

}<br />

});<br />

datasource = new BookDataSource(this);<br />

datasource.open();<br />

showAllBook();// show all book.<br />

}<br />

public void showAllBook() {<br />

values = datasource.getAllBook();<br />

ArrayAdapter adapter = new ArrayAdapter(this,<br />

android.R.layout.simple_list_item_1, values);<br />

setListAdapter(adapter);<br />

}<br />

2<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 />

232<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!