Database Android

Database Class :-

Database class Transaction

package app.dailyexpensemanager.db; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.DatabaseUtils; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; /** * Created by Fame ITC on 25-Apr-16. */ public class Transactiondata{ String from_date , to_date; String item = "CREATE TABLE " + DATABASE_TABLE_1 + " (" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_PRICE + " TEXT, " + KEY_CATEGORY + " TEXT, " + KEY_PAYMENT_MODE + " TEXT, " + KEY_DATE + " TEXT, " + KEY_DESCRIPTION + " TEXT )"; String item_2 = "CREATE TABLE " + DATABASE_TABLE_2 + " (" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_PRICE + " TEXT, " + KEY_CATEGORY + " TEXT, " + KEY_PAYMENT_MODE + " TEXT, " + KEY_DATE + " TEXT, " + KEY_DESCRIPTION + " TEXT )"; String item_3 = "CREATE TABLE " + DATABASE_TABLE_3 + " (" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_DATE + " TEXT, " + KEY_REMINDER_ID + " TEXT, " + KEY_CATEGORY + " TEXT, " + KEY_PRICE + " TEXT, " + KEY_DESCRIPTION + " TEXT )"; public static final String KEY_ID = "id"; public static final String KEY_PRICE = "transaction_price"; public static final String KEY_CATEGORY = "transaction_category"; public static final String KEY_PAYMENT_MODE = "transaction_mode"; public static final String KEY_REMINDER_ID = "reminder_key"; public static final String KEY_DATE = "transaction_date"; public static final String KEY_DESCRIPTION = "transaction_des"; public static final String DATABASE_NAME = "dailyInfo.sqlite"; public static final String DATABASE_PATH = "/data/data/app.dailyexpensemanager/databases/" + DATABASE_NAME; public static final String DATABASE_TABLE_1 = "income"; public static final String DATABASE_TABLE_2 = "expense"; public static final String DATABASE_TABLE_3 = "reminder"; public static final int DATABASE_VERSION = 9; private DbHelper dbHelper; private Context ourContext; private SQLiteDatabase database; Cursor mCursor; private class DbHelper extends SQLiteOpenHelper { public DbHelper(Context context) { super(context, DATABASE_PATH, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(item); db.execSQL(item_2); db.execSQL(item_3); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE_1); db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE_2); db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE_3); onCreate(db); } } public Transactiondata(Context c){ ourContext = c ; } public Transactiondata open() { dbHelper = new DbHelper(ourContext); database = dbHelper.getWritableDatabase(); return this; } public long incomePutInformation(String price , String category , String payment_mode , String date , String description){ ContentValues cv = new ContentValues(); cv.put(KEY_PRICE , price); cv.put(KEY_CATEGORY , category); cv.put(KEY_PAYMENT_MODE , payment_mode); cv.put(KEY_DATE, date); cv.put(KEY_DESCRIPTION, description); Log.d("hello ===== ", "insert on raw--------------------------------" + cv.get(KEY_ID)); return database.insert(DATABASE_TABLE_1, null, cv); } public long expensePutInformation(String price , String category , String payment_mode , String date , String description){ ContentValues cv = new ContentValues(); cv.put(KEY_PRICE , price); cv.put(KEY_CATEGORY , category); cv.put(KEY_PAYMENT_MODE , payment_mode); cv.put(KEY_DATE, date); cv.put(KEY_DESCRIPTION, description); Log.d("hello ===== ", "insert on raw--------------------------------"); return database.insert(DATABASE_TABLE_2, null, cv); } public long reminderPutInformation(int reminder_id , String date , String category , String price , String description){ ContentValues cv = new ContentValues(); cv.put(KEY_DATE, date); cv.put(KEY_REMINDER_ID, reminder_id); cv.put(KEY_CATEGORY , category); cv.put(KEY_PRICE , price); cv.put(KEY_DESCRIPTION, description); Log.d("hello ===== ", "insert on raw-----" + cv.get(KEY_ID) + cv.get(KEY_DATE)); return database.insert(DATABASE_TABLE_3, null, cv); } public void close(){ dbHelper.close(); } /*............................................ Display data query...................................................*/ public Cursor incomeDisplayData() { SQLiteDatabase database = dbHelper.getWritableDatabase(); mCursor = database.rawQuery("SELECT * FROM " + DATABASE_TABLE_1 , null); return mCursor; } public Cursor expenseDisplayData() { SQLiteDatabase database = dbHelper.getWritableDatabase(); mCursor = database.rawQuery("SELECT * FROM " + DATABASE_TABLE_2 , null); Log.d("hello", "findrd2: " + DatabaseUtils.dumpCursorToString(mCursor)); return mCursor; } public Cursor reminderDisplayData() { SQLiteDatabase database = dbHelper.getWritableDatabase(); mCursor = database.rawQuery("SELECT * FROM " + DATABASE_TABLE_3 , null); if(mCursor.moveToFirst()){ Log.d(" msg hai ..." , DatabaseUtils.dumpCursorToString(mCursor)); }while (mCursor.moveToNext()); return mCursor; } /*............................................ Update Reminder query...................................................*/ public void updateReminderByID(int reminder_id , String id, String date , String category , String price , String description){ ContentValues values = new ContentValues(); values.put(KEY_DATE, date); values.put(KEY_REMINDER_ID, reminder_id); values.put(KEY_CATEGORY, category); values.put(KEY_PRICE, price); values.put(KEY_DESCRIPTION, description); Log.d("hello ===== ", "insert on raw----- ID" + description + " update " + values.get(KEY_DATE)); database.update(DATABASE_TABLE_3, values , KEY_ID + "=" + id , null); } /*............................................ Delete data query...................................................*/ public void deleteIncomeEntry(String position) { // TODO Auto-generated method stub SQLiteDatabase database = dbHelper.getWritableDatabase(); database.delete(DATABASE_TABLE_1, KEY_ID + "=" + position, null); } public void deleteExpenseEntry(String position) { // TODO Auto-generated method stub SQLiteDatabase database = dbHelper.getWritableDatabase(); database.delete(DATABASE_TABLE_2, KEY_ID + "=" + position , null); } public void deleteReminderEntry(String position) { // TODO Auto-generated method stub SQLiteDatabase database = dbHelper.getWritableDatabase(); database.delete(DATABASE_TABLE_3, KEY_ID + "=" + position , null); } /*............................................ Type to Display data query...................................................*/ public Cursor dateIncomeDisplayData() { SQLiteDatabase database = dbHelper.getWritableDatabase(); mCursor = database.rawQuery("SELECT * FROM " + DATABASE_TABLE_1 + " ORDER BY "+ KEY_DATE + " DESC " , null); return mCursor; } public Cursor dateExpenseDisplayData() { SQLiteDatabase database = dbHelper.getWritableDatabase(); mCursor = database.rawQuery("SELECT * FROM " + DATABASE_TABLE_2 + " ORDER BY "+ KEY_DATE + " DESC" , null); return mCursor; } public Cursor categoryIncomeDisplayData() { SQLiteDatabase database = dbHelper.getWritableDatabase(); mCursor = database.rawQuery("SELECT * FROM " + DATABASE_TABLE_1 + " ORDER BY "+ KEY_CATEGORY , null); return mCursor; } public Cursor categoryExpenseDisplayData() { SQLiteDatabase database = dbHelper.getWritableDatabase(); mCursor = database.rawQuery("SELECT * FROM " + DATABASE_TABLE_2 + " ORDER BY "+ KEY_CATEGORY , null); return mCursor; } public Cursor paymentIncomeDisplayData() { SQLiteDatabase database = dbHelper.getWritableDatabase(); mCursor = database.rawQuery("SELECT * FROM " + DATABASE_TABLE_1 + " ORDER BY "+ KEY_PAYMENT_MODE , null); return mCursor; } public Cursor paymentExpenseDisplayData() { SQLiteDatabase database = dbHelper.getWritableDatabase(); mCursor = database.rawQuery("SELECT * FROM " + DATABASE_TABLE_2 + " ORDER BY "+ KEY_PAYMENT_MODE , null); return mCursor; } public Cursor searchIncomeDataDisplayData(String a, String b , String c , String d) { SimpleDateFormat simpleDateFormate = new SimpleDateFormat("dd-MM-yyyy"); Date date1 = null; Date date2 = null; try { date1 = simpleDateFormate.parse(a); date2 = simpleDateFormate.parse(b); SimpleDateFormat fmtOut = new SimpleDateFormat("yyyy-MM-dd"); from_date = fmtOut.format(date1); to_date = fmtOut.format(date2); } catch (ParseException e) { e.printStackTrace(); } SQLiteDatabase database = dbHelper.getWritableDatabase(); mCursor = database.rawQuery("SELECT * FROM " + DATABASE_TABLE_1 + " WHERE ("+ KEY_DATE + " >= '" + from_date
+"'" + " AND " + KEY_DATE + " <= '" + to_date +"' )" + " OR " + KEY_CATEGORY + " = '" + c + "' " + " OR " + KEY_PAYMENT_MODE + " = '" + d + "' " , null); if(mCursor.moveToFirst()){ Log.d(" msg hai ..." , DatabaseUtils.dumpCursorToString(mCursor)); }while (mCursor.moveToNext()); return mCursor; } public Cursor searchExpenseDataDisplayData(String a, String b) { SimpleDateFormat simpleDateFormate = new SimpleDateFormat("dd-MM-yyyy"); Date date1 = null; Date date2 = null; try { date1 = simpleDateFormate.parse(a); date2 = simpleDateFormate.parse(b); SimpleDateFormat fmtOut = new SimpleDateFormat("yyyy-MM-dd"); from_date = fmtOut.format(date1); to_date = fmtOut.format(date2); } catch (ParseException e) { e.printStackTrace(); } SQLiteDatabase database = dbHelper.getWritableDatabase(); mCursor = database.rawQuery("SELECT * FROM " + DATABASE_TABLE_2 + " WHERE "+ KEY_DATE + " >= '" + from_date
+"'" + " AND " + KEY_DATE + " <= '" + to_date +"' " , null); if(mCursor.moveToFirst()){ Log.d(" msg hai ..." , DatabaseUtils.dumpCursorToString(mCursor)); }while (mCursor.moveToNext()); return mCursor; } }

