Java 在ListAdapter上显示之前格式化数据

Java 在ListAdapter上显示之前格式化数据,java,android,sqlite,Java,Android,Sqlite,我有一个ListAdapter,它从我的SQLite数据库中获取日期,并将它们全部显示在列表中。问题是,日期不是人类可读的格式,我有一个助手方法来执行转换,但是如何在代码中实现它呢 我的代码是这样的: // Get all of the notes from the database and create the item list Cursor c = mDbHelper.fetchAllItems(); startManagingCursor(c); Str

我有一个ListAdapter,它从我的SQLite数据库中获取日期,并将它们全部显示在列表中。问题是,日期不是人类可读的格式,我有一个助手方法来执行转换,但是如何在代码中实现它呢

我的代码是这样的:

    // Get all of the notes from the database and create the item list
    Cursor c = mDbHelper.fetchAllItems();
    startManagingCursor(c);

    String[] from = new String[] { TrackerDbAdapter.KEY_DATE };
    int[] to = new int[] { R.id.row_date };

    // Now create an array adapter and set it to display using our row
    SimpleCursorAdapter history =
        new SimpleCursorAdapter(this, R.layout.history_row, c, from, to);
    setListAdapter(history);

我会尝试创建一个自定义ListAdapter或自定义SimpleCursAdapter

如果不需要使用光标,请查看此链接。它解释了如何使用arraylist和自定义ListAdapter


您也可以对SimpleCorsorAdapter执行相同的操作。我当时找不到好的教程。当我使用
SimpleCursorAdapter.ViewBinder
将格式化数据附加到视图时,我将对此进行添加

SimpleCursorAdapter.ViewBinder dataBinder = new SimpleCursorAdapter.ViewBinder() {
@Override
    public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
        ((TextView)view).setText(cursor.getString(columnIndex));
        return true;
    }
};
simpleCursorAdapter.setViewBinder(dataBinder)

选项1:正如@nguyendat所说,为了提高性能,您可以在数据库中存储格式化日期以及未格式化版本,以提供最大的灵活性。但是,如果在同一个表中,由于冗余,这将违反第二范式,并且您必须在代码中小心更新行中的所有数据。 要实现这一点,请将转换代码放在DBAdapter的insert命令中

选项2:为你的约会对象创建一个类

public class FormattedDate {
private int oneDate;
public Punch (int unformattedDate) {
    oneDate = unformattedDate;
}  // ends constructor

@Override
public String toString() {
    //put your conversion code here
    return myFormattedDate;
}}
这还有一个额外的好处,就是可以在适当的位置放置任何其他代码进行比较或转换

在DBAdapter中,将查询更改为

public ArrayList<FormattedDate> fetchAllItems() {   
    ArrayList<FormattedDate> results = new ArrayList<FormattedDate>();
    Cursor c = db.rawQuery("SELECT MY_UNFORMATTED_DATE FROM yourTable", null);
    if (c.getCount() > 0) {
        c.moveToFirst();
        do {
            results.add( new FormattedDate(c.getInt(c.getColumnIndex(MY_UNFORMATTED_DATE))));
            } while (c.moveToNext());
        }
        c.close();
        return results;
    }
public ArrayList fetchAllItems(){
ArrayList结果=新建ArrayList();
游标c=db.rawQuery(“从表中选择我的未格式化日期”,null);
如果(c.getCount()>0){
c、 moveToFirst();
做{
结果.add(新格式化日期(c.getInt(c.getColumnIndex(MY_Unformated_DATE)));
}而(c.moveToNext());
}
c、 close();
返回结果;
}
这将返回FormattedDate对象的ArrayList

最后,这将填充listview

setContentView(R.layout.my_list_view);
ArrayList<FormattedDate> dateArray = mDBHelper.fetchAllItens();
ArrayAdapter<FormattedDate> dateAdapter = new ArrayAdapter<FormattedDate> (getApplicationContext(), R.layout.list_item, dateArray);
setListAdapter(dateAdapter);
setContentView(R.layout.my_list_视图);
ArrayList dateArray=mDBHelper.fetchAllItens();
ArrayAdapter dateAdapter=新的ArrayAdapter(getApplicationContext(),R.layout.list_项,dateArray);
setListAdapter(日期适配器);

这不是答案,但为了提高性能,我认为在将数据保存到数据库时最好转换时间格式,而不是每次从数据库加载数据时都转换时间格式。这不是一个好主意,因为我不想将日期保存为数据库中的“2011年7月27日”,但我希望类似于“20110727”(YYYYMMDD)的内容为了便于进行比较和使用其他格式,我应该把代码放在哪里?我试着把它放在SimpleCursorAdapter之后,但是在最后一行中我得到了一些错误,dataBinder不是setCursorToString的正确参数。我认为它仍然不起作用“我不确定在我的代码中放在哪里。我是否也必须更改原始代码中的某些内容?这仍然与最后一行的论点有关。我似乎无法让它工作。