【问题标题】:date store in database as string ,how do I retrieve it?日期作为字符串存储在数据库中,我如何检索它?
【发布时间】:2014-01-28 20:10:48
【问题描述】:

我正在使用日期选择器来选择我的日期,之后,它将作为字符串存储到我的数据库中。

我可以知道如何将数据库中的日期作为字符串取出吗?

这是因为, 我需要做一个摘要页面,将所有数据传递给下一个意图。 意思是,我需要将视图中的数据全部传递到我的摘要页面中,以便在摘要页面中包含所有数据。 这是为了让我能够轻松过滤我们的日期。

例如, 如果日期 - 27/12/2014,它将显示结果列表。

我快速检查了我的数据,看看我是否能够检查结果。 我在我的 summary.java 中放了一个日期选择器,它位于 editText(monthDate) 中, 所以如果monthDate = bundleDate(这是我从查看全部传递的所有数据结果), 它将开始一个新的意图。 否则,它将转到关于页面。

但是,我的代码跳过了 IF 部分, 即使我选择了正确的日期,它也会通过 else if 代码而不是 else 代码。

public class summary extends Activity {

    static EditText monthDate;
    TextView avgPrice;
    TextView exFuel;
    TextView avgFC;
    Button doneButton;
    Button exitButton;
       private String bundleID;
       private String bundleDate;
        private String bundlePrice;
        private String bundlePump;
        private String bundleCost;
        private String bundleOdometer;
        private String bundlePreOdometer;

        private String bundleFcon;
        static final int DATE_DIALOG_ID = 0;

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

        monthDate = (EditText)findViewById(R.id.month);
        avgPrice = (TextView)findViewById(R.id.showavfPriceTV);
        exFuel = (TextView)findViewById(R.id.showexFuelTV);
        avgFC = (TextView)findViewById(R.id.showavgFCTV);
        doneButton = (Button)findViewById(R.id.doneBTN);
        exitButton = (Button)findViewById(R.id.exitBTN);

        Bundle takeBundledData = getIntent().getExtras();  
        // First we need to get the bundle data that pass from the UndergraduateListActivity

        bundleID = takeBundledData.getString("clickedID");
        bundleDate = takeBundledData.getString("clickedDate");
        bundlePrice = takeBundledData.getString("clickedPrice");
        bundlePump = takeBundledData.getString("clickedPump");
        bundleCost = takeBundledData.getString("clickedCost");
        bundleOdometer = takeBundledData.getString("clickedOdometer");
        bundlePreOdometer = takeBundledData.getString("clickedpreOdometer");
        bundleFcon = takeBundledData.getString("clickedFCon");

        Log.d("SUMMARY","bundle : "+ takeBundledData);

        monthDate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
               // showDialog(DATE_DIALOG_ID);
                DialogFragment newFragment = new DatePickerFragment();
                newFragment.show(getFragmentManager(), "datePicker");
            }
        });
        doneButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (monthDate.equals(bundleDate))
                {
                    Log.d("TRYINGOUT","date : "+ bundleDate);
                     Intent intent=new Intent(getApplicationContext(),homeActivity.class);
                     startActivity(intent);
                }
                else 
                {
                     Intent intent=new Intent(getApplicationContext(),about.class);
                     startActivity(intent);
                }

            }
        });
}


    public static class DatePickerFragment extends DialogFragment
    implements DatePickerDialog.OnDateSetListener {

        public EditText editText;
        DatePicker dpResult;

    public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the current date as the default date in the picker

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

    //return new DatePickerDialog(getActivity(), (EditSessionActivity)getActivity(), year, month, day);

    // Create a new instance of DatePickerDialog and return it
    return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {

        monthDate.setText(String.valueOf(day) + "/"
                + String.valueOf(month + 1) + "/" + String.valueOf(year));
        // set selected date into datepicker also
}}}

【问题讨论】:

    标签: android date datepicker


    【解决方案1】:

    它不起作用,因为您尝试将 Objectstring 进行比较。 .equals() 不检查变量兼容性。您需要做的是从您的EditText 获取文本并将其与您的bundleDate 进行比较

    还要记住EditText.getText()方法返回Editable所以你需要把它转换成字符串。

    将你的条件改为:

    //if(monthDate.equals(bundleDate))
    if (monthDate.getText().toString().equals(bundleDate))
     {
        Log.d("TRYINGOUT","date : "+ bundleDate);
        Intent intent=new Intent(getApplicationContext(),homeActivity.class);
        startActivity(intent);
    }
    //rest of your code
    

    并确保您存储在捆绑包中的日期与您输入到 EditText 中的日期具有相同的格式,即 dd-MM-yyyy

    @EDIT 如果您只想比较月份,并且您很确定存储在 bundle 中的日期将始终具有相同的格式(dd/MM/yyyy 正如您所提到的),那么您可以从日期字符串中提取子字符串

    1) 使用 subSequence(start, stop) - 这将返回从字符串位置 3 和 4 上的字符创建的字符串

    //if(monthDate.equals(bundleDate))
    if (monthDate.getText().toString().equals(bundleDate.subSequence(3,5).toString()))
     {
        //your code
    }
    //rest of your code
    

    2) 使用String.split()

    准备数组以保留拆分结果String[] dateParts = bundleDate.split("/"); - 这将向您的数组返回 3 个字符串,第一个将保留日,第二个将保留月,第三个将保留年

    所以在代码中使用第二个(索引从零开始)

    String[] dateParts = bundleDate.split("/");
    //if(monthDate.equals(bundleDate))
    if (monthDate.getText().toString().equals(dateParts[1]))
     {
        //your code
    }
    //rest of your code
    

    这应该可以工作

    【讨论】:

    • 我可以检查一下吗,你说的是,“还要记住 EditText.getText() 方法返回 Editabe,所以你需要将它转换为字符串。”如何更改为字符串
    • 你能帮我解决这个问题吗?
    • 您好,我可以检查一下,如果我只是想取出 MONTH,我该怎么做?
    • @Chloe 只是为了确保我理解了一切:您唯一要比较的是 bundleDate 中的 MONTH 与 EditText 中输入的 MONTH?
    猜你喜欢
    • 2016-05-19
    • 1970-01-01
    • 1970-01-01
    • 2017-01-01
    • 2018-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-19
    相关资源
    最近更新 更多