java – 将Long(从构造函数)转换为String后过滤ArrayList
发布时间:2020-12-15 02:16:52 所属栏目:Java 来源:网络整理
导读:我将这个Long值存储在数据库中作为DATETIME,我有一个关于如何根据所需日期过滤这些记录的问题,日期和时间存储为Long值.我通过这段代码查询记录 public ListDatabaseSource getListSched() { ListDatabaseSource taskList = new ArrayListDatabaseSource(); /
我将这个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)过滤记录.我的布局流程是,从日历中选择日期后,下一个活动会显示具有相似日期但时间不同的已过滤记录. @Override public 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.只需使用其构造函数并将字符串传递给它).
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |