2010-06-24 94 views
1

這是我正在處理的一段代碼。我正在爲android製作應用程序,除了這一個問題以外,一切都很順利,谷歌也不是我的朋友。我強調了重要的部分。問題是當TextViews使用updateboxes()方法加載數據時,一切都很好。當他們加載了populateFields();方法時間或日期選擇器不會識別這些值,因爲它們直接來自數據庫,並且不像string updateBuilder那樣使用updateboxes()方法構建。時間/日期選擇器無法識別字符串

字符串不是我的強項,我唯一能想到的就是將字符串分解成單獨的mDay mMonth mYear值,然後通過updateBoxes()方法運行它們,但我認爲會有更簡單的方法。無論如何,我不知道該怎麼做。

下面是代碼:

package com.example.TimeClockAppreset; 


import java.util.Calendar; 
import android.app.Activity; 
import android.app.DatePickerDialog; 
import android.app.Dialog; 
import android.app.TimePickerDialog; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.DatePicker; 
import android.widget.Spinner; 
import android.widget.TextView; 
import android.widget.TimePicker; 
import android.widget.AdapterView.OnItemSelectedListener; 

public class TimesEdit extends Activity{ 

    private TimesDbAdapter mDbHelper; 
    private Long mRowId; 

    private TextView mDateBox; 
    private TextView mTimeBox; 

    private int mYear; 
    private int mMonth; 
    private int mDay; 
    private int mHour; 
    private int mMinute; 

    static final int TIME_DIALOG_ID = 0; 
    static final int DATE_DIALOG_ID = 1; 


     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      mDbHelper = new TimesDbAdapter(this); 
      mDbHelper.open(); 

      setContentView(R.layout.entry_edit); 

      mDateBox = (TextView) findViewById(R.id.DateBox); 
      mTimeBox = (TextView) findViewById(R.id.TimeBox); 

      Button changeTime = (Button) findViewById(R.id.changeTime); 
      Button changeDate = (Button) findViewById(R.id.changeDate); 
      Button confirmButton = (Button) findViewById(R.id.confirm); 

