2016-03-06 142 views
0

尋找解決方案3天后我希望有人可以幫助我在這裏,我是一個新的android開發,我試圖保存datepicker對話框或我的datepicker部件的日期爲我的SQLite數據庫,我沒有得到任何錯誤,但它救了我的日期,1969年12月31日,無論我做什麼,這是我的DatePickerFragment類保存約會日期到數據庫

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { 

    private Calendar dateTime = Calendar.getInstance(); 


    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     // Using current date as start Date 
     final Calendar c = Calendar.getInstance(); 
     int year = c.get(Calendar.YEAR); 
     int month = c.get(Calendar.MONTH); 
     int day = c.get(Calendar.DAY_OF_MONTH); 

     // Get DatePicker Dialog 
     return new DatePickerDialog(getActivity(), this, year, month, day); 
    } 


    @Override 
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { 

     TextView chooseDateLabel = (TextView)getActivity().findViewById(R.id.chooseDateLabel); 

     dateTime.set(year, monthOfYear, dayOfMonth); 

     final int myYear = year; 
     final int myMonth = monthOfYear; 
     final int myDay = dayOfMonth; 

     chooseDateLabel.setText(new StringBuilder().append(myMonth + 1).append("/").append(myDay) 
       .append("/").append(myYear).append(" ")); 
     DatePicker datePicker = (DatePicker)getActivity().findViewById(R.id.saveJobDatePicker); 

     datePicker.init(myYear, myMonth, myDay, new DatePicker.OnDateChangedListener() { 
      @Override 
      public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { 
       year = myYear; 
       monthOfYear = myMonth; 
       dayOfMonth = myDay; 

       ContentValues values = new ContentValues(); 
       values.put("productDate", year + monthOfYear + dayOfMonth); 


      } 
     }); 


     ContentValues values = new ContentValues(); 
     values.put("date", myDay + myMonth + myYear); 

    } 
} 

,你可以看到我有沒有顯示已選定的日期一個TextView問題。 這是我添加到表的DataBaseHelper代碼。

 //add content to table 
    public void addProduct(MyProduct product){ 

    SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues values = new ContentValues(); 
    values.put(PRODUCT_NAME, product.getproductName()); 
    values.put(PRODUCT_NUMBER, product.getproductNumber()); 
    values.put(PRODUCT_WEIGHT, product.getproductWeight()); 
    values.put(PRODUCT_QTY, product.getproductQTY()); 
    values.put(DATE_NAME, product.getproductDate()); 

    db.insert(TABLE_NAME, null, values); 


    Log.v("Wish successfully!", "yeah!!"); 

    db.close(); 
} 

這就是我試圖保存日期的方法。是這樣

@Override 
public void onCreate(SQLiteDatabase db) { 
    //create our table 

    db.execSQL("CREATE TABLE " + TABLE_NAME+ "(ID INTEGER PRIMARY KEY AUTOINCREMENT, PRODUCTNAME TEXT, PRODUCTNUMBER INTEGER, PRODUCTQTY REAL, PRODUCTWEIGHT REAL, PRODUCTDATE LONG, PRODUCTCOMPLETED INTEGER)"); 

} 

我getView方法中我'嘗試我的長格式化字符串等產生

private void saveProductDB() { 


    Intent intent = getIntent(); 
    final Double myLabelResults = intent.getDoubleExtra("totalQTY", 0.0); 

    final MyProduct product = new MyProduct(); 
    product.setproductName(saveproductNameText.getText().toString().trim()); 
    product.setproductNumber(Integer.valueOf(saveproductNumberText.getText().toString().trim())); 
    product.setproductWeight(myLabelResults); 
    product.setproductQTY(myLabelResults/14); 
    product.getproductPavingOnLabel(); 

    saveProductDate = (DatePicker)findViewById(R.id.saveProductDatePicker); 

    Calendar calendar = Calendar.getInstance(); 

    int day = saveProductDate.getDayOfMonth(); 
    int month = saveProductDate.getMonth(); 
    int year = saveProductDate.getYear(); 

    calendar.set(day, month, year); 
    long myLongDate = calendar.getTimeInMillis(); 


    product.setProductDate(myLongDate); 

    dba.Product(product); 
    dba.close(); 

    saveproductNameText.setText(""); 
    saveproductNumberText.setText(""); 

    Intent i = new Intent(SaveProduct.this, MyProductActivity.class); 
    startActivity(i); 
    //Toast.makeText(getApplicationContext(),"Product Saved",  Toast.LENGTH_LONG).show(); 

} 

