将Datepicker日期保存到数据库

时间:2016-03-06 01:08:04

标签: android sqlite datepicker

在寻找解决方案3天后,我希望有人可以在这里帮助我,我是Android开发的新手,我试图将我的datepicker对话框或我的datepicker小部件的日期保存到我的sqlite数据库,我没有错误,但它保存我的日期为12/31/1969无论我做什么这是我的DatePickerFragment类

     //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();
}

你可以看到我有一个textview显示选择日期没问题。 这是我添加到表格的DataBaseHelper代码。

    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();

}

这就是我试图保存日期的方式。

  @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)");

}

我的数据库表就是这样生成的

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

在我的getView方法中我试图将我的long格式化为像这样的字符串

count_odd_digits(n):

有人可以帮我弄清楚我在哪里犯了错误。

2 个答案:

答案 0 :(得分:1)

发现问题,非常感谢@Nolly j的帮助,在我的产品活动中,我有一个refreshData方法,将日期设置为字符串用于测试目的,忘记将其更改为long,这里是。< / p>

String inserttable = (
"insert into slam(mobileno , fname , sname , nname , telephone, address, email, bornon, crush, hm, fp, fa, fas, fs, ff, ambition, ayif, impbo, date)" + 
"values ('" + st + "' , '" + a + "' , '" + b + "' , '" + c + "' , " + d + " , '" + e + "' , '" + f + "' , '" + g + "' , '" + h + "' , '" + i + "' , '" + j + "' , '" + k + "' , '" + l + "' , '" + m + "' , '"+ o + "' , '"+ p + "' , '"+ q + "' , '"+ r + "');");

注释掉的陈述是问题所在。

答案 1 :(得分: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);
}
}

这是我的列表视图的getView代码

    @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;
}