Created
September 13, 2015 15:32
-
-
Save CloudyPadmal/5de045fafb26afd82779 to your computer and use it in GitHub Desktop.
AccountSystemV1.1 View Categories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.padmal.accountsystemv10; | |
import android.app.AlertDialog; | |
import android.content.DialogInterface; | |
import android.content.Intent; | |
import android.os.AsyncTask; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.view.ContextMenu; | |
import android.view.Menu; | |
import android.view.MenuInflater; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.widget.AdapterView; | |
import android.widget.ArrayAdapter; | |
import android.widget.ListView; | |
import android.widget.Toast; | |
import java.util.ArrayList; | |
/************************************ View Categories ********************************************** | |
* This shows a list view of existing categories with their descriptions. On long press of an entry | |
* enables user to perform Delete that entry or Update the values of that entry. | |
* | |
* 1. Upon Delete selected user will be prompt to confirm action. | |
* 2. Upon Update selected user will be directed to New Category view with details of that entry | |
* | |
* Once user deleted an entry, the list should be updated i.e. deleted item must be gone. | |
***************************************************************************************************/ | |
public class ViewCategories extends AppCompatActivity { | |
// Defining variables | |
private ListView viewcategorysummaryscrollview; | |
private DBHelper dbhelp; | |
private Bundle details_brought_in; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
// Get saved state | |
super.onCreate(savedInstanceState); | |
// Set view | |
setContentView(R.layout.activity_view_categories); | |
// Assigning variables to objects | |
viewcategorysummaryscrollview = (ListView) findViewById(R.id.viewcatsummary); | |
// Create a database object | |
dbhelp = new DBHelper(this); | |
// Registering for the context menu | |
registerForContextMenu(viewcategorysummaryscrollview); | |
// Get a bundle of information from previous view | |
details_brought_in = getIntent().getExtras(); | |
// View categories carries in information from New Recory Entry, else it is from a normal view | |
if (details_brought_in != null) { | |
// "From" data will be sent only from New Record view | |
if (details_brought_in.getString("From").equalsIgnoreCase("New Record")) { | |
handleNewEntry(); | |
} | |
} | |
// Then this is invoked by main menu | |
else { | |
// Set the list of categories using AsyncTask | |
new setCategoryList().execute("All"); | |
} | |
} | |
/*********************************Handle New Entry View Invokes *******************************/ | |
private void handleNewEntry() { | |
// Get the type of categories wanted | |
final String type_of_categories_wanted = details_brought_in.getString("Type"); | |
// Set the list accordingly | |
new setCategoryList().execute(type_of_categories_wanted); | |
/* If the previous view is from New Record, onItemSelected method will be called and a new | |
intent action will be coded to go back to the new record view and I will change settings in | |
New Record to set a special view to get details sent from here */ | |
viewcategorysummaryscrollview.setOnItemClickListener(new AdapterView.OnItemClickListener() { | |
@Override | |
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { | |
/* When an item is clicked, get the row and convert it to get the first word i.e. | |
category name and send it to New Record view and set spinner there */ | |
String row_selected[] = viewcategorysummaryscrollview.getItemAtPosition(position).toString().split("\\s+"); | |
// First item is the category name | |
String category_name = row_selected[0]; | |
Intent back_to_new_entry = new Intent(getApplicationContext(), NewRecordEntry.class); | |
back_to_new_entry.putExtra("From", "View Categories"); | |
back_to_new_entry.putExtra("Type", details_brought_in.getString("Type")); | |
back_to_new_entry.putExtra("Date", details_brought_in.getString("Date")); | |
back_to_new_entry.putExtra("Category", category_name); | |
back_to_new_entry.putExtra("Amount", details_brought_in.getString("Amount")); | |
finish(); | |
startActivity(back_to_new_entry); | |
} | |
}); | |
} | |
/************************************* Options Menu Stuff *************************************/ | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
getMenuInflater().inflate(R.menu.menu_view_categories, menu); | |
return true; | |
} | |
/************************************* Context Menu Stuff **************************************/ | |
@Override | |
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { | |
super.onCreateContextMenu(menu, v, menuInfo); | |
MenuInflater inflater = getMenuInflater(); | |
// Set view to long click menu created by me | |
inflater.inflate(R.menu.longclickmenu_update_or_delete, menu); | |
} | |
@Override | |
public boolean onContextItemSelected(MenuItem item) { | |
// Get adapter view's info | |
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); | |
// Get the position of the item in the list | |
int item_position = info.position; | |
// Split the row into gettable items and set them in a string | |
final String selected_row[] = viewcategorysummaryscrollview.getItemAtPosition(item_position).toString().split("\\s+"); | |
switch (item.getItemId()) { | |
// When Delete clicked ... | |
case R.id.longmenudelete: | |
// Build and create an alert view to get confirmation | |
new AlertDialog.Builder(ViewCategories.this) | |
.setTitle("Delete Category") | |
.setMessage("Are you sure you want to delete this category?") | |
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { | |
public void onClick(DialogInterface dialog, int which) { | |
// Perform deletion ... | |
if (dbhelp.deleteCategory(selected_row[0]) > 0) { | |
// Create a toast to show success | |
Toast.makeText(getApplicationContext(), selected_row[0] + " Deleted Successfully", Toast.LENGTH_LONG).show(); | |
// Calls AsyncTask to make the list again ... | |
new setCategoryList().execute("All"); | |
} else { | |
Toast.makeText(getApplicationContext(), "Operation Failed", Toast.LENGTH_LONG).show(); | |
} | |
} | |
}) | |
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() { | |
public void onClick(DialogInterface dialog, int which) { | |
// Cancelled | |
} | |
}) | |
.show(); | |
return true; | |
// When Update clicked ... | |
case R.id.longmenuupdate: | |
// Create a new intent to go to the Update Category class | |
Intent record_update = new Intent(getApplicationContext(), NewCategoryEntry.class); | |
// Send Type, Category and Description with it | |
record_update.putExtra("Category", selected_row[0]); | |
record_update.putExtra("Description", dbhelp.getCategoryDescription_String(selected_row[0])); | |
// Start update category | |
startActivity(record_update); | |
return true; | |
default: return super.onContextItemSelected(item); | |
} | |
} | |
/************************ ASYNC TASK to get totals from database ******************************/ | |
private class setCategoryList extends AsyncTask<String, Void, ArrayList<String>> { | |
@Override | |
protected ArrayList<String> doInBackground (String... Table_Name) { | |
// This will be called in two places depending on how this view was started. | |
// If it is from main menu, all categories must be shown ... | |
if (Table_Name[0].equalsIgnoreCase("All")) { | |
// Get the list of categories from database | |
return dbhelp.getAllSummary_Array("Category"); | |
} | |
// I invoke this view from New Record Entry sometimes. Then I need a specific type of | |
// categories only unlike before. | |
else { | |
// Get the list of type_of categories from database | |
return dbhelp.getThisTypeOfCategorySummary_Array(Table_Name[0]); | |
} | |
} | |
@Override | |
protected void onPostExecute (ArrayList<String> arrayList) { | |
// Assign the array list to an adapter | |
ArrayAdapter<String> adapter_of_categories = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, arrayList); | |
// Assign the adapter to the list view | |
viewcategorysummaryscrollview.setAdapter(adapter_of_categories); | |
} | |
} | |
/**********************************************************************************************/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment