转换mellisecond日期和使用光标ListView中填充 [英] convert mellisecond to date and populate in listView using cursor

查看:171
本文介绍了转换mellisecond日期和使用光标ListView中填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找回我的数据日期colomn保存mellisecond日期现在我想检索该mellisecond和隐蔽的日期和使用simplecursorAdapter我如何能做到下面这是我的code

在列表视图填充

 字符串SD = currdate.get(Calendar.YEAR)+ - + currmonth + - +
   currdate.get(Calendar.DAY_OF_MONTH);
    SimpleDateFormat的SF =新的SimpleDateFormat(YYYY-MM-DD);    日期sDcur = NULL;
    尝试{
        sDcur = sf.parse(SD);
    }赶上(ParseException的E){
        // TODO自动生成catch块
        e.printStackTrace();
    }    长ldcur = sDcur.getTime();    光标光标= lstDb.CurrentExp(ldcur);      的String [] =列新的String [] {
        ExMgDB.EX_RowID,
        ExMgDB.EX_Cattype,
        ExMgDB.EX_Date,
        ExMgDB.EX_Price,
        ExMgDB.EX_Type       };      INT dateColumn = R.id.txtexdate;
      日期D =新的日期(dateColumn);
      SimpleDateFormat的日期格式=新的SimpleDateFormat(YYYY-MM-DD);
      字符串日期= dateFormat.format(D);
      Log.i(Mytag2,与Date.toString());
      长ldate = Long.valueOf(日期); //在这一行我秀logcate异常
  像java.long.numberfomate例外      INT []为= INT新[] {
          R.id.txtexid,
          R.id.txtexcat,
          (INT)ldate,
          R.id.txtexprice,
          R.id.txtexitype
      };    DataAdapter的=新SimpleCursorAdapter(
                对此,R.layout.emtolst,
                光标,
                列,
                至,
                0);              ListView控件的ListView =(ListView控件)
findViewById(android.R.id.list);
              //指定适配器的ListView
              listView.setAdapter(DataAdapter的);
}

这是我的查询

 公共光标CurrentExp(长ldcur){
// TODO自动生成方法存根
的String [] =列新的String [] {EX_RowID,EX_Cattype,EX_Date,EX_Price,EX_Type};
光标C = ourdatabase.query(Food_TABLE,列EX_Date +='+ ldcur +',空,
    NULL,NULL,NULL);
如果(C!= NULL){       c.moveToFirst();      }
返回℃;
}


解决方案

您正在存储的日期在SQL数据库作为文本字符串,但随后要求SQL之间返回值的限制,并期待间preT那些当它没有被告知限制为日期,他们的日期,不知道如何跨preT文本格式的日期,如果它也知道他们是日期。

使用Date.getTime()将日期转换为长期价值和存储那些在SQL中为数字。同样地转换您的开始和结束时间长值,并要求SQL这些值之间返回记录。你需要转换的内容存储在SQL回一个用户友好的日期,任何时候使用日期(长值)构造并将它传递SQL给你多少。

i want to retrieve data my date colomn save date in mellisecond now i want to retrieve this mellisecond and covert to date and populate in listview using simplecursorAdapter how can i do this below is my code

   String sd = currdate.get(Calendar.YEAR) +"-"+ currmonth +"-"+ 
   currdate.get(Calendar.DAY_OF_MONTH);
    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");  

    Date sDcur = null;
    try {
        sDcur = sf.parse(sd);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 

    long ldcur = sDcur.getTime();

    Cursor cursor = lstDb.CurrentExp(ldcur);

      String[] columns = new String[] {
        ExMgDB.EX_RowID,
        ExMgDB.EX_Cattype,
        ExMgDB.EX_Date,
        ExMgDB.EX_Price,
        ExMgDB.EX_Type

       };

      int dateColumn = R.id.txtexdate; 
      Date d = new Date(dateColumn); 
      SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");  
      String date = dateFormat.format(d);
      Log.i("Mytag2", date.toString());
      long ldate = Long.valueOf(date); // on this line my logcate show exception   
  like java.long.numberfomate exception

      int[] to =  new int[]{
          R.id.txtexid,
          R.id.txtexcat,
          (int) ldate, 
          R.id.txtexprice,
          R.id.txtexitype
      };

    dataAdapter = new SimpleCursorAdapter(
                this, R.layout.emtolst, 
                cursor, 
                columns, 
                to,
                0);

              ListView listView = (ListView) 
findViewById(android.R.id.list);
              // Assign adapter to ListView
              listView.setAdapter(dataAdapter);
}

here is my query

public Cursor CurrentExp(long ldcur) {
// TODO Auto-generated method stub
String[] columns = new String[] {EX_RowID,EX_Cattype, EX_Date, EX_Price, EX_Type };
Cursor c= ourdatabase.query(Food_TABLE, columns, EX_Date + "='" + ldcur + "'", null,   
    null, null, null);


if (c != null) {

       c.moveToFirst();

      }
return c;


}

解决方案

You are storing the dates in the SQL database as text strings, but then asking SQL to return values between to limits and expecting it to interpret those limits as dates when it hasn't been told that they are dates and wouldn't know how to interpret the text format as a date if it did know they were dates.

Use Date.getTime() to convert the dates to long values and store those in SQL as numbers. Similarly convert your start and end times to long values, and ask SQL to return records between those values. Any time you need to convert what is stored in SQL back into a user-friendly date, use the Date(long value) constructor and pass it the number that SQL gives you.

这篇关于转换mellisecond日期和使用光标ListView中填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