所以...我正在构建一个小应用程序,当用户从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();
}
}
答案 0 :(得分:0)
代码中存在很多问题。
首先,您不需要小时和分钟的单独TimePicker小部件。因此,请从代码和布局文件中删除 minEt 。
其次, timePicker 变量未在类中声明。请改用 timeEt 。
第三,我在类中找不到 minEdit 变量,如果在布局中声明它,则编译器不应该抱怨。无论如何都要删除它,因为你不需要它。
您可能想看一下使用TimePicker的工作示例。检查this。
示例活动:
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);
}
}