Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.45 KB | None | 0 0
  1. import com.afollestad.materialdialogs.DialogAction;
  2. import com.afollestad.materialdialogs.MaterialDialog;
  3.  
  4. public class tab2income extends Fragment {
  5.  
  6. private static final String TAG = "tab2income";
  7. DatabaseHelper mDatabaseHelper;
  8. private ListView mListView;
  9. View rootView;
  10. Cursor incomedata;
  11. SimpleCursorAdapter sca;
  12.  
  13. @Override
  14. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  15. Bundle savedInstanceState) {
  16. rootView = inflater.inflate(R.layout.tab2income, container, false);
  17. return rootView;
  18. }
  19.  
  20. @Override
  21. public void onActivityCreated( Bundle savedInstanceState) {
  22. super.onActivityCreated(savedInstanceState);
  23. mListView = (ListView) rootView.findViewById(R.id.listViewincome);
  24. mListView.setEmptyView(rootView.findViewById(R.id.empty));
  25. mDatabaseHelper = new DatabaseHelper(getActivity());
  26.  
  27. populateListView();
  28. }
  29.  
  30. private void populateListView() {
  31. Log.d(TAG, "populateListView: Displaying data in the ListView.");
  32. incomedata = mDatabaseHelper.getincomeData();
  33. sca = new SimpleCursorAdapter(getActivity(), android.R.layout.simple_list_item_1, incomedata, new String[]{DatabaseHelper.INCOME_AMOUNT}, new int[]{android.R.id.text1}, 0);
  34. mListView.setAdapter(sca);
  35.  
  36. mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  37. @Override
  38. public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
  39. int csrpos = incomedata.getPosition();
  40. incomedata.moveToPosition(i);
  41. displayNoteDate(
  42. incomedata.getString(incomedata.getColumnIndex(DatabaseHelper.INCOME_NOTES)),
  43. incomedata.getString(incomedata.getColumnIndex(DatabaseHelper.INCOME_DATE)),
  44. incomedata.getString(incomedata.getColumnIndex(DatabaseHelper.INCOME_CATEGORY)),
  45. l);
  46. incomedata.moveToPosition(csrpos);
  47. }
  48. });
  49. }
  50.  
  51.  
  52. @Override
  53. public void onDestroy() {
  54. super.onDestroy();
  55. incomedata.close();
  56. }
  57.  
  58. public void displayNoteDate(String noteContent, String dateValue,String category, final long noteID) {
  59. MaterialDialog.Builder builder= new MaterialDialog.Builder(getActivity())
  60. .title("Income Information")
  61. .content("Category: "+category+"nNote: "+noteContent+"nDate: "+ dateValue)
  62. .positiveText("edit")
  63. .negativeText("delete")
  64. .neutralText("close")
  65. .onPositive(new MaterialDialog.SingleButtonCallback() {
  66. @Override
  67. public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
  68. }
  69. })
  70. .onNeutral(new MaterialDialog.SingleButtonCallback() {
  71. @Override
  72. public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
  73. }
  74. })
  75. .onNegative(new MaterialDialog.SingleButtonCallback() {
  76. @Override
  77. public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
  78. mDatabaseHelper.deleteincomeData(Long.toString(noteID));
  79. incomedata = mDatabaseHelper.getincomeData();
  80. sca.swapCursor(incomedata);
  81. }
  82. });
  83. builder.show();
  84. }
  85.  
  86. }
  87.  
  88. import android.content.ContentValues;
  89. import android.content.Context;
  90. import android.database.Cursor;
  91. import android.database.sqlite.SQLiteDatabase;
  92. import android.database.sqlite.SQLiteOpenHelper;
  93.  
  94. public class DatabaseHelper extends SQLiteOpenHelper {
  95. private static final String TAG = "DatabaseHelper";
  96.  
  97. public static final String DATABASE_NAME = "budget7.db";
  98. public static final String TABLE_NAME = "expense_table";
  99. public static final String TABLE_NAME2 = "income_table";
  100. public static final String COL_1 = "_id";
  101. public static final String COL_2 = "_id";
  102. public static final String EXPENSE_AMOUNT = "EXPENSE_AMOUNT";
  103. public static final String EXPENSE_DATE = "DATE";
  104. public static final String EXPENSE_NOTES = "NOTES";
  105. public static final String INCOME_AMOUNT = "INCOME_AMOUNT";
  106. public static final String INCOME_DATE = "DATE";
  107. public static final String INCOME_NOTES = "NOTES";
  108. public static final String INCOME_CATEGORY = "INCOME_CATEGORY";
  109. public static final String EXPENSE_CATEGORY = "EXPENSE_CATEGORY";
  110.  
  111. public DatabaseHelper(Context context) {
  112. super(context, DATABASE_NAME, null, 3);
  113. }
  114.  
  115. @Override
  116. public void onCreate(SQLiteDatabase db) {
  117. db.execSQL("create table " + TABLE_NAME + " (_id INTEGER PRIMARY KEY AUTOINCREMENT, EXPENSE_AMOUNT DOUBLE,DATE INTEGER,NOTES TEXT, EXPENSE_CATEGORY TEXT)");
  118. db.execSQL("create table " + TABLE_NAME2 + " (_id INTEGER PRIMARY KEY AUTOINCREMENT,INCOME_AMOUNT DOUBLE,DATE INTEGER,NOTES TEXT, INCOME_CATEGORY TEXT)");
  119.  
  120. }
  121.  
  122. @Override
  123. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  124. db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
  125. db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME2);
  126. onCreate(db);
  127. }
  128.  
  129. public boolean insertexpenseData(String amount_expense, String date_expense, String notes_expense, String category_expense) {
  130. SQLiteDatabase db = this.getWritableDatabase();
  131. ContentValues contentValues = new ContentValues();
  132. contentValues.put(EXPENSE_AMOUNT, amount_expense);
  133. contentValues.put(EXPENSE_DATE, date_expense);
  134. contentValues.put(EXPENSE_NOTES, notes_expense);
  135. contentValues.put(EXPENSE_CATEGORY, category_expense);
  136. long result = db.insert(TABLE_NAME, null, contentValues);
  137. if (result == -1)
  138. return false;
  139. else
  140. return true;
  141. }
  142.  
  143. public boolean insertincomeData(String amount_income, String date_income, String notes_income, String category_income) {
  144. SQLiteDatabase db = this.getWritableDatabase();
  145. ContentValues contentValues = new ContentValues();
  146. contentValues.put(INCOME_AMOUNT, amount_income);
  147. contentValues.put(INCOME_DATE, date_income);
  148. contentValues.put(INCOME_NOTES, notes_income);
  149. contentValues.put(INCOME_CATEGORY, category_income);
  150. long result = db.insert(TABLE_NAME2, null, contentValues);
  151. if (result == -1)
  152. return false;
  153. else
  154. return true;
  155. }
  156.  
  157. public Cursor getexpenseData() {
  158. SQLiteDatabase db = this.getWritableDatabase();
  159. Cursor res = db.rawQuery("select * from " + TABLE_NAME, null);
  160. return res;
  161. }
  162.  
  163. public Cursor getincomeData() {
  164. SQLiteDatabase db = this.getWritableDatabase();
  165. Cursor res = db.rawQuery("select * from " + TABLE_NAME2, null);
  166. return res;
  167. }
  168.  
  169.  
  170. public boolean updateexpenseData(String id, String amount, String date, String notes, String catagory_income) {
  171. SQLiteDatabase db = this.getWritableDatabase();
  172. ContentValues contentValues = new ContentValues();
  173. contentValues.put(COL_1, id);
  174. contentValues.put(EXPENSE_AMOUNT, amount);
  175. contentValues.put(EXPENSE_DATE, date);
  176. contentValues.put(EXPENSE_NOTES, notes);
  177. contentValues.put(EXPENSE_CATEGORY, catagory_income);
  178. db.update(TABLE_NAME, contentValues, "_id = ?", new String[]{id});
  179. return true;
  180. }
  181.  
  182. public boolean updateincomeData(String id, String amount, String date, String notes, String catagory_income) {
  183. SQLiteDatabase db = this.getWritableDatabase();
  184. ContentValues contentValues = new ContentValues();
  185. contentValues.put(COL_2, id);
  186. contentValues.put(INCOME_AMOUNT, amount);
  187. contentValues.put(INCOME_DATE, date);
  188. contentValues.put(INCOME_NOTES, notes);
  189. contentValues.put(INCOME_CATEGORY, catagory_income);
  190. db.update(TABLE_NAME2, contentValues, "_id = ?", new String[]{id});
  191. return true;
  192. }
  193.  
  194. public Integer deleteexpenseData(String _id) {
  195. SQLiteDatabase db = this.getWritableDatabase();
  196. return db.delete(TABLE_NAME, "_id = ?", new String[]{_id});
  197. }
  198.  
  199. public Integer deleteincomeData(String _id) {
  200. SQLiteDatabase db = this.getWritableDatabase();
  201. return db.delete(TABLE_NAME2, "_id = ?", new String[]{_id});
  202. }
  203.  
  204. public double getNetBudget() {
  205. SQLiteDatabase db = this.getWritableDatabase();
  206. String selectQuery = "SELECT TOTAL(INCOME_AMOUNT) - (SELECT TOTAL(EXPENSE_AMOUNT) FROM expense_table) FROM income_table";
  207. Cursor cursor = db.rawQuery(selectQuery, null);
  208. double netBudget = 0.00; // if there is no row, this will mean 0 is returned. You could also set it to -1, or throw an Exception if no record is returned
  209. if (cursor.moveToFirst()) {
  210. netBudget = cursor.getInt(0);
  211. }
  212. cursor.close();
  213. return netBudget;
  214. }
  215.  
  216. public double getTotalExpense() {
  217. SQLiteDatabase db = this.getWritableDatabase();
  218. String selectQuery = "SELECT TOTAL(EXPENSE_AMOUNT) FROM expense_table";
  219. Cursor cursor = db.rawQuery(selectQuery, null);
  220. double netExpense = 0.00; // if there is no row, this will mean 0 is returned. You could also set it to -1, or throw an Exception if no record is returned
  221. if (cursor.moveToFirst()) {
  222. netExpense = cursor.getInt(0);
  223. }
  224. cursor.close();
  225. return netExpense;
  226. }
  227.  
  228. public double getTotalIncome() {
  229. SQLiteDatabase db = this.getWritableDatabase();
  230. String selectQuery = "SELECT TOTAL(INCOME_AMOUNT) FROM income_table";
  231. Cursor cursor = db.rawQuery(selectQuery, null);
  232. double netIncome = 0.00; // if there is no row, this will mean 0 is returned. You could also set it to -1, or throw an Exception if no record is returned
  233. if (cursor.moveToFirst()) {
  234. netIncome = cursor.getInt(0);
  235. }
  236. cursor.close();
  237. return netIncome;
  238. }
  239.  
  240. public void deleteAllIncome() {
  241. SQLiteDatabase db = this.getWritableDatabase();
  242. db.delete(TABLE_NAME2, null, null);
  243. db.execSQL("delete from " + TABLE_NAME2);
  244. db.close();
  245. }
  246.  
  247. public void deleteAllExpense() {
  248. SQLiteDatabase db = this.getWritableDatabase();
  249. db.delete(TABLE_NAME, null, null);
  250. db.execSQL("delete from " + TABLE_NAME);
  251. db.close();
  252. }
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement