0

所以......我正在构建一个小应用程序,当用户从 TimePicker 按钮设置时间​​值时存储时间值。由于 android TimePicker 有两个值 currentHour 和 currentMinute 我想我需要创建两个字符串(一个用于小时,一个用于分钟)然后将它们连接成一个可以显示为时间值的字符串。

很简单,对吧?

到目前为止,我已经提出了下面的源代码 - 我觉得我几乎将 TimePicker 连接到时间值的字符串,但是我有 3 个问题:

timePicker cannot be resolved   AddEditCountry.java line 104
minEdit cannot be resolved or is not a field    AddEditCountry.java line 37 
timePicker cannot be resolved   AddEditCountry.java line 103    

...更不用说 TimePicker 按钮不会更改(组合 currentHour 和 currentMinute)字符串。

^ - 大问题

AddEditCountry.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;
     private TimePicker minEt;
     public static final String KEY_BUNDLE_TIME = "time";
     public static final String KEY_BUNDLE_MIN = "min";

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


          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.containsKey(KEY_BUNDLE_TIME) ? extras.getInt(KEY_BUNDLE_TIME) : 0);
             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(),
                          timePicker.getCurrentHour().toString(),
                          timePicker.getCurrentMinute().toString());

              }
              else
              {
                 dbConnector.updateContact(rowID,
                    nameEt.getText().toString(),
                    capEt.getText().toString(),
                    timeEt.getCurrentHour().toString(),
                    minEt.getCurrentMinute().toString(),/* Storing as String*/
                    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 {

    public static final String KEY_BUNDLE_TIME = "time";
    public static final String KEY_BUNDLE_MIN = "min";
    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);
                  newCon.put(AddEditCountry.KEY_BUNDLE_TIME, time);
                  newCon.put(AddEditCountry.KEY_BUNDLE_MIN, min);

                  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);
                  editCon.put(AddEditCountry.KEY_BUNDLE_TIME, time);
                  editCon.put(AddEditCountry.KEY_BUNDLE_MIN, min);
                  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();
               }
}
4

1 回答 1

0

代码中有很多问题。

首先,您不需要单独的 TimePicker 小部件用于小时和分钟。所以从你的代码和布局文件中删除minEt 。

其次,timePicker变量没有在类中声明。请改用timeEt

第三,我在类中找不到minEdit变量,如果它在布局中声明,那么编译器不应该抱怨。无论如何删除它,因为你不需要它。

您可能想查看一个使用 TimePicker 的工作示例。看看这个

示例活动

public class TimePickerActivity extends Activity {

    public static final int DIALOG_TIME = 100;
    private TextView timeText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        timeText = new TextView(this);
        setContentView(timeText);
    }   

    @Override
    protected void onResume() {
        super.onResume();
        showDialog(DIALOG_TIME);
    }

    @Override
    protected Dialog onCreateDialog(final int id) {
        switch(id) {
        case DIALOG_TIME:
            Calendar cal = Calendar.getInstance();
            TimePickerDialog.OnTimeSetListener mTimeSetListener =
                new TimePickerDialog.OnTimeSetListener() {
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        timeText.setText(hourOfDay+":"+minute);
                    }
                };
            return new TimePickerDialog(this, mTimeSetListener, cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), true);
        }
        return super.onCreateDialog(id);
    }   
}
于 2013-03-25T04:28:27.377 回答