Java – filter ArrayList after converting long (from constructor) to string

I store this long value in the database as datetime. I have a question about how to filter these records according to the required date. The date and time are stored as long values I query records through this code

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

My constructor

public DatabaseSource(int id,String subject,String description,Long dueDateTime) {

        this.id = id;
        this.subject = subject;
        this.description = description;
        this.dueDateTime = dueDateTime;
    }

Now, when I try to convert the value in getduedatetime (long) to a string date, it can be completely displayed as date and time After the conversion, I want to filter the records according to the date string (from sharedpref) My layout process is that after selecting a date from the calendar, the next event will display filtered records with similar dates but different times This is how I display records in list view

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

As you can see, the date and time have been successfully converted, but all records are displayed. I just want to display records with the same date after comparing them with the save date in sharedpref, regardless of the time Suppose you click a date in the calendar, the date has been saved to sharedpref, and the next event has filtered records based on the date clicked from the previous event

Solution

I understand your question. You want to filter your records according to the specified date First, format the DB date value in the same date format as in sharing preferences, assuming you are using mm / DD / yyyy format Then, you will use the date according to the same format, and then use (date obj) date The gettime () method (returns milliseconds) now compares these milliseconds (your preferred shared date) with milliseconds (your database date). Basically, the check will be on the date object rather than the string. If the above matches, it means that the dates are the same. Now you can filter your records according to it (convert the string to date obj. Just use its constructor and pass the string to it)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>