Add data in Database

package app.dailyexpensemanager.activity; import android.app.DatePickerDialog; import android.app.Dialog; import android.content.Context; import android.content.res.Resources; import android.graphics.Color; import android.os.Bundle; import android.support.v7.widget.Toolbar; import android.text.Editable; import android.text.Html; import android.text.TextWatcher; import android.view.MotionEvent; import android.view.View; import android.view.WindowManager; import android.widget.DatePicker; import android.widget.EditText; import android.widget.RadioButton; import android.widget.TextView; import android.widget.Toast; import android.widget.ToggleButton; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import app.dailyexpensemanager.R; import app.dailyexpensemanager.db.Transactiondata; /** * Created by Fame ITC on 23-Apr-16. */ public class AddTransactionActivity extends MainActivity { EditText price , category , payment_mode , date , description; TextView save , reset; RadioButton rb1 , rb2 ; String date_change; Toast t; private int year; private int month; private int day; Context context = this; static final int DATE_PICKER_ID = 1111; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_add_transaction); Toolbar toolbarTop = (Toolbar) findViewById(R.id.toolbar); TextView mTitle = (TextView) toolbarTop.findViewById(R.id.title); mTitle.setText("Transaction Details"); rb1 =(RadioButton)findViewById(R.id.rb1) ; rb2 =(RadioButton)findViewById(R.id.rb2) ; date = (EditText) findViewById(R.id.date); price = (EditText) findViewById(R.id.price); category = (EditText) findViewById(R.id.category); payment_mode = (EditText) findViewById(R.id.payment_mode); description = (EditText) findViewById(R.id.description); save = (TextView) findViewById(R.id.save); reset = (TextView) findViewById(R.id.reset); date.addTextChangedListener(new GenericTextWatcher(date)); price.addTextChangedListener(new GenericTextWatcher(price)); payment_mode.addTextChangedListener(new GenericTextWatcher(payment_mode)); category.addTextChangedListener(new GenericTextWatcher(category)); description.addTextChangedListener(new GenericTextWatcher(description)); // Get current date by calender rb1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (rb1.isChecked()) { rb1.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_background_select)); rb2.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_background_second)); } } }); rb2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (rb2.isChecked()) { rb2.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_background_second_sele)); rb1.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_background)); } } }); final Calendar c = Calendar.getInstance(); year = c.get(Calendar.YEAR); month = c.get(Calendar.MONTH); day = c.get(Calendar.DAY_OF_MONTH); // Show current date /* date.setText(new StringBuilder() // Month is 0 based, just add 1 .append(day).append("-").append(month + 1).append("-") .append(year).append(" "));*/ // Button listener to show date picker dialog date.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { showDialog(DATE_PICKER_ID); return false; } }); reset.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { price.setText(""); category.setText(""); payment_mode.setText(""); date.setText(""); description.setText(""); price.requestFocus(); //Prevent Toast Overlapping every time clicking on button /*if (t != null) { t.cancel(); } t = Toast.makeText(context, "Clear", Toast.LENGTH_SHORT); t.show();*/ } }); save.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(!validatePrice()){ return; } if(!validateCategory()){ return; } if(!validatePayment()){ return; } if(!validateDate()){ return; } if(!validateDescription()){ return; } if(rb1.isChecked()) { String sql_price = price.getText().toString(); String sql_category = category.getText().toString(); String sql_payment_mode = payment_mode.getText().toString(); String sql_date = date.getText().toString(); String sql_description = description.getText().toString(); SimpleDateFormat simpleDateFormate = new SimpleDateFormat("dd-MM-yyyy"); Date date1 = null; try { date1 = simpleDateFormate.parse(sql_date); SimpleDateFormat fmtOut = new SimpleDateFormat("yyyy-MM-dd"); date_change = fmtOut.format(date1); } catch (ParseException e) { e.printStackTrace(); } Transactiondata newEntry = new Transactiondata(context); newEntry.open(); newEntry.incomePutInformation(sql_price, sql_category, sql_payment_mode, date_change , sql_description); newEntry.close(); Toast.makeText(AddTransactionActivity.this, "Data Added Successfully.", Toast.LENGTH_SHORT).show(); price.setText(""); category.setText(""); payment_mode.setText(""); date.setText(""); description.setText(""); price.requestFocus(); }else { String sql_price = price.getText().toString(); String sql_category = category.getText().toString(); String sql_payment_mode = payment_mode.getText().toString(); String sql_date = date.getText().toString(); String sql_description = description.getText().toString(); SimpleDateFormat simpleDateFormate = new SimpleDateFormat("dd-MM-yyyy"); Date date1 = null; try { date1 = simpleDateFormate.parse(sql_date); SimpleDateFormat fmtOut = new SimpleDateFormat("yyyy-MM-dd"); date_change = fmtOut.format(date1); } catch (ParseException e) { e.printStackTrace(); } Transactiondata newEntry = new Transactiondata(context); newEntry.open(); newEntry.expensePutInformation(sql_price, sql_category, sql_payment_mode, date_change, sql_description); newEntry.close(); Toast.makeText(AddTransactionActivity.this, "Data Added Successfully.", Toast.LENGTH_SHORT).show(); price.setText(""); category.setText(""); payment_mode.setText(""); date.setText(""); description.setText(""); price.requestFocus(); } } }); } @Override protected Dialog onCreateDialog(int id) { switch (id) { case DATE_PICKER_ID: // open datepicker dialog. // set date picker for current date // add pickerListener listner to date picker return new DatePickerDialog(this, pickerListener, year, month,day); } return null; } private DatePickerDialog.OnDateSetListener pickerListener = new DatePickerDialog.OnDateSetListener() { // when dialog box is closed, below method will be called. @Override public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) { year = selectedYear; month = selectedMonth; day = selectedDay; // Show selected date date.setText(new StringBuilder().append(day) .append("-").append(month + 1).append("-").append(year) .append(" ")); } }; private boolean validatePrice() { if (price.getText().toString().trim().isEmpty()) { price.setError(Html.fromHtml("Enter the price")); requestFocus(price); return false; } else { price.setError(null); } return true; } private boolean validateCategory() { if (category.getText().toString().trim().isEmpty()) { category.setError(Html.fromHtml("Enter the category")); requestFocus(category); return false; } else { category.setError(null); } return true; } private boolean validatePayment() { if (payment_mode.getText().toString().trim().isEmpty()) { payment_mode.setError(Html.fromHtml("Enter the payment mode")); requestFocus(payment_mode); return false; } else { payment_mode.setError(null); } return true; } private boolean validateDate() { if (date.getText().toString().trim().isEmpty()) { date.setError(Html.fromHtml("Enter the date")); requestFocus(date); return false; } else { date.setError(null); } return true; } private boolean validateDescription() { if (description.getText().toString().trim().isEmpty()) { description.setError(Html.fromHtml("Enter the description")); requestFocus(description); return false; } else { description.setError(null); } return true; } private void requestFocus(View view) { if (view.requestFocus()) { getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); } } private class GenericTextWatcher implements TextWatcher { private View view; private GenericTextWatcher(View view) { this.view = view; } public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {} public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {} public void afterTextChanged(Editable editable) { String text = editable.toString(); switch(view.getId()){ case R.id.price: if(text.length()>0) price.setError(null); break; case R.id.category: if(text.length()>0) category.setError(null); break; case R.id.payment_mode: if(text.length()>0) payment_mode.setError(null); break; case R.id.date: if(text.length()>0) date.setError(null); break; case R.id.description: if(text.length()>0) description.setError(null); break; } } } }

