
我将这个Long值存储在数据库中作为DATETIME,我有一个关于如何根据所需日期过滤这些记录的问题,日期和时间存储为Long值.我通过这段代码查询记录
public List<DatabaseSource> getListSched() { List<DatabaseSource> taskList = new ArrayList<DatabaseSource>(); // Select All query String selectquery = "SELECT * FROM schedTBL" ; sqliteDatabase db = dbHelper.getReadableDatabase(); Cursor cursor = db.rawquery(selectquery, null); // looPing through all rows and adding to List if (cursor.movetoFirst()) { do { DatabaseSource tasks = new DatabaseSource(); tasks.setID(cursor.getInt(0)); tasks.setSubject(cursor.getString(1)); tasks.setDescription(cursor.getString(2)); tasks.setDueDateTime(cursor.getLong(3)); // Adding contact to List taskList.add(tasks); } while (cursor.movetoNext()); } db.close(); // return contact List return taskList;}我的构造函数
public DatabaseSource(int ID, String subject, String description, Long dueDateTime) { this.ID = ID; this.subject = subject; this.description = description; this.dueDateTime = dueDateTime; }现在,当我尝试将getDueDateTime(Long)中的值转换为字符串的日期时,它可以完全显示为日期和时间.转换后,我想根据日期字符串(来自sharedpref)过滤记录.我的布局流程是,从日历中选择日期后,下一个活动会显示具有相似日期但时间不同的已过滤记录.
这是我在列表视图中显示记录的方式
@OverrIDepublic VIEw getVIEw(int position, VIEw convertVIEw, VIEwGroup parent) { sharedPreference = new SharedPreferenceUID(); text = sharedPreference.getValue2(mContext); String dateString= DateFormat.format("yyyy-MM-dd hh:mm a", new Date(mSchedLists.get(position).getDueDateTime())).toString(); VIEw v = VIEw.inflate(mContext, R.layout.item_ListvIEw, null); TextVIEw subj = (TextVIEw)v.findVIEwByID(R.ID.txtSubject); TextVIEw duedate = (TextVIEw) v.findVIEwByID(R.ID.txtDueDateTime); subj.setText(mSchedLists.get(position).getSubject()); duedate.setText(dateString); v.setTag( mSchedLists.get(position).getID()); return v;}如您所见,日期和时间已成功转换但显示所有记录,我只想在将其与sharedpref中的保存日期进行比较后显示具有相同日期的记录,而不管时间如何.假设您单击了日历中的日期,并且该日期已保存到sharedpref,并且下一个活动已根据从上一个活动单击的日期过滤了记录.
解决方法:
我理解你的问题,你想根据指定的日期过滤你的记录,为此.
首先,使用与共享首选项中相同的日期格式格式化db日期值,假设您使用的是MM / dd / yyyy格式.
然后,您将根据相同的格式使用日期,然后使用(Date Obj)date.getTime()方法(返回毫秒)现在将这些毫秒(您的共享首选日期)与毫秒(您的数据库日期)进行比较基本上检查将在日期对象而不是字符串,如果上述匹配,这意味着日期相同,现在您可以根据它来过滤您的记录.
(将字符串转换为Date obj.只需使用其构造函数并将字符串传递给它).
以上是内存溢出为你收集整理的java – 将Long(从构造函数)转换为String后过滤ArrayList全部内容,希望文章能够帮你解决java – 将Long(从构造函数)转换为String后过滤ArrayList所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)