i have a problem when startup a program
i want to know how to use arab_book() function from TableBook class this is table class
package info.androidhive.slidingmenu;
public class Book {
private int id;
private String title;
private String author;
public Book() {}
public Book(String title, String auther) {
this.title = title;
this.author = auther;
}
// ---- setter
public void setId(int id){
this.id = id;
}
public void setTitle(String title){
this.title = title;
}
public void setAuthor(String author){
this.author = author;
}
// --- getter ---
public int getId(){
return id;
}
public String getTitle(){
return title;
}
public String getAuthor(){
return author;
}
public String toString(){
return "Book >> id:"+id+" | title:"+title+" | author:"+author;
}
}
and this is BookTable
package info.androidhive.slidingmenu;
import java.util.LinkedList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class BookTable extends SQLiteOpenHelper {
private static final int DB_VERSION = 1;
private static final String DB_NAME = "Book";
//Book Table name
private static final String TABLE_BOOK = "books";
//Book Table Columns name
private static final String KEY_ID = "id";
private static final String KEY_TITLE = "title";
private static final String KEY_AUTHER = "author";
private static final String[] COLUMNS = {KEY_ID, KEY_TITLE, KEY_AUTHER};
public BookTable(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
// SQL statement to create book table
String CREATE_BOOK_TABLE = "CREATE TABLE books ( " +
"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"title TEXT, "+
"author TEXT )";
// create books table
db.execSQL(CREATE_BOOK_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS books");
// create fresh books table
this.onCreate(db);
}
/*
* --------------------------------------------------------------------------------
*/
public void addBook(Book book) {
Log.d("addBook", book.toString());
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_TITLE, book.getTitle());
values.put(KEY_AUTHER, book.getAuthor());
db.insert(TABLE_BOOK, null, values);
db.close();
}
public Book arab_book(String id){
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.query(TABLE_BOOK, COLUMNS, KEY_ID, new String[] {id}, null, null, null);
if( c != null )
c.moveToFirst();
Book book = new Book();
book.setId(Integer.parseInt(c.getString(0)));
book.setTitle(c.getString(1));
book.setAuthor(c.getString(2));
return book;
}
public Book getBook(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.query(TABLE_BOOK, COLUMNS, KEY_ID, new String[] {String.valueOf(id)}, null, null, null, null);
if( c != null )
c.moveToFirst();
Book book = new Book();
book.setId(c.getInt(c.getColumnIndex(KEY_ID)));
book.setTitle((c.getString(c.getColumnIndex(KEY_TITLE))));
book.setAuthor(c.getString(c.getColumnIndex(KEY_AUTHER)));
//Log.d("GetBook("+id+")", book.toString());
return book;
}
// Get All Books
public Book[] getAllBooks() {
Book list[] = null;
// 1. build the query
String query = "SELECT * FROM " + TABLE_BOOK;
// 2. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
System.out.println("Count of data in Book table >>"+cursor.getCount());
list = new Book[cursor.getCount()];
// 3. go over each row, build book and add it to list
Book book = null;
int index = 0;
if (cursor.moveToFirst()) {
do {
book = new Book();
book.setId(Integer.parseInt(cursor.getString(0)));
book.setTitle(cursor.getString(1));
book.setAuthor(cursor.getString(2));
// Add book to books
list[index]= book;
System.out.println("Book "+index+": ");
index++;
} while (cursor.moveToNext());
}
/*
//Log.d("getAllBooks()", books.toString());
for(int i=0; i<list.length; i++){
System.out.println("Book "+i+": "+list[i].getTitle());
}*/
// return books
return list;
}
// Updating single book
public int updateBook(Book book) {
// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
// 2. create ContentValues to add key "column"/value
ContentValues values = new ContentValues();
values.put("title", book.getTitle()); // get title
values.put("author", book.getAuthor()); // get author
// 3. updating row
int i = db.update(TABLE_BOOK, //table
values, // column/value
KEY_ID+" = ?", // selections
new String[] { String.valueOf(book.getId()) }); //selection args
// 4. close
db.close();
return i;
}
// Deleting single book
public void deleteBook(Book book) {
// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
// 2. delete
db.delete(TABLE_BOOK,
KEY_ID+" = ?",
new String[] { String.valueOf(book.getId()) });
// 3. close
db.close();
Log.d("deleteBook", book.toString());
}
}
and this is adapter class
package info.androidhive.slidingmenu;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomListViewAdapter extends BaseAdapter {
private Activity activity;
private Book[] data;
private static LayoutInflater inflater=null;
public CustomListViewAdapter(Activity a, Book list[]) {
activity = a;
data=list;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getBookId(int position){
return data[position].getId();
}
public int getCount() {
return data.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_row, null);
TextView row_id =(TextView)vi.findViewById(R.id.row_id);
TextView name=(TextView)vi.findViewById(R.id.title);
TextView descp = (TextView) vi.findViewById(R.id.artist);
ImageView image=(ImageView)vi.findViewById(R.id.image);
row_id.setText(String.valueOf(data[position].getId()));
name.setText(data[position].getTitle());
descp.setText("Author: "+data[position].getAuthor());
return vi;
}
}
and when i use this code in main activity i have error
bt = new BookTable(getActivity().getApplicationContext());
Book bs = bt.arab_book("4");