· web viewandroid program developed using adt bundle ***** // book recommender system code...
TRANSCRIPT
**********ANDROID PROGRAM DEVELOPED USING ADT BUNDLE *********
// BOOK RECOMMENDER SYSTEM
*********************Code AndroidManifest.xml*****************
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bookrecommender"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14" android:targetSdkVersion="19" /> <application android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Holo.Light" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".AddNewBookActivity"
android:label="@string/title_activity_add_new_book" >
</activity>
<activity
android:name=".BookRatingActivity"
android:label="@string/title_activity_book_rating" >
</activity>
</application>
</manifest>
**********************************************************************
Various Layout Files :
**********************Activity_main.xml********************
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.bookrecommender.MainActivity" >
<Spinner
android:id="@+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout> **********************************************************************
************************Activity_add_new_book.xml************************
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="@dimen/activity_horizontal_margin"
tools:context="com.example.bookrecommender.AddNewBookActivity" >
<EditText
android:id="@+id/editTextBookName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Book Name" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editTextAuther"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Auther name" />
<Spinner
android:id="@+id/categories"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/buttonAddNew"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FF0040"
android:text="Add Book" />
</LinearLayout>
**********************************************************************
*************************activity_book_rating.xml******************
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.bookrecommender.BookRatingActivity" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</RelativeLayout>
**********************************************************************
********************List_item_book_rating_layout.xml*******************
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E0E0E0"
android:orientation="vertical"
android:padding="@dimen/activity_horizontal_margin" >
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:textSize="16sp"
android:textStyle="bold" />
<RatingBar
android:id="@+id/ratingBar"
style="?android:attr/ratingBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
/>
</LinearLayout>
**********************************************************************
**********************List_item_book_layout.xml********************
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:padding="@dimen/activity_horizontal_margin"
android:orientation="vertical" >
<TextView
android:id="@+id/textViewBookName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textStyle="bold"
android:textColor="@android:color/black"
android:textSize="16dp" />
<TextView
android:id="@+id/textViewReleaseDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="@android:color/black"
android:textSize="16dp" />
<TextView
android:id="@+id/textViewRating"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="@android:color/black"
android:textSize="16dp" />
</LinearLayout>
**********************************************************************
**************************Various menu files*********************
****************Main.xml******************
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.bookrecommender.MainActivity" >
<item
android:id="@+id/action_add_book"
android:orderInCategory="100"
android:title="Add Book"
android:showAsAction="always"/>
<item
android:id="@+id/action_rate_book"
android:orderInCategory="100"
android:title="rate Book"
android:showAsAction="always"/>
</menu>
**********************************************************************
************************Add_new_book.xml*********************
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.bookrecommender.AddNewBookActivity" >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
android:showAsAction="never"/>
</menu>
**********************************************************************
***********************Book_rating.xml*****************
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.bookrecommender.BookRatingActivity" >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
android:showAsAction="never"/>
</menu>
*************************************************************************************************Various Java Files***************
*****************************AddNewBookActivity.java******************
package com.example.bookrecommender;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class AddNewBookActivity extends Activity implements OnClickListener,
OnItemSelectedListener {
private EditText editTextBookName, editTextAuther;
private Spinner spinnerCategories;
private Button buttonAddNew;
private DatabaseHelper databaseHelper;
private ArrayAdapter<String> categoriesAdapter;
private String bookname;
private String author;
private String categories;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_new_book);
databaseHelper = new DatabaseHelper(getApplicationContext());
editTextBookName = (EditText) findViewById(R.id.editTextBookName);
editTextAuther = (EditText) findViewById(R.id.editTextAuther);
spinnerCategories = (Spinner) findViewById(R.id.categories);
buttonAddNew = (Button) findViewById(R.id.buttonAddNew);
categoriesAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, getResources()
.getStringArray(R.array.book_types));
spinnerCategories.setAdapter(categoriesAdapter);
//set Listeners
buttonAddNew.setOnClickListener(this);
spinnerCategories.setOnItemSelectedListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.add_new_book, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
if (!editTextBookName.getText().toString().equals("")
|| !editTextAuther.getText().toString().equals("")) {
bookname = editTextBookName.getText().toString();
author = editTextAuther.getText().toString();
databaseHelper.addNewBook(bookname, categories, author);
onBackPressed();
} else {
Toast.makeText(getApplicationContext(), "Enter All Fileds",
Toast.LENGTH_SHORT).show();
}
}
@Override
public void onItemSelected(AdapterView<?> listAdapter, View view,
int position, long id) {
categories = listAdapter.getAdapter().getItem(position).toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
@Override
public void onBackPressed() {
Intent intent=new Intent(this,MainActivity.class);
startActivity(intent);
finish();
}
}
**********************************************************************
***********************BookBean.java*********************
package com.example.bookrecommender;
public class BookBean {
private int id;
private String bookname,author,categories,releaseDate;
private float rating=0;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public String getAuther() {
return author;
}
public void setAuther(String author) {
this.author = author;
}
public String getCategories() {
return categories;
}
public void setCategories(String categories) {
this.categories = categories;
}
public float getRating() {
return rating;
}
public void setRating(float rating) {
this.rating = rating;
}
public String getReleaseDate() {
return releaseDate;
}
public void setReleaseDate(String releaseDate) {
this.releaseDate = releaseDate;
}
}
**************************************************************************************************BookListAdapter.java******************
package com.example.bookrecommender;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;public class BookListAdapter extends BaseAdapter {
private ArrayList<BookBean> list;
private LayoutInflater inflater;
DatabaseHelper databaseHelper;
BookListAdapter(Context context,ArrayList<BookBean> list){
this.list=list;
inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
databaseHelper = new DatabaseHelper(context);
}
@Override
public int getCount() {
return list.size();
}
@Override
public BookBean getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView=inflater.inflate(R.layout.list_item_book_rlayout, null);
TextView textViewBookName=(TextView)convertView.findViewById(R.id.textViewBookName);
TextView textViewReleaseDate=(TextView)convertView.findViewById(R.id.textViewReleaseDate);
TextView textViewRating=(TextView)convertView.findViewById(R.id.textViewRating);
//Set Data final BookBean dataBean=list.get(position);
textViewBookName.setText( dataBean.getBookname());
textViewReleaseDate.setText("Release Date : " + dataBean.getReleaseDate());
textViewRating.setText("Rating : "+dataBean.getRating());
return convertView;
}}
**********************************************************************
******************BookRatingActivity.java******************
package com.example.bookrecommender;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ListView;
public class BookRatingActivity extends Activity {
private BookRatingAdapter bookAdapter;
private ListView listView;
private DatabaseHelper databaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_book_rating);
//Initialize views
listView = (ListView) findViewById(R.id.list);
//Initialize database
databaseHelper = new DatabaseHelper(getApplicationContext());
//get List from dataBase
ArrayList<BookBean> list = databaseHelper
.getBookList(getString(R.string.newrelease));
//Initialize adapter
bookAdapter = new BookRatingAdapter(getApplicationContext(), list);
//set adapter
listView.setAdapter(bookAdapter);
}
@Override
public void onBackPressed() {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
}
*********************************************************************************************BookRatingAdapter.java*******************
package com.example.bookrecommender;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;
public class BookRatingAdapter extends BaseAdapter {
private ArrayList<BookBean> list;
private LayoutInflater inflater;
private DatabaseHelper databaseHelper;
BookRatingAdapter(Context context,ArrayList<BookBean> list){
this.list=list;
inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
databaseHelper = new DatabaseHelper(context);
}
@Override
public int getCount() {
return list.size();
}
@Override
public BookBean getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
/*create views(List item)*/
convertView=inflater.inflate(R.layout.list_item_book_rating_layout, null);
TextView textView=(TextView)convertView.findViewById(R.id.textView);
RatingBar ratingBar=(RatingBar)convertView.findViewById(R.id.ratingBar);
//LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable();
//set Data
final BookBean dataBean=list.get(position);
ratingBar.setRating(dataBean.getRating());
textView.setText(dataBean.getBookname());
ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
databaseHelper.rateBook(dataBean.getId(), rating); } });
return convertView;
}
}
**********************************************************************
*************************DatabaseHelper.java***************************
package com.example.bookrecommender;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Locale;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String TAG = DatabaseHelper.class.getName();
public static final String DATABASE_NAME = "BOOKSYSTEM";
private static final int DATABASE_VERSION = 1;
rivate static final String TABLE_QUERY = "CREATE TABLE BOOKSYSTEM( ID INTEGER PRIMARY KEY AUTOINCREMENT, BOOKNAME TEXT NOT NULL, AUTHOR TEXT NOT NULL, RATING REAL,RELEASEDATE TEXT,DESC TEXT,CATEGORY TEXT)";
private static DatabaseHelper sDbInstance = null;
private Context context;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TABLE_QUERY);
} @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public static DatabaseHelper getInstance(Context context) {
if (sDbInstance == null) {
sDbInstance = new DatabaseHelper(context);
}
return sDbInstance;
}
/**
* Method to execute SQL Query
*
* @param s
* SQL Query
* @return cursor of returned results
*/
public synchronized Cursor ExecuteRawSql(String s) {// Select Query
try {
SQLiteDatabase sqLiteDb = getReadableDatabase();
Cursor cursor = sqLiteDb.rawQuery(s, null);
return cursor;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* Method to execute SQL Query
*
* @param s
* SQL Query
* @return true if query was successfully executed.
*/
public synchronized boolean ExecuteSql(String s) {// Update Query
try {
Log.d(TAG, "Actual Query--->>" + s);
SQLiteDatabase sqLiteDb = getWritableDatabase();
sqLiteDb.execSQL(s);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public boolean saveData(String startTime, String stopTime, int stepCount) {
String qry = "INSERT INTO PEDODATA (STARTTIME,STOPTIME,STEPS) VALUES('"
+ startTime + "','" + stopTime + "','" + stepCount + "');";
return ExecuteSql(qry);
}
public ArrayList<String> getData() {
ArrayList<String> list = new ArrayList<String>();
String qry = "SELECT STARTTIME,STOPTIME,STEPS FROM PEDODATA ORDER BY ID DESC";
Cursor cursor = ExecuteRawSql(qry);
if (cursor != null && cursor.moveToFirst()) {
do {
String startTime = cursor.getString(0);
String stopTime = cursor.getString(1);
int stepCount = cursor.getInt(2);
String time = "From : " + startTime + " \nTo : " + stopTime;
String steps = " \nSteps : " + stepCount;
list.add(time + steps);
} while (cursor.moveToNext());
}
return list; }
public boolean addNewBook(String name, String category, String AUTHOR) {
String qry = "INSERT INTO BOOKSYSTEM (BOOKNAME,CATEGORY,AUTHOR ,RELEASEDATE) VALUES('" + name + "','" + category + "','" + AUTHOR + "','"+getDateTime()+"');";
return ExecuteSql(qry); }
public boolean rateBook(int bookId, float rating) {
String qry = "UPDATE BOOKSYSTEM SET RATING=" + rating + " WHERE id="
+ bookId + "";
return ExecuteSql(qry); } public ArrayList<BookBean> getBookList(String category) {
ArrayList<BookBean> list = new ArrayList<BookBean>();
String qry;
if (category.equals(context.getString(R.string.toprated))) {
qry = "SELECT ID,BOOKNAME,AUTHOR,RELEASEDATE,RATING,CATEGORY FROM BOOKSYSTEM ORDER BY RATING DESC";
} else {
qry = "SELECT ID,BOOKNAME,AUTHOR,RELEASEDATE,RATING,CATEGORY FROM BOOKSYSTEM ORDER BY RELEASEDATE DESC"; }
Cursor cursor = ExecuteRawSql(qry);
if (cursor != null && cursor.moveToFirst()) {
do {
BookBean bookName = new BookBean();
bookName.setId(cursor.getInt(0));
bookName.setBookname(cursor.getString(1));
bookName.setAuther(cursor.getString(2));
bookName.setReleaseDate(cursor.getString(3));
bookName.setRating(cursor.getFloat(4));
bookName.setCategories(cursor.getString(5));
list.add(bookName);
} while (cursor.moveToNext());
}
return list;
}
public String getDateTime() {
Calendar c = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy",
Locale.US);
String formattedDate = formatter.format(c.getTime());
String s = formattedDate;
return s;
}
}
**********************************************************************
**********************MainActivity.java***********************
package com.example.bookrecommender;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
public class MainActivity extends Activity {
private Spinner searchView;
private ArrayAdapter<String> searchAdapter;
private ListView listView;
private DatabaseHelper databaseHelper;
private BookListAdapter bookListAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initialize UI components
searchView = (Spinner) findViewById(R.id.searchView);
listView = (ListView) findViewById(R.id.list);
searchAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.searchCategory));
searchView.setAdapter(searchAdapter);
databaseHelper = new DatabaseHelper(getApplicationContext());
ArrayList<BookBean> list = databaseHelper.getBookList(searchView.getSelectedItem().toString());
bookListAdapter = new BookListAdapter(getApplicationContext(), list);
listView.setAdapter(bookListAdapter); }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_add_book) {
Intent intent=new Intent(this,AddNewBookActivity.class);
startActivity(intent);
finish();
}else if(id == R.id.action_rate_book){
Intent intent=new Intent(this,BookRatingActivity.class);
startActivity(intent);
finish();
}
return super.onOptionsItemSelected(item);
}
/**
* Find book as per search text
*
* @param bookType
*/
/* private void findBookList(String bookType) {
String[] booklist = getBooklistfromResources(bookType);
listAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, booklist);
listView.setAdapter(listAdapter);
}*/
/**
* get list of books from resources
*
* @param bookType
* @return
*/
/* private String[] getBooklistfromResources(String bookType) {
String[] booklist = null;
if (bookType.equals(getString(R.string.novel))) {
booklist = getResources().getStringArray(R.array.novel);
} else if (bookType.equals(getString(R.string.fantasy))) {
booklist = getResources().getStringArray(R.array.fantasy);
} else if (bookType.equals(getString(R.string.literature))) {
booklist = getResources().getStringArray(R.array.literature);
} else if (bookType.equals(getString(R.string.scienceFiction))) {
booklist = getResources().getStringArray(R.array.scienceFiction);
} else if (bookType.equals(getString(R.string.java))) {
booklist = getResources().getStringArray(R.array.java);
}
return booklist; }*/