0

我有一个应用程序,它将 TimePicker 中的 CurrentHour 和 CurrentMinute 存储在 sqlite 数据库中,然后将其显示在另一个活动 ViewCountry java

我想在 9:30 合并这两个字段并将它们一起显示 - 例如在

<TextView 
   android:id="@+id/codeText"
   style="@style/StyleText"/>

view_country xml

add_country xml
=================

        <?xml version="1.0" encoding="utf-8"?>
        <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" android:layout_weight="1">

        <LinearLayout android:id="@+id/linearLayout"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:orientation="vertical" 
          android:padding="5dp">

          <EditText android:id="@+id/nameEdit"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content" 
             android:imeOptions="actionNext" 
             android:hint="@string/name_hint"
             android:inputType="textPersonName|textCapWords"/>

          <EditText android:id="@+id/capEdit"
             android:layout_width="fill_parent" 
             android:layout_height="wrap_content" 
             android:imeOptions="actionNext" 
             android:hint="@string/cap_hint"
             android:inputType="textPersonName|textCapWords"/>

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Data Limit"
            android:textColor="#ffffff"
            android:textAppearance="?android:textAppearanceMedium" />

        <SeekBar
            android:id="@+id/seekBar1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1.0"
                android:gravity="left"
                android:textColor="#ffffff"
                android:text="10MB" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1.0"
                android:gravity="right"
                android:textColor="#ffffff"
                android:text="Unlimited Data" />
        </LinearLayout>

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Bandwidth Limit"
            android:textColor="#ffffff"
            android:textAppearance="?android:textAppearanceMedium" />

        <SeekBar
            android:id="@+id/seekBar1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1.0"
                android:gravity="left"
                android:textColor="#ffffff"
                android:text="10kbs" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1.0"
                android:textColor="#ffffff"
                android:gravity="right"
                android:text="Unlimited Bandwidth" />
        </LinearLayout>

        <TextView
            android:id="@+id/TextView02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:textAppearanceSmall" />

         <TextView
            android:id="@+id/TextView02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="WiFi Time Limit"
            android:textColor="#ffffff"
            android:textAppearance="?android:textAppearanceMedium" />

        <TimePicker
            android:id="@+id/timeEdit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:layout_weight="1.0" />

         <EditText
            android:id="@+id/codeEdit"
            android:inputType="textUri"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:lines="1"
            android:hint="@string/code_hint"
            android:imeOptions="actionNext" />

          <Button android:id="@+id/saveBtn" 
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_marginTop="15dp"
             android:layout_gravity="center_horizontal"
             android:text="@string/save_btn"/>
        </LinearLayout>
        </ScrollView>

**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<Object, Object, Object> saveContactTask = 
                           new AsyncTask<Object, Object, Object>() 
                           {
                              @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): <ConfigWindowMatch:2165>: 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): <ConfigWindowMatch:2165>: 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): <ConfigWindowMatch:2165>: 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): <ConfigWindowMatch:2165>: 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
4

2 回答 2

1

JodaTime有一个可以用小时和分钟构造的 LocalTime 类它还有一个 toString 方法,可以为指定的模式生成字符串。

于 2013-03-22T21:03:18.823 回答
1

Your code has lot of compilation errors, 其中之一是

您的 Create table 语句是错误的

String createQuery = "CREATE TABLE country (_id integer primary key autoincrement,name, cap, code,  time, min);";   

您尚未指定type其他字段,即name,cap,code, time and min

像这样为它们指定类型

String createQuery = "CREATE TABLE country (_id integer primary key autoincrement,name text, cap text , code text ,  time text , min text );";   

第二

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());

这不是调用方法的方式。您不能在方法参数内创建新字符串。应该是这样的

 dbConnector.updateContact(rowID, nameEt.getText().toString(),capEt.getText().toString(),timeEt.getCurrentHour().toString()+":"+minEt.getCurrentMinute().toString(), codeEt.getText().toString());
于 2013-03-22T21:03:27.540 回答