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

我有一个xml布局,用户可以在其中输入多个设置。我的目标是能够保存这些设置并在以后检索它们-到目前为止,我已经成功地做到了。我有sqlite数据库,它存储几个值,包括时间选择器中的currentHour以及另一个xml布局,用于输出保存在数据库中的设置。

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

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

附言

对于如何完成此操作,我确实需要一个简单的答案(假设我什么都不知道),我已经尝试了几天,但仍需要帮助,以便将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(用于布局的Java文件,其中包含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;

       @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

最佳答案

您可以通过在数据库中保留一列和一个字符串来添加分钟。然后,在设置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以应对其他参数,但是在需要这些部分时避免分割字符串。

07-28 14:03