Android Sqlite Sample Crud Application Datahelper - Java: "Lara - Books"
Android Sqlite Sample Crud Application Datahelper - Java: "Lara - Books"
import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import com.lara.book.Book;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;
Book.java
package com.lara.book;
import java.io.Serializable;
main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:id="@+id/home_scroll" android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<TableLayout android:id="@+id/home_tb" android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow android:layout_width="fill_parent" android:gravity="center">
<TextView android:text="Book Management"
android:layout_width="200px" android:textSize="20px"
android:layout_height="30px"/>
</TableRow>
<TableRow android:layout_width="fill_parent" android:gravity="center">
<Button android:id="@+id/add" android:text="Add Book"
android:layout_width="120px"/>
</TableRow>
<TableRow android:layout_width="fill_parent" android:gravity="center">
<Button android:id="@+id/list" android:text="List Books"
android:layout_width="120px"/>
</TableRow>
</TableLayout>
AddBook.java
package com.lara.book;
import com.lara.db.DataHelper;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Book book = new Book();
book.setName(name.getText().toString());
book.setAuther(auther.getText().toString());
book.setPrice(price.getText().toString());
dh.addBook(book);
startActivity(homeIntent);
}
});
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
startActivity(homeIntent);
}
});
}
add_book.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:id="@+id/home_scroll" android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<TableLayout android:id="@+id/home_tb" android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow android:layout_width="fill_parent" android:gravity="center">
<TextView android:text="Add Book" android:layout_width="200px"
android:textSize="20px" android:layout_height="30px"/>
</TableRow>
<TableRow android:layout_width="fill_parent" android:gravity="center">
<EditText android:id="@+id/book_name" android:hint="Name.."
android:layout_width="120px"/>
</TableRow>
<TableRow android:layout_width="fill_parent" android:gravity="center">
<EditText android:id="@+id/book_auther" android:hint="Auther.."
android:layout_width="120px"/>
</TableRow>
<TableRow android:layout_width="fill_parent" android:gravity="center">
<EditText android:id="@+id/book_price" android:hint="Price.."
android:layout_width="120px"/>
</TableRow>
<TableRow android:layout_width="fill_parent" android:gravity="center">
<Button android:id="@+id/cancel_add_book" android:text="Cancel"
android:layout_width="120px"/>
<Button android:id="@+id/add_add_book" android:text="Add"
android:layout_width="120px"/>
</TableRow>
</TableLayout>
</ScrollView>
ListBooks.java
package com.lara.book;
import java.util.ArrayList;
import com.lara.db.DataHelper;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
Button ok = (Button)findViewById(R.id.list_ok_books);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
finish();
}
});
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
String bookName = lv.getItemAtPosition(position).toString();
Log.d("bookName-->>>>>", bookName);
bookDetailsIntent.putExtra("bookName", bookName);
startActivity(bookDetailsIntent);
}
});
}
}
list_books.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:id="@+id/home_scroll" android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
</ListView>
<TableRow android:layout_width="fill_parent">
<Button android:id="@+id/list_ok_books" android:text="ok"
android:layout_width="120px"/>
</TableRow>
</TableLayout>
</ScrollView>
BookDetails.java
package com.lara.book;
import com.lara.db.DataHelper;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
name.setText(book.getName());
auther.setText(book.getAuther());
price.setText(book.getPrice());
Button ok = (Button)findViewById(R.id.ok_book_details);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editIntent.putExtra("book", book);
startActivity(editIntent);
}
});
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dh.deleteBook(book.getName());
startActivity(homeIntent);
}
});
}
}
book_details.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:id="@+id/home_scroll" android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<TableLayout android:id="@+id/home_tb" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow android:layout_width="fill_parent" android:gravity="center">
<TextView android:layout_width="120px" android:text="BookDetails"
android:layout_height="30px"
android:id="@+id/listAcctslabel_tv"></TextView>
</TableRow>
<TableRow android:layout_width="fill_parent">
<TextView android:layout_width="120px" android:text="Name"
android:layout_height="30px"></TextView>
<TextView android:layout_width="120px" android:text=""
android:layout_height="30px"
android:id="@+id/book_name_display"></TextView>
</TableRow>
<TableRow android:layout_width="fill_parent">
EditBook.java
package com.lara.book;
import com.lara.db.DataHelper;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Book book = new Book();
book.setName(name.getText().toString());
book.setAuther(auther.getText().toString());
book.setPrice(price.getText().toString());
dh.editBook(book);
startActivity(homeIntent);
}
});
}
}
edit_book.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:id="@+id/home_scroll" android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<TableLayout android:id="@+id/home_tb" android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow android:layout_width="fill_parent" android:gravity="center">
<TextView android:text="Edit Book" android:layout_width="200px"
android:textSize="20px" android:layout_height="30px"/>
</TableRow>
<TableRow android:layout_width="fill_parent" android:gravity="center">
<EditText android:id="@+id/book_name_edit" android:hint="Name.."
android:layout_width="120px"/>
</TableRow>
<TableRow android:layout_width="fill_parent" android:gravity="center">
<EditText android:id="@+id/book_auther_edit" android:hint="Auther.."
android:layout_width="120px"/>
</TableRow>
<TableRow android:layout_width="fill_parent" android:gravity="center">
<EditText android:id="@+id/book_price_edit" android:hint="Price.."
android:layout_width="120px"/>
</TableRow>
<TableRow android:layout_width="fill_parent" android:gravity="center">
<Button android:id="@+id/cancel_edit_book" android:text="Cancel"
android:layout_width="120px"/>
<Button android:id="@+id/save_edit_book" android:text="Save Changes"
android:layout_width="120px"/>
</TableRow>
</TableLayout>
</ScrollView>