How can I combine the currentMinute and currentHour into one text field so it displays in a normal time format [ex 9:30]-Collection of common programming errors
I have an application which stores the CurrentHour and CurrentMinute from a TimePicker in a sqlite database then displays it in another activity ViewCountry java
I would like to combine these two fields in and display them together 9:30 – for example in the
of view_country xml
add_country xml
=================
**ADDEDITCOUNTRY**
============================
quite possible minute values from TimePicker here are messing things up
import android.app.Activity;
import android.app.AlertDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.ViewGroup;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TimePicker;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.content.Context;
public class AddEditCountry extends Activity {
private long rowID;
private EditText nameEt;
private EditText capEt;
private EditText codeEt;
private TimePicker timeEt;
private TimePicker minEt;
private String cHour;
private String cMinute;
private String normalTimeFormat;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.add_country);
nameEt = (EditText) findViewById(R.id.nameEdit);
capEt = (EditText) findViewById(R.id.capEdit);
codeEt = (EditText) findViewById(R.id.codeEdit);
timeEt = (TimePicker) findViewById(R.id.timeEdit);
minEt = (TimePicker) findViewById(R.id.timeEdit);
Bundle extras = getIntent().getExtras();
if (extras != null)
{
rowID = extras.getLong("row_id");
nameEt.setText(extras.getString("name"));
capEt.setText(extras.getString("cap"));
codeEt.setText(extras.getString("code"));
timeEt.setCurrentHour(extras.getInt("time"));
minEt.setCurrentMinute(extras.getInt("min"));
}
Button saveButton =(Button) findViewById(R.id.saveBtn);
saveButton.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
if (nameEt.getText().length() != 0)
{
AsyncTask saveContactTask =
new AsyncTask()
{
@Override
protected Object doInBackground(Object... params)
{
saveContact();
return null;
}
@Override
protected void onPostExecute(Object result)
{
finish();
}
};
saveContactTask.execute((Object[]) null);
}
else
{
AlertDialog.Builder alert = new AlertDialog.Builder(AddEditCountry.this);
alert.setTitle(R.string.errorTitle);
alert.setMessage(R.string.errorMessage);
alert.setPositiveButton(R.string.errorButton, null);
alert.show();
}
}
});
}
private void saveContact()
{
DatabaseConnector dbConnector = new DatabaseConnector(this);
if (getIntent().getExtras() == null)
{
dbConnector.insertContact(nameEt.getText().toString(),
capEt.getText().toString(),
timeEt.getCurrentHour().toString(),
minEt.getCurrentMinute().toString(),
codeEt.getText().toString());
}
else
{
dbConnector.updateContact(rowID,
nameEt.getText().toString(),
capEt.getText().toString(),
String cHour = timeEt.getCurrentHour().toString(), /* Storing as String*/
String cMinute = minEt.getCurrentMinute().toString(),
String normalTimeFormat = cHour + ":" + cMinute;
//Do whatever you want to do with normalTimeFormat
codeEt.getText().toString());
}
}
}
**DATABASECONNECTOR JAVA**
=========================
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
public class DatabaseConnector {
private static final String DB_NAME = "WorldCountries";
private SQLiteDatabase database;
private DatabaseOpenHelper dbOpenHelper;
public DatabaseConnector(Context context) {
dbOpenHelper = new DatabaseOpenHelper(context, DB_NAME, null, 1);
}
public void open() throws SQLException
{
//open database in reading/writing mode
database = dbOpenHelper.getWritableDatabase();
}
public void close()
{
if (database != null)
database.close();
}
public void insertContact(String name, String cap, String code, String time, String min)
{
ContentValues newCon = new ContentValues();
newCon.put("name", name);
newCon.put("cap", cap);
newCon.put("time", time);
newCon.put("min", min);
newCon.put("code", code);
open();
database.insert("country", null, newCon);
close();
}
public void updateContact(long id, String name, String cap,String code, String time, String min)
{
ContentValues editCon = new ContentValues();
editCon.put("name", name);
editCon.put("cap", cap);
editCon.put("time", time);
editCon.put("min", min);
editCon.put("code", code);
open();
database.update("country", editCon, "_id=" + id, null);
close();
}
public Cursor getAllContacts()
{
return database.query("country", new String[] {"_id", "name"},
null, null, null, null, "name");
}
public Cursor getOneContact(long id)
{
return database.query("country", null, "_id=" + id, null, null, null, null);
}
public void deleteContact(long id)
{
open();
database.delete("country", "_id=" + id, null);
close();
}
}
**DATABASEOPENHELPER JAVA**
===============================
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseOpenHelper extends SQLiteOpenHelper {
public DatabaseOpenHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createQuery = "CREATE TABLE country (_id integer primary key autoincrement,name, cap, code, time, min);";
db.execSQL(createQuery);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
LOGCAT:
==========
03-22 14:35:00.174: I/Adreno200-EGLSUB(1320): : Format RGBA_8888.
03-22 14:35:00.174: D/memalloc(1320): ion: Mapped buffer base:0x5ca41000 size:614400 offset:0 fd:57
03-22 14:35:00.184: E/(1320): Can't open file for reading
03-22 14:35:00.184: E/(1320): Can't open file for reading
03-22 14:35:00.225: D/memalloc(1320): ion: Mapped buffer base:0x5d12e000 size:614400 offset:0 fd:61
03-22 14:35:02.087: D/Activity(1320): Activity.onPause(), editTextTapSensorList size: 0
03-22 14:35:02.127: I/Adreno200-EGLSUB(1320): : Format RGBA_8888.
03-22 14:35:02.127: D/memalloc(1320): ion: Mapped buffer base:0x5d4ce000 size:614400 offset:0 fd:64
03-22 14:35:02.227: D/memalloc(1320): ion: Mapped buffer base:0x5d6f7000 size:614400 offset:0 fd:71
03-22 14:35:02.247: D/memalloc(1320): ion: Unmapping buffer base:0x5ca41000 size:614400
03-22 14:35:02.247: D/memalloc(1320): ion: Unmapping buffer base:0x5d12e000 size:614400
03-22 14:35:02.917: D/memalloc(1320): ion: Mapped buffer base:0x5c929000 size:614400 offset:0 fd:56
03-22 14:35:04.319: D/Activity(1320): Activity.onPause(), editTextTapSensorList size: 0
03-22 14:35:04.509: I/Adreno200-EGLSUB(1320): : Format RGBA_8888.
03-22 14:35:04.509: D/memalloc(1320): ion: Mapped buffer base:0x5ca41000 size:614400 offset:0 fd:60
03-22 14:35:04.599: D/memalloc(1320): ion: Mapped buffer base:0x5dc02000 size:614400 offset:0 fd:75
03-22 14:35:04.619: D/memalloc(1320): ion: Mapped buffer base:0x5dca8000 size:614400 offset:0 fd:78
03-22 14:35:04.629: D/memalloc(1320): ion: Unmapping buffer base:0x5d4ce000 size:614400
03-22 14:35:04.629: D/memalloc(1320): ion: Unmapping buffer base:0x5d6f7000 size:614400
03-22 14:35:04.629: D/memalloc(1320): ion: Unmapping buffer base:0x5c929000 size:614400
03-22 14:35:08.433: W/dalvikvm(1320): threadid=12: thread exiting with uncaught exception (group=0x410889d8)
03-22 14:35:08.453: E/AndroidRuntime(1320): FATAL EXCEPTION: AsyncTask #2
03-22 14:35:08.453: E/AndroidRuntime(1320): java.lang.RuntimeException: An error occured while executing doInBackground()
03-22 14:35:08.453: E/AndroidRuntime(1320): at android.os.AsyncTask$3.done(AsyncTask.java:278)
03-22 14:35:08.453: E/AndroidRuntime(1320): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
03-22 14:35:08.453: E/AndroidRuntime(1320): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
03-22 14:35:08.453: E/AndroidRuntime(1320): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
03-22 14:35:08.453: E/AndroidRuntime(1320): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-22 14:35:08.453: E/AndroidRuntime(1320): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
03-22 14:35:08.453: E/AndroidRuntime(1320): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-22 14:35:08.453: E/AndroidRuntime(1320): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-22 14:35:08.453: E/AndroidRuntime(1320): at java.lang.Thread.run(Thread.java:856)
03-22 14:35:08.453: E/AndroidRuntime(1320): Caused by: java.lang.Error: Unresolved compilation problems:
03-22 14:35:08.453: E/AndroidRuntime(1320): String cannot be resolved to a variable
03-22 14:35:08.453: E/AndroidRuntime(1320): Syntax error on token "cHour", delete this token
03-22 14:35:08.453: E/AndroidRuntime(1320): String cannot be resolved to a variable
03-22 14:35:08.453: E/AndroidRuntime(1320): Syntax error on token "cMinute", delete this token
03-22 14:35:08.453: E/AndroidRuntime(1320): Syntax error, insert ")" to complete MethodInvocation
03-22 14:35:08.453: E/AndroidRuntime(1320): Syntax error, insert ";" to complete BlockStatements
03-22 14:35:08.453: E/AndroidRuntime(1320): String cannot be resolved to a variable
03-22 14:35:08.453: E/AndroidRuntime(1320): Syntax error on token ")", delete this token
03-22 14:35:08.453: E/AndroidRuntime(1320): at com.nfc.linkingmanager.AddEditCountry.saveContact(AddEditCountry.java:122)
03-22 14:35:08.453: E/AndroidRuntime(1320): at com.nfc.linkingmanager.AddEditCountry.access$1(AddEditCountry.java:103)
03-22 14:35:08.453: E/AndroidRuntime(1320): at com.nfc.linkingmanager.AddEditCountry$1$1.doInBackground(AddEditCountry.java:76)
03-22 14:35:08.453: E/AndroidRuntime(1320): at android.os.AsyncTask$2.call(AsyncTask.java:264)
03-22 14:35:08.453: E/AndroidRuntime(1320): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-22 14:35:08.453: E/AndroidRuntime(1320): ... 5 more
03-22 14:35:08.493: D/Activity(1320): Activity.onPause(), editTextTapSensorList size: 0
03-22 14:35:08.563: I/Adreno200-EGLSUB(1320): : Format RGBA_8888.
03-22 14:35:08.563: D/memalloc(1320): ion: Mapped buffer base:0x5d4bb000 size:614400 offset:0 fd:56
03-22 14:35:08.573: D/memalloc(1320): ion: Mapped buffer base:0x5d8f7000 size:614400 offset:0 fd:64
03-22 14:35:08.593: D/memalloc(1320): ion: Unmapping buffer base:0x5ca41000 size:614400
03-22 14:35:08.593: D/memalloc(1320): ion: Unmapping buffer base:0x5dc02000 size:614400
03-22 14:35:08.593: D/memalloc(1320): ion: Unmapping buffer base:0x5dca8000 size:614400
03-22 14:35:09.775: D/memalloc(1320): ion: Mapped buffer base:0x5ca41000 size:614400 offset:0 fd:60
Additional Info:
============
It only force closes after you click the save button in add_country:
Button android:id="@+id/saveBtn
-
JodaTime has a LocalTime class that can be constructed with hours and minutes. It also have a toString method that is able to generate a string for a specified pattern.
-
Your code has lot of compilation errors
, And one of them isYour Create table statement is wrong
String createQuery = "CREATE TABLE country (_id integer primary key autoincrement,name, cap, code, time, min);";
you have not specified the
type
of the other fields i.ename,cap,code, time and min
specify the type for them like this
String createQuery = "CREATE TABLE country (_id integer primary key autoincrement,name text, cap text , code text , time text , min text );";
second :
dbConnector.updateContact(rowID, nameEt.getText().toString(), capEt.getText().toString(), String cHour = timeEt.getCurrentHour().toString(), /* Storing as String*/ String cMinute = minEt.getCurrentMinute().toString(), String normalTimeFormat = cHour + ":" + cMinute; //Do whatever you want to do with normalTimeFormat codeEt.getText().toString());
this is not the way to call method. You can’t create a new string inside the method parameter. it should be like this
dbConnector.updateContact(rowID, nameEt.getText().toString(),capEt.getText().toString(),timeEt.getCurrentHour().toString()+":"+minEt.getCurrentMinute().toString(), codeEt.getText().toString());