Skip to content Skip to sidebar Skip to footer

How To Add A Time Stamp To A List View?

I want to add a timeStamp to the listView so it shows you when the item was created in this code. Is there any way, I can go about doing this. if some knows how to do this please l

Solution 1:

I think this modification helps you, I just write the main parts, you need to apply changes yourself. good luck!

DatabaseHelper.java

@OverridepublicvoidonCreate(SQLiteDatabase db) {

    String createQuery = "CREATE TABLE " + ITEMS_TABLE + " (" +
            "_id INTEGER PRIMARY KEY AUTOINCREMENT," +
            "description TEXT NOT NULL, " +
            "completed INTEGER NOT NULL DEFAULT 0," + 
            "timeStamp INTEGER NOT NULL)";
    db.execSQL(createQuery);
}

ToDoItem.java

publicclassToDoItem{
    privateLong timeStamp;
    private String description;
    private boolean isComplete;
    private long id;

    // I just added two getter and setter methodspublic void setTimeStamp(Long timeStamp){
        this.timeStamp = timeStamp;
    }

    publicLong getTimeStamp(){
        return timeStamp;
    }

    public ToDoItem(String description,boolean isComplete,long id, Long timeStamp) {
        this.description = description;
        this.isComplete = isComplete;
        this.id = id;
        this.timeStamp = timeStamp;
    }

    public ToDoItem(String description,boolean isComplete,long id) {
        this.description = description;
        this.isComplete = isComplete;
        this.id = id;
        Calendar now = Calendar.getInstance();
        this.timeStamp = now.getTimeInMillis();
    }

}

ToDoListManager.java

publicList<ToDoItem> getList() {
    ...
    ToDoItem item = newToDoItem(
                    cursor.getString(cursor.getColumnIndex("description")),
                    cursor.getInt(cursor.getColumnIndex("completed")) != 0,
                    cursor.getLong(cursor.getColumnIndex("_id")),
                    cursor.getLong(cursor.getColumnIndex("timeStamp"))
            );
    ...
}

publicvoidaddItem(ToDoItem item) {
    ...
    // add this line
    newItem.put("timeStamp", item.getTimeStamp());
    ...
}

publicvoidupdateItem(ToDoItem item) {
    ...
    // add this line
    editItem.put("timeStamp", item.getTimeStamp());
    ...
}

ToDoItemAdapter class

I assume that you add a TextView timeStamp to your to_do_item_layout and your ItemViewHolder class

ItemViewHolder class

publicstaticclassItemViewHolder{
    public TextView itemDescription;
    public CheckBox itemState;
    public TextView itemTimeStamp;
   }
}

getView add this lines

holder.itemTimeStamp = (TextView) convertView.findViewById(R.id.timeStamp);
holder.itemTimeStamp.setText(getPassedTimeForCreation(item.getTimeStamp()));

getPassedTimeForCreation(Long timeStamp)

String getPassedTimeFromCreation(Long timeStamp){
        Calendar now = Calendar.getInstance();
        Long differece = now.getTimeInMillis() - timeStamp;

        if (differece < (5 * 60 * 1000)) { // (5 minute in milliseconds)return"5 min ago";
        }
        elseif(differece < (2 * 24 * 60 * 60 * 1000)){ // (2 days in millisecond)return"2 days ago";
        }else{
            return"long time ago";
        }
    }

add getPassedTimeForCreation function in MainActivity class, not inside of MainActivity's function body!!!

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    `...`
}

@OverrideprotectedvoidonPause() {
    `...`
}

privatevoidonAddButtonClick() {
    `...`
}

privateclassToDoItemAdapterextendsArrayAdapter<ToDoItem> {
    `...`
}

publicstaticclassItemViewHolder{
    `...`
}

/**
 * this is the place that you must put the function!
 */StringgetPassedTimeFromCreation(Long timeStamp){
    `...`
}

Post a Comment for "How To Add A Time Stamp To A List View?"