      Spinner spinner = (Spinner) findViewById(R.id.spinner); 
      ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
        this, R.array.ClockBox_array, android.R.layout.simple_spinner_item); 
      adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
      spinner.setAdapter(adapter); 

      spinner.setOnItemSelectedListener(new MyOnItemSelectedListener()); 

      mRowId = (savedInstanceState == null) ? null : 
       (Long) savedInstanceState.getSerializable(TimesDbAdapter.KEY_ROWID); 
      if (mRowId == null) { 
       Bundle extras = getIntent().getExtras(); 
       mRowId = extras != null ? extras.getLong(TimesDbAdapter.KEY_ROWID) 
             : null; 
      } 

      //updateBoxes(); 
      populateFields(); 

      changeTime.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) { 
        showDialog(TIME_DIALOG_ID); 
       } 
      }); 

      changeDate.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) { 
        showDialog(DATE_DIALOG_ID); 
       } 
      }); 

      confirmButton.setOnClickListener(new View.OnClickListener() { 

       public void onClick(View view) { 
        setResult(RESULT_OK); 

        finish(); 
       } 

      }); 
    } 

     //THIS GETS THE INFO AND POPULATES THE FIELDS 
     private void populateFields() { 
       if (mRowId != null) { 
        Cursor time = mDbHelper.fetchTime(mRowId); 
        startManagingCursor(time); 
        mDateBox.setText(time.getString(
           time.getColumnIndexOrThrow(TimesDbAdapter.KEY_DATE))); 
        mTimeBox.setText(time.getString(
          time.getColumnIndexOrThrow(TimesDbAdapter.KEY_TIME))); 
       } 
       else 
        updateBoxes(); 
      } 

      private void updateBoxes() { 
       final Calendar c = Calendar.getInstance(); 
       mYear = c.get(Calendar.YEAR); 
       mMonth = c.get(Calendar.MONTH); 
       mDay = c.get(Calendar.DAY_OF_MONTH); 
       mHour = c.get(Calendar.HOUR_OF_DAY); 
       mMinute = c.get(Calendar.MINUTE); 
       updateDateDisplay(); 
       updateTimeDisplay(); 
     } 

     private void updateDateDisplay() { 
       mDateBox.setText(
         new StringBuilder() 
           // Month is 0 based so add 1 
           .append(mMonth + 1).append("-") 
           .append(mDay).append("-") 
           .append(mYear).append(" ")); 
    } 

     private void updateTimeDisplay() { 
      mTimeBox.setText(
        new StringBuilder() 
        .append(pad(mHour)).append(":") 
        .append(pad(mMinute))); 
     } 

     private static String pad(int c) { 
       if (c >= 10) 
        return String.valueOf(c); 
       else 
        return "0" + String.valueOf(c); 
      } 

     public class MyOnItemSelectedListener implements OnItemSelectedListener { 

       public void onItemSelected(AdapterView<?> parent, 
        View view, int pos, long id) { 
       } 

       @SuppressWarnings("unchecked") 
       public void onNothingSelected(AdapterView parent) { 
        // Do nothing. 
       } 
      } 

     private TimePickerDialog.OnTimeSetListener mTimeSetListener = 
       new TimePickerDialog.OnTimeSetListener() { 
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 
         mHour = hourOfDay; 
         mMinute = minute; 
         updateTimeDisplay(); 
        } 
       }; 

     private DatePickerDialog.OnDateSetListener mDateSetListener = 
        new DatePickerDialog.OnDateSetListener() { 

         public void onDateSet(DatePicker view, int year, 
               int monthOfYear, int dayOfMonth) { 
          mYear = year; 
          mMonth = monthOfYear; 
          mDay = dayOfMonth; 
          updateDateDisplay(); 
         } 
        }; 

     @Override 
     protected Dialog onCreateDialog(int id) { 
         switch (id) { 
         case DATE_DIALOG_ID: 
          return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay); 
         case TIME_DIALOG_ID: 
          return new TimePickerDialog(this, mTimeSetListener, mHour, mMinute, false); 
         } 
         return null; 
        } 

     private void saveState() { 
       String date = mDateBox.getText().toString(); 
       String time = mTimeBox.getText().toString(); 
       String inOut = "NA"; 
       if (mRowId == null) { 
        long id = mDbHelper.createEntry(time, date, inOut); 
        if (id > 0) { 
         mRowId = id; 
        } 
       } else { 
        mDbHelper.updateTimeTest(mRowId, time, date); 
       } 
      } 

     @Override 
      protected void onSaveInstanceState(Bundle outState) { 
       super.onSaveInstanceState(outState); 
       saveState(); 
       outState.putSerializable(TimesDbAdapter.KEY_ROWID, mRowId); 
      } 

     @Override 
     protected void onPause() { 
       super.onPause(); 
       saveState(); 
      } 

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

} 

任何幫助,將不勝感激。考慮到其他一切都很好,這個問題讓我瘋狂。

回答

1

很好,長話短說,至於我可以告訴getMonth()方法不工作,除非你正在使用當前月份。它奇怪的getYear()和getDate()方法都可以工作,但他們已經改寫了getMonth()方法,這是因爲我理解的原因。

這是簡單的解決方案。

public class GetDateValues { private String date; private char dash;

public GetDateValues(String str, char sep) { 
    date = str; 
    dash = sep; 
} 

public String month() { 
    int dot = date.indexOf(dash); 
    return date.substring(0,dot); 
} 

public String day() { 
    int dot = date.lastIndexOf(dash); 
    int sep = date.indexOf(dash); 
    return date.substring(sep + 1, dot); 
} 

public String year() { 
    int sep = date.lastIndexOf(dash); 
    return date.substring(sep + 1, sep + 5); 
} 

}

現在你要重拍的雜色山雀採摘的字符串值。