Greetings ... I would like someone to help me with a question about getting the last record of a table please. I have been using the following code but it is not working properly for me. Any help is very welcome, thanks in advance.
public class StatusFragment extends Fragment {
Handler handler;
double[] values;
ConnectionSQLiteHelper connectorStatus;
TextView voltBattery,currentBattery,tempBattery,powerBattery,impedBattery,healthBattery,averagePower,cycleCount,crankAmpere,memory;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_status, container, false);
connectorStatus = new ConnectionSQLiteHelper(getActivity(),"bd_batmon",null,1);
voltBattery = (TextView) v.findViewById(R.id.textVoltage);
currentBattery = (TextView) v.findViewById(R.id.textCurrent);
tempBattery = (TextView) v.findViewById(R.id.textTemperature);
powerBattery = (TextView) v.findViewById(R.id.textPower);
impedBattery = (TextView) v.findViewById(R.id.textImpedance);
healthBattery = (TextView) v.findViewById(R.id.textHealth);
averagePower = (TextView) v.findViewById(R.id.textAverage);
cycleCount = (TextView) v.findViewById(R.id.textCycle);
crankAmpere = (TextView) v.findViewById(R.id.textCrank);
memory = (TextView) v.findViewById(R.id.textMemory);
handler = new Handler();
values = new double[3];
handler.post(upDate);
return v;
}
private Runnable upDate = new Runnable() {
@Override
public void run() {
SQLiteDatabase db_History = connectorStatus.getReadableDatabase();
String get_History = "SELECT * FROM "+Utilities.TABLE_HISTORY+" WHERE "+Utilities.ID_HISTORY+"=(SELECT MAX("+Utilities.ID_HISTORY+") FROM "+Utilities.TABLE_HISTORY+")";
Cursor cursor = db_History.rawQuery( get_History, null );
if(cursor.moveToLast()) {
values[0] = cursor.getDouble( cursor.getColumnIndex( Utilities.VOLTAGE_BATTERY ) );
values[1] = cursor.getDouble( cursor.getColumnIndex( Utilities.CURRENT_BATTERY ) );
values[2] = cursor.getDouble( cursor.getColumnIndex( Utilities.TEMP_BATTERY ) );
cursor.close();
db_History.close();
}
Toast.makeText( getActivity().getApplicationContext(), values[0] + "-" + values[1] + "-" + values[2], Toast.LENGTH_SHORT ).show();
/*voltBattery.setText(String.valueOf(values[0]));
currentBattery.setText(String.valueOf(values[1]));
tempBattery.setText(String.valueOf(values[2]));
double power = values[0]*values[1];
double impedance = values[0]/values[1];
powerBattery.setText(String.valueOf(power));
impedBattery.setText(String.valueOf(impedance));*/
handler.postDelayed(this,3000);
}
};
}