-3

我有一个应用程序,它将 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>

添加国家

来自 TimePicker 的很可能的分钟值在这里搞砸了

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;

public class AddEditCountry extends Activity {

 private long rowID; 
 private EditText nameEt;
 private EditText capEt;
 private EditText codeEt;
 private TimePicker timeEt;
 private TimePicker minEt;

   @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(),
                timeEt.getCurrentHour().toString(), /* Storing as String*/
                minEt.getCurrentMinute().toString(), 
                codeEt.getText().toString());
          }
       }
}

数据库连接器 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();
           }
 }

数据库开放助手 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) {

}
 }

如何将 currentMinute 和 currentHour 合并到一个文本字段中,以便以正常时间格式显示 [ex 9:30]

在 fcm 的解决方案/建议之后更新:

JAVA

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

更新的日志:

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

信息

现在它的行为略有不同。现在它仅在您单击 add_country 中的保存按钮后才会强制关闭:

按钮 android:id="@+id/saveBtn

4

3 回答 3

0

您可以使用格式化的字符串:

int hour = 9;
int minute = 30;
String format = "%d:%d";
String time = String.format (format, hour, minute);

yourTextView.setText (time);
于 2013-03-22T13:32:25.367 回答
0

我可能误解了,它可能比这复杂得多,但是......

String normalTimeFormat = CurrentHour + ":" + CurrentMinute;

private void saveContact() 
       {

          DatabaseConnector dbConnector = new DatabaseConnector(this);

          //Here's the new change
          String cHour = timeEt.getCurrentHour().toString();
          String cMinute = minEt.getCurrentMinute().toString();
          String normalTimeFormat = cHour + ":" + cMinute;

          //Do whatever with normalTimeFormat now

          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(),
                      codeEt.getText().toString());
                }
       }
于 2013-03-22T13:31:05.230 回答
0

可能我错过了一些实现细节,但你可以只存储System.currentTimeinMillis()然后做类似的事情

Date date = new Date(timeStampFromSQLite);
String currentDate = date.getHours() + ":" + date.getMinutes();
于 2013-03-22T13:36:34.523 回答