我的數據庫表這個

long dateFromProductDate = holder.myProduct.getProductDate(); 
String dateString = new SimpleDateFormat("MM/dd/yyyy").format(new Date(dateFromProductDate)); 
holder.mJobsDate.setText(dateString); 

有人可以幫我找出我在哪裏犯錯,謝謝。

+0

「無論我做什麼,它都將我的日期保存爲12/31/1969」 - 您想如何保存日期? – Inducesmile

+0

對不起,我的意思是,這是不是從datepicker保存日期和其默認爲12/31/1969,並忽略日期選擇器上的選定日期 – Vacano

回答

1

發現問題,感謝getView代碼很多@Nolly j表示的幫助下,我的產品活動中我有一個refreshData方法有日期爲測試目的而設置爲字符串,並忘記將其更改爲長,在這裏。

private void refreshData() { 
    dbProducts.clear(); 
    dba = new DatabaseHandler(getApplicationContext()); 

    ArrayList<MyProduct> productsFromDB = dba.getProducts(); 

    for (int i = 0; i < productsFromDB.size(); i++){ 

     String productName = productsFromDB.get(i).getProductName(); 
     int productNumber = productsFromDB.get(i).getProductNumber(); 
     Double productTons = productsFromDB.get(i).getProductWeight(); 
     Double productTrucks = productsFromDB.get(i).getProductQTY(); 
     //String productDate = productsFromDB.get(i).getProductBirthDate(); 
     long productDate = productsFromDB.get(i).getProductDate(); 

     MyProduct myProduct = new MyProduct(); 
     myProduct.setProductName(productName); 
     myProduct.setProductNumber(productNumber); 
     myProduct.setProductTons(productWeight); 
     myProduct.setProductTrucks(productQTY); 
     myProduct.setProductDate(productDate); 
     //myJob.setProductDate(ProductDate); 

     dbProducts.add(myProduct); 
    } 

註釋掉的語句是問題。

0

我寫了一個簡單的代碼,可以讓你朝正確的方向發展。選定的日期時間是全球性的,並且可以訪問。

import android.app.DatePickerDialog; 
import android.app.Dialog; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.v4.app.DialogFragment; 
import android.support.v7.app.AppCompatActivity; 
import android.widget.DatePicker; 
import android.widget.TextView; 

import java.util.Calendar; 

public class MainActivity extends AppCompatActivity { 

private static TextView chooseDateLabel; 

private DatePickerFragment datePickerFragment; 
private static Calendar dateTime = Calendar.getInstance(); 

protected static int mYear; 
protected static int mMonth; 
protected static int mDay; 

private static long dateInMilliSeconds; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    chooseDateLabel = (TextView)findViewById(R.id.chooseDateLabel); 

    datePickerFragment = new DatePickerFragment(); 
    datePickerFragment.show(getSupportFragmentManager(), "My Date Picker"); 

} 

public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     // Using current date as start Date 
     final Calendar c = Calendar.getInstance(); 
     mYear = c.get(Calendar.YEAR); 
     mMonth = c.get(Calendar.MONTH); 
     mDay = c.get(Calendar.DAY_OF_MONTH); 

     // Get DatePicker Dialog 
     return new DatePickerDialog(getActivity(), this, mYear, mMonth, mDay); 
    } 

    @Override 
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { 

     mYear = year; 
     mMonth = monthOfYear; 
     mDay = dayOfMonth; 

     chooseDateLabel.setText(String.valueOf(mDay) + "/" + String.valueOf(mMonth) + "/" + String.valueOf(mYear)); 

     dateTime.set(mYear, monthOfYear, dayOfMonth); 
     dateInMilliSeconds = dateTime.getTimeInMillis(); 
    } 
} 

private void saveProductDB(){ 

    Intent intent = getIntent(); 
    final Double myLabelResults = intent.getDoubleExtra("totalQTY", 0.0); 

    final MyProduct product = new MyProduct(); 
    product.setproductName(saveproductNameText.getText().toString().trim()); 
    product.setproductNumber(Integer.valueOf(saveproductNumberText.getText().toString().trim())); 
    product.setproductWeight(myLabelResults); 
    product.setproductQTY(myLabelResults/14); 
    product.setProductDate(dateInMilliSeconds); 
    product.getproductPavingOnLabel(); 

    dba.Product(product); 
    dba.close(); 

    saveproductNameText.setText(""); 
    saveproductNumberText.setText(""); 

    Intent i = new Intent(SaveProduct.this, MyProductActivity.class); 
    startActivity(i); 
} 
} 

