在ListView中显示日期

时间:2019-01-05 10:31:19

标签: android listview

对于我的大学,我必须开发一个待办事项应用程序。一个要求是,我选择的日期必须显示在ListView中。我找到了一个YoutTube教程,该教程仅显示当前年份。

在两张图片中,您可以看到ListView和具有一个ToDo的ListView的布局。右侧的TextView字段应显示截止日期。它的ID为tvDueDate

enter image description here enter image description here

我该如何更改代码,使其显示我想要的方式?

这是我的代码:

((TextView) view.findViewById(R.id.tvName)).setText(currentToDo.getName());
       TextView dueDate = (TextView) view.findViewById(R.id.tvDueDate);
       ImageView fav = (ImageView) view.findViewById(R.id.favIcon);

       if(currentToDo.getDueDate() == null){
           dueDate.setVisibility(View.GONE);
       } else {
           dueDate.setVisibility(View.VISIBLE);
dueDate.setText(String.valueOf(currentToDo.getDueDate().get(Calendar.YEAR)));
       }

要添加新的待办事项,请使用以下代码:

todo = new ToDo();
dueDate = (TextView) findViewById(R.id.dueDate);
btnAdd = (Button) findViewById(R.id.btnAdd);

btnAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(todo.getName() == null){
                Toast.makeText(ToDoCreateNew.this, "Please insert some value.", Toast.LENGTH_LONG).show();
                return;
            }
            ToDoDatabaseHelper.getInstance(ToDoCreateNew.this).createTodo(todo);
            finish();

我的ToDoDatabaseHelper:

public ToDo createTodo(final ToDo todo) {
    SQLiteDatabase database = this.getWritableDatabase();

    ContentValues contentValues = new ContentValues();
    contentValues.put(NAME_COLUMN, todo.getName());
    contentValues.put(DUEDATE_COLUMN, todo.getDueDate() == null ? null : todo.getDueDate().getTimeInMillis() / 1000);
    contentValues.put(FAVORITE_COLUMN, todo.isFavorite() ? 1 : 0);
    contentValues.put(DESCRIPTION_COLUMN, todo.getDescription());
    contentValues.put(DUETIME_COLUMN, String.valueOf(todo.getDueTime() == null ? null : todo.getDueTime().getTime()));

    long newID = database.insert(TABLE_NAME, null, contentValues);
    database.close();
    return readToDo(newID);
}

编辑:

下面的两张图片显示了我要添加一个具有到期日的新ToDo的意图。

enter image description here enter image description here

用户单击带有“单击到期日期”的TextView,然后打开DatePicker。

以下是选择日期的代码:

dueDate.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
           Calendar calendar = Calendar.getInstance();
           int year = calendar.get(Calendar.YEAR);
           int month = calendar.get(Calendar.MONTH);
           int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);

           DatePickerDialog dialog = new DatePickerDialog(ToDoCreateNew.this,
                   android.R.style.Theme_Material_Dialog_MinWidth,
                   datePickerDialog,
                   year, month, dayOfMonth);
           ((DatePickerDialog) dialog).show();
       }
   });

   datePickerDialog = new DatePickerDialog.OnDateSetListener() {
       @Override
       public void onDateSet(DatePicker datePicker, int year, int month, int dayOfMonth) {
           dueDate.setText(String.format("%02d. %02d. %d", dayOfMonth, (month + 1), year));
       }
   };

EDIT2:

