package com.example.billtracking;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.ActionBar;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
public class Particular extends ActionBarActivity {
EditText edtDate;
Button bAccept;
Button bClear;
public EditText edtParticular;
public EditText edtQty;
public EditText edtPrice;
public ListView listView;
ListViewAdapterHelper adapter;
static Map<Integer, List<String>> map = new HashMap<Integer, List<String>>();
static int i = 1;
private MenuItem menuItem;
public EditText edtSearch;
private ProgressDialog prgDialog;
public static final int progress_bar_type = 0;
/*
* FragmentManager fm; private boolean isTaskRunning = false;
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ActionBar actionBar = getActionBar();
edtDate = (EditText) findViewById(R.id.date);
Date myDate = new Date();
edtDate.setText(new SimpleDateFormat("dd/MM/yyyy").format(myDate));
edtParticular = (EditText) findViewById(R.id.particular);
edtQty = (EditText) findViewById(R.id.qty);
edtPrice = (EditText) findViewById(R.id.price);
listView = (ListView) findViewById(R.id.list);
bAccept = (Button) findViewById(R.id.accept);
bClear = (Button) findViewById(R.id.clear);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME
| ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_CUSTOM);
bAccept.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listView.setAdapter(null);
// Create Inner Thread Class
Thread background = new Thread(new Runnable() {
public void run() {
DatabaseManager.initializeInstance(new DBHelper(
getApplicationContext()));
SQLiteDatabase dbSqlite = DatabaseManager.getInstance()
.openDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("particulars", edtParticular
.getText().toString().trim());
contentValues.put("qty", edtQty.getText().toString()
.trim());
contentValues.put("price", edtPrice.getText()
.toString().trim());
contentValues.put("date", edtDate.getText().toString()
.trim());
dbSqlite.insert("particular", null, contentValues);
DatabaseManager.getInstance().closeDatabase();
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(),
"Successfully inserted",
Toast.LENGTH_SHORT).show();
}
});
}
});
background.start();
List<String> arraylist = new ArrayList<String>();
arraylist.add(edtParticular.getText().toString().trim());
arraylist.add(edtQty.getText().toString().trim());
arraylist.add(edtPrice.getText().toString().trim());
arraylist.add(edtDate.getText().toString().trim());
map.put(i++, arraylist);
listView.setAdapter(new ListViewAdapterHelper(getBaseContext(),
map));
}
});
bClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (map.size() > 0) {
map.clear();
i = 1;
listView.setAdapter(null);
}
}
});
}
@Override
public void onResume() {
super.onResume();
if (map.size() > 0) {
listView.setAdapter(new ListViewAdapterHelper(getBaseContext(), map));
}
}
@Override
public void onPause() {
super.onPause();
listView.setAdapter(null);
}
@Override
public void onDestroy() {
super.onDestroy();
listView.setAdapter(null);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case progress_bar_type:
prgDialog = new ProgressDialog(this);
prgDialog.setMessage("Displaying Usage Statistics...");
prgDialog.setIndeterminate(false);
prgDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
prgDialog.setCancelable(true);
prgDialog.show();
return prgDialog;
default:
return null;
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.search_action:
menuItem = item;
menuItem.setActionView(R.layout.search_layout);
menuItem.collapseActionView();
edtSearch = (EditText) item.getActionView().findViewById(
R.id.txt_search);
edtSearch.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN)
&& (keyCode == KeyEvent.KEYCODE_ENTER)) {
if (!edtSearch.getText().toString().trim().equals("")) {
/*ThreadProcess tp = new ThreadProcess();
tp.execute(edtSearch.getText().toString().trim());*/
}
}
return false;
}
});
break;
default:
break;
}
return true;
}
/* class ThreadProcess extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
* int currentOrientation =
* getResources().getConfiguration().orientation; if
* (currentOrientation == Configuration.ORIENTATION_PORTRAIT) {
* setRequestedOrientation
* (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } else {
* setRequestedOrientation
* (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); }
showDialog(progress_bar_type);
}
@Override
protected void onPostExecute(String result) {
// setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
dismissDialog(progress_bar_type);
Intent intent = new Intent(Particular.this, SearchResultsActivity.class);
startActivity(intent);
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
try {
Thread.sleep(7000);
Map<String, Double> map = new HashMap<String, Double>();
DatabaseManager.initializeInstance(new DBHelper(
getApplicationContext()));
SQLiteDatabase dbSqlite = DatabaseManager.getInstance().openReadDatabase();
//where particulars=? ORDER BY date"
Cursor cursor = dbSqlite.rawQuery("select * from particular where particulars=? ORDER BY date", params);
Log.d("com.example.billtracking", "cursor getCount = "+cursor.getCount());
if (cursor.getCount() > 0) {
cursor.moveToFirst();
do {
String key = cursor.getString(cursor
.getColumnIndex("date"));
Double value = Double.parseDouble(cursor
.getString(cursor.getColumnIndex("qty")));
String[] sDate = key.split("/");
Double qtyTemp;
// Calculate total qty of an item within same month
if (map.containsKey(sDate[1])) {
qtyTemp = map.get(sDate[1]);
map.put(sDate[1], qtyTemp + value);
} else
map.put(sDate[1], value);
} while (cursor.moveToNext());
}
DatabaseManager.getInstance().closeDatabase();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}*/
}