我的时间选择器工作得很好,但只有一种情况下它会产生错误的输出。如果我调用edit函数,并且所有变量都是从数据库中填充的,并且我不调用时间选择器(用这个函数点击按钮),它会用实际的系统值重写我的db值。我该怎么修?在我用picker点击按钮后,他们会显示我的数据库值,startactivity的textview显示数据库值,但在保存后,只有当我点击picker按钮(调用他)时,它才会保存数据库值。。。我如何设置值,然后我必须点击它,并且值保持在数据库中
public class PredmetPridajActivity extends Activity implements OnItemSelectedListener
{
//DateFormat fmtTime = DateFormat.getTimeInstance();
SimpleDateFormat fmtTime = new SimpleDateFormat("kk:mm");
SimpleDateFormat fmtDate2 = new SimpleDateFormat("yyyy-MM-dd kk:mm"); // upravit na den alebo v selecte lepsi order
TextView timeLabel;
Calendar time = Calendar.getInstance ();
public static final String ODOSLI = "odosli";
private DatabaseOp mDbHelper;
String username;
String odosli = "";
String predmet;
String obsah;
String poloha;
String vyucujuci;
String hodina;
String den;
int item;
String strItem;
EditText tv_nazov;
EditText tv_poznamka;
EditText tv_miesto;
EditText tv_vyucujuci;
private Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.predmet_pridaj);
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
spinner = (Spinner)findViewById(R.id.spinner_dni);
ArrayAdapter<?> adapter = ArrayAdapter.createFromResource(this, R.array.dni, android.R.layout.simple_spinner_dropdown_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
mDbHelper = new DatabaseOp(this);
mDbHelper.open();
timeLabel = (TextView)findViewById(R.id.time);
updateLabel();
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
username = sharedPrefs.getString("prefUsername", "NULL");
Intent i = getIntent();
if (i.hasExtra(ODOSLI))
odosli = i.getStringExtra(ODOSLI);
if (odosli.length()>0)
{
dbOperations();
tv_nazov = (EditText)findViewById(R.id.predmet);
tv_nazov.setText(predmet);
tv_poznamka = (EditText)findViewById(R.id.obsah);
tv_poznamka.setText(obsah);
tv_miesto = (EditText)findViewById(R.id.miesto);
tv_miesto.setText(poloha);
tv_vyucujuci = (EditText)findViewById(R.id.vyucujuci);
tv_vyucujuci.setText(vyucujuci);
timeLabel.setText(hodina);
spinner.setSelection(Integer.parseInt(den));
}
}
public void onItemSelected (AdapterView<?> parent, View view, int position, long id)
{
strItem = parent.getItemAtPosition (position).toString();
item = parent.getSelectedItemPosition();
}
public void onNothingSelected (AdapterView <?> parent)
{
}
protected void onPause(Bundle savedInstanceState)
{
mDbHelper.close();
}
public void buttonClicked_uloz(View button)
{
getData();
if (predmet.length()<1)
{
Toast.makeText(this, R.string.invalid_nazov, Toast.LENGTH_SHORT).show();
} else {
if (odosli.length()>0)
{
mDbHelper.updatePredmet(odosli, predmet, poloha, vyucujuci, hodina, obsah, item);
} else {
mDbHelper.createPredmet(username, predmet, obsah, poloha, vyucujuci, hodina, 1, item);
}
startActivity ();
}
}
public void dbOperations ()
{
mDbHelper = new DatabaseOp(this);
mDbHelper.open();
Cursor cursor = mDbHelper.fetchPredmet(odosli, 1);
startManagingCursor(cursor);
predmet = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseOp.KEY_NAZOV));
obsah = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseOp.KEY_POZNAMKA));
poloha = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseOp.KEY_POLOHA));
vyucujuci = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseOp.KEY_VYUCUJUCI));
hodina = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseOp.KEY_HODINA));
den = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseOp.KEY_DLZKA));
}
public void getData ()
{
EditText predmetInput = ((EditText)findViewById(R.id.predmet));
predmet = predmetInput.getText().toString();
EditText obsahInput = ((EditText)findViewById(R.id.obsah));
obsah = obsahInput.getText().toString();
EditText polohaInput = ((EditText)findViewById(R.id.miesto));
poloha = polohaInput.getText().toString();
EditText vyucujuciInput = ((EditText)findViewById(R.id.vyucujuci));
vyucujuci = vyucujuciInput.getText().toString();
hodina = fmtTime.format(time.getTime());
}
public void chooseTime (View v)
{
if (odosli.length()>0)
{
new TimePickerDialog (PredmetPridajActivity.this, t, Integer.parseInt(hodina.substring(0,2)), Integer.parseInt(hodina.substring(3,5)),true).show();
} else {
new TimePickerDialog (PredmetPridajActivity.this, t, time.get(Calendar.HOUR_OF_DAY),time.get(Calendar.MINUTE),true).show();
}
}
public void updateLabel()
{
timeLabel.setText(fmtTime.format(time.getTime()));
}
TimePickerDialog.OnTimeSetListener t = new TimePickerDialog.OnTimeSetListener()
{
public void onTimeSet (TimePicker view, int hourOfDay, int minute)
{
time.set(Calendar.HOUR_OF_DAY, hourOfDay);
time.set(Calendar.MINUTE, minute);
updateLabel();
}
};
public void startActivity ()
{
PredmetPridajActivity.this.finish();
}
}
试试这个,但这个设置当前时间就像默认的
SimpleDateFormat sdf = new SimpleDateFormat("hh:ss");
Date date = null;
try {
date = sdf.parse("07:00");
} catch (ParseException e) {
}
Calendar c = Calendar.getInstance();
c.setTime(date);
TimePicker picker = new TimePicker(getApplicationContext());
picker.setCurrentHour(c.get(Calendar.HOUR_OF_DAY));//your default hour
picker.setCurrentMinute(c.get(Calendar.MINUTE));// your default minute