public class ToDoDatabaseHelper extends SQLiteOpenHelper {
public static ToDoDatabaseHelper INSTANCE = null;

public static final String DB_NAME = "TODOS";
public static final int VERSION = 9;
public static final String TABLE_NAME = "todos";

public static final String ID_COLUMN = "ID";
public static final String NAME_COLUMN = "name";
public static final String DUEDATE_COLUMN = "duedate";
public static final String FAVORITE_COLUMN = "favorite";
public static final String DESCRIPTION_COLUMN = "description";
public static final String DUETIME_COLUMN = "duetime";

public ToDoDatabaseHelper(Context context) {
    super(context, DB_NAME, null, VERSION);
}

public static ToDoDatabaseHelper getInstance(final Context context) {
    if (INSTANCE == null) {
        INSTANCE = new ToDoDatabaseHelper(context);
    }
    return INSTANCE;
}

@Override
public void onCreate(SQLiteDatabase db) {
    String createQuery = "CREATE TABLE " + TABLE_NAME
            + "(" + ID_COLUMN + " INTEGER PRIMARY KEY, " + NAME_COLUMN + " TEXT NOT NULL,"
            + DUEDATE_COLUMN + " INTEGER DEFAULT NULL, " + FAVORITE_COLUMN + " INTEGER DEFAULT 0,"
            + DESCRIPTION_COLUMN + " TEXT DEFAULT NULL, " + DUETIME_COLUMN + " INTEGER DEFAULT NULL)";
    db.execSQL(createQuery);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    String dropTable = "DROP TABLE IF EXISTS " + TABLE_NAME;
    db.execSQL(dropTable);

    onCreate(db);
}

public ToDo createTodo(final ToDo todo) {
    SQLiteDatabase database = this.getWritableDatabase();

    ContentValues contentValues = new ContentValues();
    contentValues.put(NAME_COLUMN, todo.getName());
    contentValues.put(DUEDATE_COLUMN, todo.getDueDate());
    //contentValues.put(DUEDATE_COLUMN, todo.getDueDate() == null ? null : todo.getDueDate().getTimeInMillis() / 1000);
    contentValues.put(FAVORITE_COLUMN, todo.isFavorite() ? 1 : 0);
    contentValues.put(DESCRIPTION_COLUMN, todo.getDescription());
    contentValues.put(DUETIME_COLUMN, String.valueOf(todo.getDueTime() == null ? null : todo.getDueTime().getTime()));


    long newID = database.insert(TABLE_NAME, null, contentValues);
    database.close();
    return readToDo(newID);
}

Edit3:

enter image description here

EDIT4:

我的ToDo.java类

package model;

import java.util.Calendar;

import java.io.Serializable;

public class ToDo implements Serializable {
    private long id;
    private String name;
    private String dueDate;
    private  Calendar dueTime;
    private boolean favorite;
    private String contact;
    private String description;

    public ToDo() {
        this(null, null, null, false, null,null);
    }

    public ToDo(String name) {
        // Verschachtelt aufrufen, damit nichts vergessen wird
        this(name, null, null, false, null, null);
    }

    public ToDo(String name, String dueDate, Calendar dueTime, final boolean     favorite, String contact, String description) {
        this.name = name;
        this.dueDate = dueDate;
        this.dueTime = dueTime;
        this.favorite = favorite;
        this.contact = contact;
        this.description = description;
    }



    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public Calendar getDueTime() {
        return dueTime;
    }

    public void setDueTime(Calendar dueTime) {
        this.dueTime = dueTime;
    }

    public boolean isFavorite() {
        return favorite;
    }

    public void setFavorite(boolean favorite) {
        this.favorite = favorite;
    }

    public String getContact() {
        return contact;
    }

    public void setContact(String contact) {
        this.contact = contact;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getDueDate() {
        return dueDate;
    }

    public void setDueDate(String dueDate) {
        this.dueDate = dueDate;
    }

    /*public Calendar getDueDate() {
        return dueDate;
    }

    public void setDueDate(Calendar dueDate) {
        this.dueDate = dueDate;
    }*/
}

1 个答案:

答案 0 :(得分:0)

以下解决方案可能会解决您的问题

((TextView) view.findViewById(R.id.tvName)).setText(currentToDo.getName());
   TextView dueDate = (TextView) view.findViewById(R.id.tvDueDate);
   ImageView fav = (ImageView) view.findViewById(R.id.favIcon);

   if(currentToDo.getDueDate() == null){
       dueDate.setVisibility(View.GONE);
   } else {
       dueDate.setVisibility(View.VISIBLE);
       SimpleDateFormat dateView = new SimpleDateFormat("MMM MM dd, yyyy h:mm a");
       String dateString = dateView.format(currentToDo.getDueDate());
       dueDate.setText(dateString);
   }