這是我的列表視圖

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     View row = convertView; 
     //ViewHolder holder = null; 

     if(row == null || (row.getTag()) == null){ 

      LayoutInflater inflater = LayoutInflater.from(activity); 
      row = inflater.inflate(layoutResorce, null); 
      holder = new ViewHolder(); 

      holder.mProductName = (TextView) row.findViewById(R.id.ProductNameLabel); 
      holder.mProductNumber = (TextView) row.findViewById(R.id.ProductNumberLabel); 
      holder.mProductWeight = (TextView) row.findViewById(R.id.WeightLabel); 
      holder.mProductQTY = (TextView) row.findViewById(R.id.QTYLable); 
      holder.mProductDate = (TextView) row.findViewById(R.id.dateLabel);     

      row.setTag(holder); 
     }else{ 

      holder = (ViewHolder) row.getTag(); 
     } 

     holder.myProduct = getItem(position); 

     holder.mProductName.setText(holder.myProduct.getProductName()); 
     holder.mProductNumber.setText(String.valueOf(holder.myProduct.getProductNumber())); 
     holder.mProductWeight.setText(String.format("%.1f", holder.myProduct.getProductWeight()) + " Weight"); 
     holder.mProductQTY.setText(String.format("%.1f", holder.myProduct.getProductQTY()) + " QTY"); 

     long dateFromProductDate = holder.myProduct.getProductDate(); 
     String dateString = new SimpleDateFormat("MM/dd/yyyy").format(new Date(dateFromProductDate)); 
     holder.mProductDate.setText(dateString); 

     return row; 
    } 

    //get all products 
public ArrayList<MyProduct> getProducts(){ 

    //String selectQuery = "SELECT * FROM " + TABLE_NAME; 
    SQLiteDatabase db = this.getReadableDatabase(); 

    Cursor cursor = db.query(TABLE_NAME, new String[]{ PRODUCT_NAME, PRODUCT_NUMBER, PRODUCT_QTY, 
      PRODUCT_WEIGHT, DATE_NAME}, null, null, null, null, "PRODUCTDATE DESC"); 

    //loop through cursor 
    if(cursor.moveToFirst()){ 
     do{ 

      MyProduct job = new MyProduct(); 
      product.setProductName(cursor.getString(cursor.getColumnIndex(PRODUCT_NAME))); 
      product.setProductNumber(cursor.getInt(cursor.getColumnIndex(PRODUCT_NUMBER))); 
      product.setProductWeight(cursor.getDouble(cursor.getColumnIndex(PRODUCT_WEIGHT))); 
      product.setProductQTY(cursor.getDouble(cursor.getColumnIndex(PRODUCT_QTY))); 
      //product.setProductDate(cursor.getLong(cursor.getColumnIndex(DATE_NAME))); 

      long milliSeconds = product.setProductDate(cursor.getLong(product.getProductDate())); 
      DateFormat df = new SimpleFormat("MM/dd/yyyy"); 
      String dateString = df.format(new Date(milliSeconds)).toString(); 

      //java.text.DateFormat dateFormat = java.text.DateFormat.getDateInstance(); 
      //String dateData = dateFormat.format(new Date(cursor.getLong(cursor.getColumnIndex(DATE_NAME))).getTime()); 
      product.setProductDate(dateString); 

      productList.add(product); 

     }while(cursor.moveToNext()); 
    } 
    cursor.close(); 
    db.close(); 

    return productList; 
} 
+0

以及它似乎我得到一個值,dateInMilliSeconds現在= 1473136589551謝謝,但仍然是我的listview顯示12/31/1969,我會發布我的getView代碼。 – Vacano

+0

您需要將此值插入到數據庫中,當您從數據庫檢索數據時,您將其格式化爲所需的日期格式。 – Inducesmile

+0

這些值在數據庫中,以確保我導出數據庫,並發現所有的日期只是無法弄清楚如何格式化和顯示在我的列表視圖我試過DateFormat,但我仍然不能。 – Vacano

相關問題