开发者

Format data before showing on ListAdapter

I have a ListAdapter which takes the dates from my SQLite database and displays them all on the list. The thing is, the date is not in human readable format, and I have a helper method to perform the conversion, but how do I implement it on my code?

This is how my code looks like:

    // 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);


I would try creating a custom ListAdapter or custom SimpleCursorAdapter

If you do not need to use a cursor, have a look at this link. http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/ It explains how to use an arraylist and custom listAdpater.

You can also do the same with the SimpleCursorAdapter. I was unable to find a good tutorial at this time. I will add to this when I do


Use SimpleCursorAdapter.ViewBinder to attach the formatted data to Views.

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)


Option 1: Like @nguyendat said, for performance, you could store the formatted date in the database, as well as the un-formatted version, to give you the most flexibility. If in the same table though, this would violate 2nd normal form because of the redundancy, and you would have to be careful in your code to update all of the data in the row. To implement this, put your conversion code in your DBAdapter, in the insert command.

Option 2: Create a class for your date

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

This has the added benefit of a proper place to put any other code for comparisons or conversions.

Inside your DBAdapter, change your query to this

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

This returns an ArrayList of FormattedDate objects

Finally, this would populate a 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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