All data display

package app.dailyexpensemanager.activity; import android.database.Cursor; import android.database.DatabaseUtils; import android.os.Bundle; import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.View; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.RadioButton; import android.widget.RelativeLayout; import android.widget.TextView; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import app.dailyexpensemanager.R; import app.dailyexpensemanager.adapters.DisplayAllDataAdapter; import app.dailyexpensemanager.db.Transactiondata; /** * Created by Fame ITC on 25-Apr-16. */ public class AllTransactionActivity extends MainActivity { ArrayList price = new ArrayList(); ArrayList category = new ArrayList(); ArrayList date = new ArrayList(); ArrayList id = new ArrayList(); ArrayList payment_mode = new ArrayList(); ArrayList description = new ArrayList(); RadioButton rb1 , rb2 ; ListView listView ; Cursor cv; LinearLayout list_lay; RelativeLayout no_data_lay; String date_change; Boolean check_btn = false; Transactiondata data ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_all_transaction); findViewById(); Toolbar toolbarTop = (Toolbar) findViewById(R.id.toolbar); TextView mTitle = (TextView) toolbarTop.findViewById(R.id.title); mTitle.setText("All Transaction"); data = new Transactiondata(AllTransactionActivity.this); data.open(); cv = data.incomeDisplayData(); id.clear(); price.clear(); category.clear(); date.clear(); payment_mode.clear(); description.clear(); if (cv.moveToFirst()) { do { category.add(cv.getString(cv.getColumnIndex(Transactiondata.KEY_CATEGORY))); id.add(cv.getString(cv.getColumnIndex(Transactiondata.KEY_ID))); // date.add(cv.getString(cv.getColumnIndex(Transactiondata.KEY_DATE))); SimpleDateFormat simpleDateFormate = new SimpleDateFormat("yyyy-MM-dd"); Date date1 = null; try { date1 = simpleDateFormate.parse(cv.getString(cv.getColumnIndex(Transactiondata.KEY_DATE))); SimpleDateFormat fmtOut = new SimpleDateFormat("dd-MM-yyyy"); date_change = fmtOut.format(date1); } catch (ParseException e) { e.printStackTrace(); } date.add(date_change); price.add(cv.getString(cv.getColumnIndex(Transactiondata.KEY_PRICE))); payment_mode.add(cv.getString(cv.getColumnIndex(Transactiondata.KEY_PAYMENT_MODE))); description.add(cv.getString(cv.getColumnIndex(Transactiondata.KEY_DESCRIPTION))); } while (cv.moveToNext()); }else { list_lay.setVisibility(View.GONE); no_data_lay.setVisibility(View.VISIBLE); } listView.setAdapter(new DisplayAllDataAdapter(this, AllTransactionActivity.this , check_btn , id , category , date , price , payment_mode , description )); cv.close(); } public void findViewById(){ listView = (ListView)findViewById(R.id.list); rb1 =(RadioButton)findViewById(R.id.rb1) ; rb2 =(RadioButton)findViewById(R.id.rb2) ; no_data_lay = (RelativeLayout)findViewById(R.id.no_data_lay); list_lay = (LinearLayout)findViewById(R.id.list_lay); } // method call from xml file .............................................................................. public void findrd1(View v){ check_btn = false; //Toast.makeText(this , "hello " , Toast.LENGTH_LONG).show(); if (rb1.isChecked()) { rb1.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_background_select)); rb2.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_background_second)); } data = new Transactiondata(AllTransactionActivity.this); data.open(); cv = data.incomeDisplayData(); id.clear(); price.clear(); category.clear(); date.clear(); payment_mode.clear(); description.clear(); if (cv.moveToFirst()) { no_data_lay.setVisibility(View.GONE); list_lay.setVisibility(View.VISIBLE); do { category.add(cv.getString(cv.getColumnIndex(Transactiondata.KEY_CATEGORY))); SimpleDateFormat simpleDateFormate = new SimpleDateFormat("yyyy-MM-dd"); Date date1 = null; try { date1 = simpleDateFormate.parse(cv.getString(cv.getColumnIndex(Transactiondata.KEY_DATE))); SimpleDateFormat fmtOut = new SimpleDateFormat("dd-MM-yyyy"); date_change = fmtOut.format(date1); } catch (ParseException e) { e.printStackTrace(); } date.add(date_change); id.add(cv.getString(cv.getColumnIndex(Transactiondata.KEY_ID))); price.add(cv.getString(cv.getColumnIndex(Transactiondata.KEY_PRICE))); payment_mode.add(cv.getString(cv.getColumnIndex(Transactiondata.KEY_PAYMENT_MODE))); description.add(cv.getString(cv.getColumnIndex(Transactiondata.KEY_DESCRIPTION))); } while (cv.moveToNext()); } else { list_lay.setVisibility(View.GONE); no_data_lay.setVisibility(View.VISIBLE); } listView.setAdapter(new DisplayAllDataAdapter(this, AllTransactionActivity.this , check_btn, id, category, date, price, payment_mode, description)); cv.close(); } // method call from xml file .............................................................................. public void findrd2(View v){ check_btn = true; //Toast.makeText(this , "hii " , Toast.LENGTH_LONG).show(); if (rb2.isChecked()) { rb2.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_background_second_sele)); rb1.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_background)); } data = new Transactiondata(AllTransactionActivity.this); data.open(); Cursor cv; cv = data.expenseDisplayData(); id.clear(); price.clear(); category.clear(); date.clear(); payment_mode.clear(); description.clear(); if (cv.moveToFirst()) { no_data_lay.setVisibility(View.GONE); list_lay.setVisibility(View.VISIBLE); do { category.add(cv.getString(cv.getColumnIndex(Transactiondata.KEY_CATEGORY))); SimpleDateFormat simpleDateFormate = new SimpleDateFormat("yyyy-MM-dd"); Date date1 = null; try { date1 = simpleDateFormate.parse(cv.getString(cv.getColumnIndex(Transactiondata.KEY_DATE))); SimpleDateFormat fmtOut = new SimpleDateFormat("dd-MM-yyyy"); date_change = fmtOut.format(date1); } catch (ParseException e) { e.printStackTrace(); } date.add(date_change); id.add(cv.getString(cv.getColumnIndex(Transactiondata.KEY_ID))); price.add(cv.getString(cv.getColumnIndex(Transactiondata.KEY_PRICE))); payment_mode.add(cv.getString(cv.getColumnIndex(Transactiondata.KEY_PAYMENT_MODE))); description.add(cv.getString(cv.getColumnIndex(Transactiondata.KEY_DESCRIPTION))); Log.d("hello", "findrd2: " + DatabaseUtils.dumpCursorToString(cv)); } while (cv.moveToNext()); } else { list_lay.setVisibility(View.GONE); no_data_lay.setVisibility(View.VISIBLE); } Log.d("hello", "findrd2: " + category +" , " + payment_mode ); listView.setAdapter(new DisplayAllDataAdapter(this, AllTransactionActivity.this , check_btn , id , category , date , price , payment_mode , description )); cv.close(); listView.setLongClickable(true); } @Override protected void onResume() { super.onResume(); /*List newItems = databaseHandler.getItems(); ListArrayAdapter.clear(); ListArrayAdapter.addAll(newItems); ListArrayAdapter.notifyDataSetChanged(); databaseHandler.close();*/ } }

Adapter Class

package app.dailyexpensemanager.adapters; import android.app.Activity; import android.app.Dialog; import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; import java.util.ArrayList; import app.dailyexpensemanager.R; import app.dailyexpensemanager.activity.EditReminderActivity; import app.dailyexpensemanager.activity.PaymentTransactionActivity; import app.dailyexpensemanager.db.Transactiondata; /** * Created by Fame ITC on 27-Apr-16. */ public class DisplayAllDataAdapter extends BaseAdapter { private Context mContext; private static Cursor cursor; private static Activity activity; private static Boolean check_btn; private ArrayList category_list; private ArrayList date_list; private ArrayList id_list ; private ArrayList price_list ; private ArrayList payment_mode_list ; private ArrayList description_list ; public DisplayAllDataAdapter(Context cv, Activity activity, Boolean check_btn, ArrayList id, ArrayList category, ArrayList date, ArrayList price, ArrayList payment_mode, ArrayList description) { this.check_btn = check_btn; this.id_list = id; this.category_list = category; this.date_list = date; this.price_list = price; this.payment_mode_list = payment_mode; this.description_list = description; this.mContext = cv; this.activity = activity; } public DisplayAllDataAdapter(Context cv, Activity activity,Cursor cursor , Boolean check_btn, ArrayList id, ArrayList category, ArrayList date, ArrayList price, ArrayList payment_mode, ArrayList description) { this.check_btn = check_btn; this.id_list = id; this.category_list = category; this.date_list = date; this.price_list = price; this.payment_mode_list = payment_mode; this.description_list = description; this.mContext = cv; this.activity = activity; this.cursor = cursor; } @Override public int getCount() { return date_list.size(); } @Override public Object getItem(int position) { return date_list.get(position); } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View child, ViewGroup parent) { final Holder mHolder; LayoutInflater layoutInflater = null; if (child == null) { layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); child = layoutInflater.inflate(R.layout.activity_list_all_transaction, null); mHolder = new Holder(); mHolder.txt_category = (TextView) child.findViewById(R.id.category); mHolder.txt_date = (TextView) child.findViewById(R.id.date); mHolder.txt_id = (TextView) child.findViewById(R.id.id); mHolder.txt_price = (TextView) child.findViewById(R.id.price); mHolder.txt_payment = (TextView) child.findViewById(R.id.payment); mHolder.txt_description = (TextView) child.findViewById(R.id.description); child.setTag(mHolder); } else { mHolder = (Holder) child.getTag(); } mHolder.txt_category.setText(category_list.get(position)); mHolder.txt_date.setText(date_list.get(position)); mHolder.txt_id.setText(id_list.get(position)); mHolder.txt_price.setText(price_list.get(position)); mHolder.txt_payment.setText(payment_mode_list.get(position)); mHolder.txt_description.setText(description_list.get(position)); child.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Animation animation1 = new AlphaAnimation(0.3f, 1.0f); animation1.setDuration(2000); v.startAnimation(animation1); String price_lv = mHolder.txt_price.getText().toString(); String id_lv = mHolder.txt_id.getText().toString(); String category_lv = mHolder.txt_category.getText().toString(); String payment_lv = mHolder.txt_payment.getText().toString(); String date_lv = mHolder.txt_date.getText().toString(); String description_lv = mHolder.txt_description.getText().toString(); showCustomeDialogBox(mContext, false, price_lv, id_lv, category_lv, payment_lv, date_lv, description_lv); // notifyDataSetChanged(); } }); return child; } public class Holder { TextView txt_category; TextView txt_price; TextView txt_id; TextView txt_date; TextView txt_payment; TextView txt_description; } public static void showCustomeDialogBox(final Context context, final boolean activityVisibility,String price, final String id , String category , String payment , String date , String description ) { final Dialog dialog = new Dialog(context); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.setCancelable(true); dialog.setContentView(R.layout.activity_custom_dailog); Button positivebtn = (Button) dialog.findViewById(R.id.positive); Button negativebtn = (Button) dialog.findViewById(R.id.negative); LinearLayout category_lay = (LinearLayout) dialog.findViewById(R.id.category_lay); TextView dialog_title = (TextView) dialog.findViewById(R.id.dialog_title); TextView price_tv = (TextView) dialog.findViewById(R.id.price); TextView category_tv = (TextView) dialog.findViewById(R.id.category); TextView payment_tv = (TextView) dialog.findViewById(R.id.payment_mode); TextView date_tv = (TextView) dialog.findViewById(R.id.date); TextView description_tv = (TextView) dialog.findViewById(R.id.description); category_lay.setVisibility(View.GONE); dialog_title.setText(category); price_tv.setText(price); category_tv.setText(category); payment_tv.setText(payment); date_tv.setText(date); description_tv.setText(description); positivebtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (!check_btn) { Transactiondata delete = new Transactiondata(context); delete.open(); delete.deleteIncomeEntry(id); delete.close(); /*recreate all transaction Activity .... to refresh data ..........*/ activity.recreate(); dialog.dismiss(); } else { Transactiondata delete = new Transactiondata(context); delete.open(); delete.deleteExpenseEntry(id); delete.close(); /*recreate all transaction Activity .... to refresh data ..........*/ activity.recreate(); dialog.dismiss(); } } }); negativebtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); dialog.show(); Window window = dialog.getWindow(); window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); } }