I want to get the average of temperature and humidity in a day and last 6 days each (total 7 days) and also find the average of individual averages of this 7 days (total individual averages in 7 days / 7). I am attaching my code here` package com.blueserial;
import java.util.Calendar;
import java.util.UUID;
import android.app.ActionBar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class History_Main_Activity extends Activity{
public static final String DEVICE_EXTRA = "com.blueserial.SOCKET";
public static final String DEVICE_UUID = "com.blueserial.uuid";
public static final String BUFFER_SIZE = "com.blueserial.buffersize";
private BluetoothDevice mDevice;
private UUID mDeviceUUID;
private int mMaxChars = 50000;//Default
Button submit,weekly;
TextView date, temp, humid, summary, temptit, humidtit,weektemp,weekhumid,weeksummary;
ImageView calender;
SQLiteDatabase work_band;
private int mYear;
private int mMonth;
private int mDay;
String today, d1;
static final int DATE_DIALOG_ID = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
getActionBar().setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.activity_history__main_);
calender = (ImageView) findViewById(R.id.img_caldr);
date = (TextView) findViewById(R.id.txt_pick_date);
temp = (TextView) findViewById(R.id.avg_temp_value);
humid = (TextView) findViewById(R.id.avg_humid_value);
summary = (TextView) findViewById(R.id.summary);
submit = (Button) findViewById(R.id.btn_submit);
humidtit = (TextView) findViewById(R.id.avg_humidity);
temptit = (TextView) findViewById(R.id.avg_temp);
weekly=(Button)findViewById(R.id.btn_week);
weektemp=(TextView)findViewById(R.id.week_temp);
weekhumid=(TextView)findViewById(R.id.week_humid);
weeksummary=(TextView)findViewById(R.id.week_summary);
Intent intent = getIntent();
Bundle b = intent.getExtras();
//mDevice = b.getParcelable(DEVICE_EXTRA);
//mDeviceUUID = UUID.fromString(b.getString(DEVICE_UUID));
//mMaxChars = b.getInt(BUFFER_SIZE);
ActionBar ab = getActionBar();
ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#0B3B24"));
ab.setBackgroundDrawable(colorDrawable);
work_band =History_Main_Activity.this.openOrCreateDatabase("work_band", MODE_PRIVATE, null);
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
updateDisplay();
calender.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(DATE_DIALOG_ID);
}
});
submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
temptit.setVisibility(View.VISIBLE);
humidtit.setVisibility(View.VISIBLE);
temp.setVisibility(View.VISIBLE);
humid.setVisibility(View.VISIBLE);
summary.setVisibility(View.VISIBLE);
String dat = date.getText().toString();
try
{
work_band =History_Main_Activity.this.openOrCreateDatabase("work_band", MODE_PRIVATE, null);
Cursor curday = work_band.rawQuery("SELECT AVG(temp), AVG(humidity) FROM gps_track WHERE date LIKE '%"+dat+"%'", null);
while(curday.moveToNext()){
String avgtemp = curday.getString(curday.getColumnIndex("AVG(temp)"));
String avghumid = curday.getString(curday.getColumnIndex("AVG(humidity)"));
temp.setText(avgtemp);
humid.setText(avghumid);
double temp1 = Double.valueOf(avgtemp).doubleValue();//Integer.parseInt(temp.getText().toString());
if(temp1>=50){
summary.setText(" Extremely hot day");
}
else if(temp1>=30 && temp1<50){
summary.setText("Hot day");
}
else if(temp1>=25 && temp1<30){
summary.setText("broken clouds and Warm day");
}
else if(temp1>=18 && temp1<25){
summary.setText(" Passing clouds and Mild day");
}
else{
summary.setText("pretty cool day");
}
}
}
catch (Exception e) {
Toast.makeText(History_Main_Activity.this, "No records found ",Toast.LENGTH_SHORT).show();
}
}
});
weekly.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
String year=Integer.toString(mYear);
String month=Integer.toString(mMonth);
String day=Integer.toString(mDay);
String datw=""+year+"-"+month+"-"+day+"";
try
{
work_band =History_Main_Activity.this.openOrCreateDatabase("work_band", MODE_PRIVATE, null);
Cursor curday = work_band.rawQuery("select avg(temp),avg(humidity) from gps_track where date between date(date_sub('%"+datw+"%',interval 3 day)) and '%"+datw+"%' group by date(date)", null);
while(curday.moveToNext()){
double sumt = 0,sumh = 0;
String wavgtemp = curday.getString(curday.getColumnIndex("AVG(temp)"));
String wavghumid = curday.getString(curday.getColumnIndex("AVG(humidity)"));
double wavg_temp=Double.parseDouble(wavgtemp);
double wavg_humid=Double.parseDouble(wavghumid);
sumt=sumt+wavg_temp;
sumh=sumh+wavg_humid;
double weeksumt=sumt/3;
double weeksumh=sumh/3;
String weeklytemp= Double.toString(weeksumt);
String weeklyhumid= Double.toString(weeksumh);
weektemp.setText(weeklytemp);
weekhumid.setText(weeklyhumid);
double temp2 = Double.valueOf(weeklytemp).doubleValue();//Integer.parseInt(temp.getText().toString());
if(temp2>=50){
weeksummary.setText(" Extremely hot week");
}
else if(temp2>=30 && temp2<50){
weeksummary.setText("Hot week");
}
else if(temp2>=25 && temp2<30){
weeksummary.setText("broken clouds and Warm week");
}
else if(temp2>=18 && temp2<25){
weeksummary.setText(" Passing clouds and Mild week");
}
else{
weeksummary.setText("pretty cool week");
}
}
}catch (SQLException e) {
Toast.makeText(History_Main_Activity.this, "sql",Toast.LENGTH_SHORT).show();
}
catch (NullPointerException e) {
Toast.makeText(History_Main_Activity.this, "null pointer",Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
Toast.makeText(History_Main_Activity.this, "No records found ",Toast.LENGTH_SHORT).show();
}
}
});
}
public String currentDate() {
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
int month = mMonth + 1;
String formattedMonth = "" + mMonth;
String formattedDayOfMonth = "" + mDay;
if(month < 10){
formattedMonth = "0" + month;
}
if(mDay < 10){
formattedDayOfMonth = "0" + mDay;
}
today = formattedDayOfMonth + "/" + formattedMonth + "/" + mYear;
return today;
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this,
mDateSetListener,
mYear, mMonth, mDay);
}
return null;
}
private void updateDisplay()
{
int month = mMonth + 1;
String formattedMonth = "" + month;
String formattedDayOfMonth = "" + mDay;
if(month < 10){
formattedMonth = "0" + month;
}
if(mDay < 10){
formattedDayOfMonth = "0" + mDay;
}
d1 = formattedDayOfMonth + "/" + formattedMonth + "/" + mYear;
String date_txt = mYear + "-" + formattedMonth + "-" + formattedDayOfMonth;
//Toast.makeText(getApplicationContext(), d1, Toast.LENGTH_LONG).show();
date.setText(date_txt);
}
private DatePickerDialog.OnDateSetListener mDateSetListener =
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay();
}
};
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 true;
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
//Checking which menu item is Selected
switch (item.getItemId()) {
case R.id.action_search:
Intent exer = new Intent(History_Main_Activity.this, Homescreen.class);
// exer.putExtra(DEVICE_EXTRA, mDevice);
// exer.putExtra(DEVICE_UUID, mDeviceUUID.toString());
// exer.putExtra(BUFFER_SIZE, mMaxChars);
// startActivity(exer);
startActivity(exer);
finish();
break;
}
return super.onOptionsItemSelected(item);
}
}
` In above code, problem is in weekly.setOnClickListener body. It shows SQLException.Anybody please fix it up for me, thanks :)
Aucun commentaire:
Enregistrer un commentaire