将TimePicker数据保存在sqlite数据库中

时间:2013-03-25 08:10:05

标签: java android eclipse sqlite timepicker

好的 - 所以我现在真的迷失了......

我有一个xml布局,用户可以在其中输入多个设置。我的目标是能够保存这些设置并在以后检索它们 - 直到这一点我已成功完成。我有sqlite数据库,它存储了几个值,包括来自timepicker的currentHour以及另一个输出保存在数据库中的设置的xml布局。

目前 - timePicker只将currentHour保存到数据库,然后保存到我用来在view_country XML文件中显示它的字符串。

我想要做的是显示整个时间 - 不仅仅是currentHour(例如currentHour:currentMinute以显示4:00 AM的全部时间)作为一个字符串我可以用来显示另一个xml的时间布局。

P.S。

我真的需要一个直截了当的答案,如何做到这一点(假设我什么都不知道)我已经尝试了几天,我仍然需要帮助将currentMinute显示在currentHour旁边。

我已经做了几次尝试,但是此时我觉得最好恢复到我最后一个已知的工作状态(当尝试连接这两个字段的不同方法时,我已经得到了强制关闭问题)并要求救命。 (我知道存储和显示TimePicker的时间值并不困难 - 特别是对于一些技术娴熟的StackOverflow忍者来说)

add_country.xml(TimePicker按钮所在的位置)

<?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.java(包含TimePicker按钮的布局的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;

public class AddEditCountry extends Activity {

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

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



          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"));    
             String time = extras.getInt("time");
             String[] parts = time.split(":");
             timeEt.setCurrentHour(parts[0]);
             timeEt.setCurrentMinute(parts[1]);
          }

          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() + ":"
                              + timeEt.getCurrentMinute().toString(),
                          codeEt.getText().toString());
              }
              else
              {
                 dbConnector.updateContact(rowID,
                         capEt.getText().toString(),
                         timeEt.getCurrentHour().toString() + ":"
                             + timeEt.getCurrentMinute().toString(),
                         codeEt.getText().toString());
              }
           }
}

DatbaseConnector.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) 
               {
                  ContentValues newCon = new ContentValues();
                  newCon.put("name", name);
                  newCon.put("cap", cap);
                  newCon.put("time", time);
                  newCon.put("code", code);

                  open();
                  database.insert("country", null, newCon);
                  close();
               }


               public void updateContact(long id, String name, String cap,String code, String time) 
               {
                  ContentValues editCon = new ContentValues();
                  editCon.put("name", name);
                  editCon.put("cap", cap);
                  editCon.put("time", time);
                  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();
               }
}

view_country.xml(此时显示currentHour - 但需要将currentHour和currentMinute一起显示为一个我可以显示为时间的字符串)

<?xml version="1.0" encoding="utf-8"?>

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
android:stretchColumns="1" 
android:layout_margin="5dp">
<TableRow>         
  <TextView 
    style="@style/StyleLabel"
    android:text="@string/name_lbl"/>
 <TextView 
    android:id="@+id/nameText" 
    style="@style/StyleText"/>         
 </TableRow>

 <TableRow>         
 <TextView 
    style="@style/StyleLabel"
    android:text="@string/cap_lbl"/>         
 <TextView 
    android:id="@+id/capText"
    style="@style/StyleText"/>         
 </TableRow>

  <TableRow>       
  <TextView 
    style="@style/StyleLabel"
    android:text="Time Limit"/>                    
 <TextView 
    android:id="@+id/codeText"
    style="@style/StyleText"/>         
 </TableRow>
 <TableRow>         
 <TextView 
    style="@style/StyleLabel"
    android:text="Linked Users"/>         
 <TextView 
android:id="@+id/codeText"
    style="@style/StyleText"/>         
 </TableRow>
 <TableRow>         
 <TextView 
    style="@style/StyleLabel"
    android:text="@string/code_lbl"/>         
 <TextView 
    android:id="@+id/timeEdit"
    style="@style/StyleText"/>  

</TableRow>


</TableLayout>

logcat的:

03-25 12:50:45.175: D/Activity(10602): Activity.onPause(), editTextTapSensorList size: 0
03-25 12:50:45.265: I/Adreno200-EGLSUB(10602): <ConfigWindowMatch:2165>: Format RGBA_8888.
03-25 12:50:45.276: D/memalloc(10602): ion: Mapped buffer base:0x5ca42000 size:614400 offset:0 fd:57
03-25 12:50:45.276: E/(10602): Can't open file for reading
03-25 12:50:45.276: E/(10602): Can't open file for reading
03-25 12:50:45.316: D/memalloc(10602): ion: Mapped buffer base:0x5d12f000 size:614400 offset:0 fd:61
03-25 12:50:45.326: W/IInputConnectionWrapper(10602): showStatusIcon on inactive InputConnection
03-25 12:50:45.326: W/IInputConnectionWrapper(10602): getExtractedText on inactive InputConnection
03-25 12:50:47.248: D/memalloc(10602): ion: Unmapping buffer  base:0x5ca42000 size:614400
03-25 12:50:47.248: D/memalloc(10602): ion: Unmapping buffer  base:0x5d12f000 size:614400
03-25 12:50:47.288: D/Activity(10602): Activity.onPause(), editTextTapSensorList size: 0
03-25 12:50:47.318: I/Adreno200-EGLSUB(10602): <ConfigWindowMatch:2165>: Format RGBA_8888.
03-25 12:50:47.318: D/memalloc(10602): ion: Mapped buffer base:0x5cd67000 size:614400 offset:0 fd:58
03-25 12:50:47.388: D/memalloc(10602): ion: Mapped buffer base:0x5d05f000 size:614400 offset:0 fd:63
03-25 12:50:47.388: W/IInputConnectionWrapper(10602): getExtractedText on inactive InputConnection
03-25 12:50:48.549: D/memalloc(10602): ion: Mapped buffer base:0x5d12f000 size:614400 offset:0 fd:66

1 个答案:

答案 0 :(得分:2)

您可以通过在数据库中保留一列和一个字符串来添加分钟。然后,在设置time

时,您必须拆分TimePicker字符串
String time = extras.getString("time");
String[] parts = time.split(":");
timeEt.setCurrentHour(Integer.valueOf(parts[0]));
timeEt.setCurrentMinute(Integer.valueOf(parts[1]));

并在插入或更新时将其连接起来

dbConnector.insertContact(nameEt.getText().toString(),
                          capEt.getText().toString(),
                          timeEt.getCurrentHour().toString() + ":"
                              + timeEt.getCurrentMinute().toString(),
                          codeEt.getText().toString());

或单独hourminute。这意味着扩展数据库模式和DatabaseConnector以应对其他参数,但在需要部件时避免拆分字符串。