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 the 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) into a string date, it can be completely displayed as date and time. After conversion, I want to filter records according to the date string (from sharedpref). My layout process is that after selecting a date from the calendar, The next activity will display filtered records with similar dates but different times. This is how I display records in the 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 the records based on the date clicked from the previous event

resolvent:

I understand your problem. You want to filter your records according to the specified date. For this purpose, first, format the DB date value in the same date format as in the sharing preference, assuming that you use the mm / DD / yyyy format. Then, you will use the date according to the same format, and then use the (date obj) date. Gettime() method (return milliseconds). Now these milliseconds (your sharing preference date) Comparing with milliseconds (your database date) basically checks the date object instead of the string. If the above matches, it means that the date is 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
分享
二维码
< <上一篇
下一篇>>