Step 1:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/colorOrange">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:layout_width="40dp"
android:layout_gravity="center"
android:layout_height="40dp"
android:background="@drawable/logo"/>
<TextView
android:id="@+id/toolbar_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Biling and Storing"
android:textSize="16dp"
android:layout_marginLeft="10dp"
android:fontFamily="serif"
android:textColor="@android:color/white"/>
</LinearLayout>
</android.support.v7.widget.Toolbar>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:layout_weight="8"
android:background="@color/colorOrange"
android:orientation="horizontal">
<TextView
android:id="@+id/text_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
android:text="Today Date"
android:textColor="@android:color/white"
android:textSize="18dp"
android:textStyle="bold" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/colorOrange"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2.2"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tvNoRecordsFound"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:gravity="center"
android:textColor="@android:color/black"
android:text="No Records Found"
android:textSize="15dp" />
<ListView
android:id="@+id/list_product"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp"
android:background="@android:color/white"
android:divider="@android:color/white"
android:dividerHeight="10dp"
android:scrollbars="none"></ListView>
<ImageView
android:id="@+id/add_item"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="bottom|right"
android:layout_marginBottom="70dp"
android:layout_marginRight="30dp"
android:background="@drawable/add" />
</FrameLayout>
</LinearLayout>
</LinearLayout>
Step 2:
public class MainActivity extends AppCompatActivity {
TextView text_date;
AlertDialog.Builder alertDialogBuilder;
AlertDialog.Builder alertDialogBuilder2;
ImageView im_add;
String sdfString,sdfString2;
Date mDate,mDate2;
EditText input_name;
EditText input_price;
private String name;
private String price;
ListView listView;
private CustomAdapter customAdapter;
Cursor cursor;
StudentRepo studentRepo ;
private final static String TAG= MainActivity.class.getName().toString();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
valid();
im_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
click_add();
}
});
}
public void valid(){
alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
alertDialogBuilder2 = new AlertDialog.Builder(MainActivity.this);
im_add=(ImageView)findViewById(R.id.add_item);
listView=(ListView)findViewById(R.id.list_product);
text_date = (TextView) findViewById(R.id.text_date);
studentRepo = new StudentRepo(this);
cursor=studentRepo.getStudentList();
customAdapter = new CustomAdapter(MainActivity.this, cursor, 0);
listView.setAdapter(customAdapter);
bill=(LinearLayout)findViewById(R.id.layout_bill);
sale=(LinearLayout)findViewById(R.id.layout_sale);
detail=(LinearLayout)findViewById(R.id.layout_detail);
hellp=(LinearLayout)findViewById(R.id.layout_help);
setting=(LinearLayout)findViewById(R.id.layout_setting);
mDate=new Date();
mDate2=new Date();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy", Locale.US);
SimpleDateFormat sdf2 = new SimpleDateFormat("ddMMyyyy", Locale.US);
sdfString = sdf.format(mDate);
sdfString2 = sdf2.format(mDate2);
text_date.setText(sdfString);
}
public void click_add(){
alertDialogBuilder.setTitle("Meteirial Name");
alertDialogBuilder2.setTitle("Meteirial Price");
alertDialogBuilder.setMessage("Enter Meteirial Name");
alertDialogBuilder2.setMessage("Enter Meteirial Price");
input_name = new EditText(MainActivity.this);
input_price = new EditText(MainActivity.this);
int maxLength = 10;
int maxLength2 = 4;
input_name.setInputType(InputType.TYPE_CLASS_TEXT);
input_price.setInputType(InputType.TYPE_CLASS_NUMBER);
InputFilter[] FilterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.LengthFilter(maxLength);
InputFilter[] FilterArray2 = new InputFilter[1];
FilterArray2[0] = new InputFilter.LengthFilter(maxLength2);
input_name.setFilters(FilterArray);
input_price.setFilters(FilterArray2);
alertDialogBuilder.setView(input_name);
alertDialogBuilder2.setView(input_price);
alertDialogBuilder.setIcon(R.drawable.logo);
alertDialogBuilder2.setIcon(R.drawable.logo);
alertDialogBuilder.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
name=input_name.getText().toString();
if(name.length()>=2){
dialog.cancel();
AlertDialog dialog_price = alertDialogBuilder2.create();
dialog_price.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
dialog_price.show();
}
else {
Toast.makeText(getApplicationContext(),"Enter Correct Value",Toast.LENGTH_SHORT).show(); }
}
});
alertDialogBuilder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
dialog.cancel();
}
});
alertDialogBuilder2.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog2,int which) {
price=input_price.getText().toString();
if(price.length()>=2){
insertDummy();
}
else {
Toast.makeText(getApplicationContext(),"Enter Correct Value",Toast.LENGTH_SHORT).show();
}
}
});
alertDialogBuilder2.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog2,int which) {
dialog2.cancel();
}
});
alertDialogBuilder.setCancelable(true);
alertDialogBuilder2.setCancelable(true);
AlertDialog dialog_name = alertDialogBuilder.create();
dialog_name.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
dialog_name.show();
}
private void insertDummy(){
Student student=new Student();
student.age= Integer.parseInt(sdfString2);
student.email=""+price;
student.name=""+name;
studentRepo.insert(student);
cursor=studentRepo.getStudentListByKeyword("");
if (cursor==null){
Toast.makeText(MainActivity.this,"No records found!",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this, cursor.getCount() + " records found!",Toast.LENGTH_LONG).show();
}
customAdapter.swapCursor(cursor);
}
}
Step 3:
CustomAdapter .java
public class CustomAdapter extends CursorAdapter {
TextView txtId,txtName,txtEmail;
private LayoutInflater mInflater;
public CustomAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = mInflater.inflate(R.layout.list_item, parent, false);
ViewHolder holder = new ViewHolder();
holder.txtId = (TextView) view.findViewById(R.id.txtId);
holder.txtName = (TextView) view.findViewById(R.id.txtName);
holder.txtEmail = (TextView) view.findViewById(R.id.txtEmail);
view.setTag(holder);
return view;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
//If you want to have zebra lines color effect uncomment below code
/*if(cursor.getPosition()%2==1) {
view.setBackgroundResource(R.drawable.item_list_backgroundcolor);
} else {
view.setBackgroundResource(R.drawable.item_list_backgroundcolor2);
}*/
ViewHolder holder = (ViewHolder) view.getTag();
holder.txtId.setText(cursor.getString(cursor.getColumnIndex(Student.KEY_ID)));
holder.txtName.setText(cursor.getString(cursor.getColumnIndex(Student.KEY_name)));
holder.txtEmail.setText(cursor.getString(cursor.getColumnIndex(Student.KEY_email)));
}
static class ViewHolder {
TextView txtId;
TextView txtName;
TextView txtEmail;
}
}
Step 4:
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.instinctcoder.zipunzip.MainActivity">
<LinearLayout android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:layout_marginRight="5dip"
android:layout_alignParentRight="true">
<ImageView
android:id="@+id/list_image"
android:layout_width="25dip"
android:layout_height="25dip"
android:src="@drawable/logo" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="email@instinctcoder.com"
android:id="@+id/txtEmail"
android:layout_weight="90"
android:layout_alignBottom="@+id/txtName"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/txtId"
android:visibility="gone"
android:text="1"
android:layout_weight="50" />
<TextView
android:layout_width="318dp"
android:layout_height="40dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/txtName"
android:text="ten"
android:textStyle="bold"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/colorOrange">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:layout_width="40dp"
android:layout_gravity="center"
android:layout_height="40dp"
android:background="@drawable/logo"/>
<TextView
android:id="@+id/toolbar_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Biling and Storing"
android:textSize="16dp"
android:layout_marginLeft="10dp"
android:fontFamily="serif"
android:textColor="@android:color/white"/>
</LinearLayout>
</android.support.v7.widget.Toolbar>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:layout_weight="8"
android:background="@color/colorOrange"
android:orientation="horizontal">
<TextView
android:id="@+id/text_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
android:text="Today Date"
android:textColor="@android:color/white"
android:textSize="18dp"
android:textStyle="bold" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/colorOrange"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2.2"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tvNoRecordsFound"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:gravity="center"
android:textColor="@android:color/black"
android:text="No Records Found"
android:textSize="15dp" />
<ListView
android:id="@+id/list_product"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp"
android:background="@android:color/white"
android:divider="@android:color/white"
android:dividerHeight="10dp"
android:scrollbars="none"></ListView>
<ImageView
android:id="@+id/add_item"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="bottom|right"
android:layout_marginBottom="70dp"
android:layout_marginRight="30dp"
android:background="@drawable/add" />
</FrameLayout>
</LinearLayout>
</LinearLayout>
Step 2:
public class MainActivity extends AppCompatActivity {
TextView text_date;
AlertDialog.Builder alertDialogBuilder;
AlertDialog.Builder alertDialogBuilder2;
ImageView im_add;
String sdfString,sdfString2;
Date mDate,mDate2;
EditText input_name;
EditText input_price;
private String name;
private String price;
ListView listView;
private CustomAdapter customAdapter;
Cursor cursor;
StudentRepo studentRepo ;
private final static String TAG= MainActivity.class.getName().toString();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
valid();
im_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
click_add();
}
});
}
public void valid(){
alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
alertDialogBuilder2 = new AlertDialog.Builder(MainActivity.this);
im_add=(ImageView)findViewById(R.id.add_item);
listView=(ListView)findViewById(R.id.list_product);
text_date = (TextView) findViewById(R.id.text_date);
studentRepo = new StudentRepo(this);
cursor=studentRepo.getStudentList();
customAdapter = new CustomAdapter(MainActivity.this, cursor, 0);
listView.setAdapter(customAdapter);
bill=(LinearLayout)findViewById(R.id.layout_bill);
sale=(LinearLayout)findViewById(R.id.layout_sale);
detail=(LinearLayout)findViewById(R.id.layout_detail);
hellp=(LinearLayout)findViewById(R.id.layout_help);
setting=(LinearLayout)findViewById(R.id.layout_setting);
mDate=new Date();
mDate2=new Date();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy", Locale.US);
SimpleDateFormat sdf2 = new SimpleDateFormat("ddMMyyyy", Locale.US);
sdfString = sdf.format(mDate);
sdfString2 = sdf2.format(mDate2);
text_date.setText(sdfString);
}
public void click_add(){
alertDialogBuilder.setTitle("Meteirial Name");
alertDialogBuilder2.setTitle("Meteirial Price");
alertDialogBuilder.setMessage("Enter Meteirial Name");
alertDialogBuilder2.setMessage("Enter Meteirial Price");
input_name = new EditText(MainActivity.this);
input_price = new EditText(MainActivity.this);
int maxLength = 10;
int maxLength2 = 4;
input_name.setInputType(InputType.TYPE_CLASS_TEXT);
input_price.setInputType(InputType.TYPE_CLASS_NUMBER);
InputFilter[] FilterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.LengthFilter(maxLength);
InputFilter[] FilterArray2 = new InputFilter[1];
FilterArray2[0] = new InputFilter.LengthFilter(maxLength2);
input_name.setFilters(FilterArray);
input_price.setFilters(FilterArray2);
alertDialogBuilder.setView(input_name);
alertDialogBuilder2.setView(input_price);
alertDialogBuilder.setIcon(R.drawable.logo);
alertDialogBuilder2.setIcon(R.drawable.logo);
alertDialogBuilder.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
name=input_name.getText().toString();
if(name.length()>=2){
dialog.cancel();
AlertDialog dialog_price = alertDialogBuilder2.create();
dialog_price.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
dialog_price.show();
}
else {
Toast.makeText(getApplicationContext(),"Enter Correct Value",Toast.LENGTH_SHORT).show(); }
}
});
alertDialogBuilder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
dialog.cancel();
}
});
alertDialogBuilder2.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog2,int which) {
price=input_price.getText().toString();
if(price.length()>=2){
insertDummy();
}
else {
Toast.makeText(getApplicationContext(),"Enter Correct Value",Toast.LENGTH_SHORT).show();
}
}
});
alertDialogBuilder2.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog2,int which) {
dialog2.cancel();
}
});
alertDialogBuilder.setCancelable(true);
alertDialogBuilder2.setCancelable(true);
AlertDialog dialog_name = alertDialogBuilder.create();
dialog_name.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
dialog_name.show();
}
private void insertDummy(){
Student student=new Student();
student.age= Integer.parseInt(sdfString2);
student.email=""+price;
student.name=""+name;
studentRepo.insert(student);
cursor=studentRepo.getStudentListByKeyword("");
if (cursor==null){
Toast.makeText(MainActivity.this,"No records found!",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this, cursor.getCount() + " records found!",Toast.LENGTH_LONG).show();
}
customAdapter.swapCursor(cursor);
}
}
Step 3:
CustomAdapter .java
public class CustomAdapter extends CursorAdapter {
TextView txtId,txtName,txtEmail;
private LayoutInflater mInflater;
public CustomAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = mInflater.inflate(R.layout.list_item, parent, false);
ViewHolder holder = new ViewHolder();
holder.txtId = (TextView) view.findViewById(R.id.txtId);
holder.txtName = (TextView) view.findViewById(R.id.txtName);
holder.txtEmail = (TextView) view.findViewById(R.id.txtEmail);
view.setTag(holder);
return view;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
//If you want to have zebra lines color effect uncomment below code
/*if(cursor.getPosition()%2==1) {
view.setBackgroundResource(R.drawable.item_list_backgroundcolor);
} else {
view.setBackgroundResource(R.drawable.item_list_backgroundcolor2);
}*/
ViewHolder holder = (ViewHolder) view.getTag();
holder.txtId.setText(cursor.getString(cursor.getColumnIndex(Student.KEY_ID)));
holder.txtName.setText(cursor.getString(cursor.getColumnIndex(Student.KEY_name)));
holder.txtEmail.setText(cursor.getString(cursor.getColumnIndex(Student.KEY_email)));
}
static class ViewHolder {
TextView txtId;
TextView txtName;
TextView txtEmail;
}
}
Step 4:
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.instinctcoder.zipunzip.MainActivity">
<LinearLayout android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:layout_marginRight="5dip"
android:layout_alignParentRight="true">
<ImageView
android:id="@+id/list_image"
android:layout_width="25dip"
android:layout_height="25dip"
android:src="@drawable/logo" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="email@instinctcoder.com"
android:id="@+id/txtEmail"
android:layout_weight="90"
android:layout_alignBottom="@+id/txtName"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/txtId"
android:visibility="gone"
android:text="1"
android:layout_weight="50" />
<TextView
android:layout_width="318dp"
android:layout_height="40dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/txtName"
android:text="ten"
android:textStyle="bold"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
Step 5:
DBHelper .java
public class DBHelper extends SQLiteOpenHelper {
//version number to upgrade database version
//each time if you Add, Edit table, you need to change the
//version number.
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "searchwidget.db";
public DBHelper(Context context ) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
//All necessary tables you like to create will create here
String CREATE_TABLE_STUDENT = "CREATE TABLE " + Student.TABLE + "("
+ Student.KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT ,"
+ Student.KEY_name + " TEXT, "
+ Student.KEY_age + " INTEGER, "
+ Student.KEY_email + " TEXT )";
db.execSQL(CREATE_TABLE_STUDENT);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed, all data will be gone!!!
db.execSQL("DROP TABLE IF EXISTS " + Student.TABLE);
// Create tables again
onCreate(db);
}
}
Step 6:
Student .java
public class Student {
// Labels table name
public static final String TABLE = "Student";
// Labels Table Columns names
public static final String KEY_ROWID = "_id";
public static final String KEY_ID = "id";
public static final String KEY_name = "name";
public static final String KEY_email = "email";
public static final String KEY_age = "age";
// property help us to keep data
public int student_ID;
public String name;
public String email;
public int age;
}
Step 7:
StudentRepo .java
public class StudentRepo {
private DBHelper dbHelper;
public StudentRepo(Context context) {
dbHelper = new DBHelper(context);
}
public int insert(Student student) {
//Open connection to write data
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Student.KEY_age, student.age);
values.put(Student.KEY_email,student.email);
values.put(Student.KEY_name, student.name);
// Inserting Row
long student_Id = db.insert(Student.TABLE, null, values);
db.close(); // Closing database connection
return (int) student_Id;
}
public Cursor getStudentList() {
//Open connection to read only
SQLiteDatabase db = dbHelper.getReadableDatabase();
String selectQuery = "SELECT rowid as " +
Student.KEY_ROWID + "," +
Student.KEY_ID + "," +
Student.KEY_name + "," +
Student.KEY_email + "," +
Student.KEY_age +
" FROM " + Student.TABLE;
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor == null) {
return null;
} else if (!cursor.moveToFirst()) {
cursor.close();
return null;
}
return cursor;
}
public Cursor getStudentListByKeyword(String search) {
//Open connection to read only
SQLiteDatabase db = dbHelper.getReadableDatabase();
String selectQuery = "SELECT rowid as " +
Student.KEY_ROWID + "," +
Student.KEY_ID + "," +
Student.KEY_name + "," +
Student.KEY_email + "," +
Student.KEY_age +
" FROM " + Student.TABLE +
" WHERE " + Student.KEY_name + " LIKE '%" +search + "%' "
;
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor == null) {
return null;
} else if (!cursor.moveToFirst()) {
cursor.close();
return null;
}
return cursor;
}
public Student getStudentById(int Id){
SQLiteDatabase db = dbHelper.getReadableDatabase();
String selectQuery = "SELECT " +
Student.KEY_ID + "," +
Student.KEY_name + "," +
Student.KEY_email + "," +
Student.KEY_age +
" FROM " + Student.TABLE
+ " WHERE " +
Student.KEY_ID + "=?";// It's a good practice to use parameter ?, instead of concatenate string
int iCount =0;
Student student = new Student();
Cursor cursor = db.rawQuery(selectQuery, new String[] { String.valueOf(Id) } );
if (cursor.moveToFirst()) {
do {
student.student_ID =cursor.getInt(cursor.getColumnIndex(Student.KEY_ID));
student.name =cursor.getString(cursor.getColumnIndex(Student.KEY_name));
student.email =cursor.getString(cursor.getColumnIndex(Student.KEY_email));
student.age =cursor.getInt(cursor.getColumnIndex(Student.KEY_age));
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return student;
}
}
No comments:
Post a Comment