Changing values from Cursor using SimpleCursorAdapter

独自空忆成欢 提交于 2019-11-27 06:59:12
Hendrik

The simplest way to format a cursor value is to use SimpleCursorAdapter.setViewBinder(..):

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.list, cursor,
            new String[] { Definition.Item.TITLE, Definition.Item.CREATE_DATE }, new int[] { R.id.title, R.id.createDate});

adapter.setViewBinder(new ViewBinder() {

    public boolean setViewValue(View aView, Cursor aCursor, int aColumnIndex) {

        if (aColumnIndex == 2) {
                String createDate = aCursor.getString(aColumnIndex);
                TextView textView = (TextView) aView;
                textView.setText("Create date: " + MyFormatterHelper.formatDate(getApplicationContext(), createDate));
                return true;
         }

         return false;
    }
});
Manas

i also had the same problem after long struggle finally i found answer :) ( see below )

use setViewText (TextView v, String text)

for example

SimpleCursorAdapter shows = new SimpleCursorAdapter(this, R.layout.somelayout, accountCursor, from, to)
{
 @Override
 public void setViewText(TextView v, String text) {
 super.setViewText(v, convText(v, text));
}    
};

private String convText(TextView v, String text)
{

 switch (v.getId())
 {
 case R.id.date:
             String formatedText = text;
             //do format
            return formatedText;
        }
return text;
}

You can use setViewBinder(), or subclass SimpleCursorAdapter and override bindView().

You can use SQLite syntax on that column to format the date.

Something like this will do it

SELECT strftime('%d-%m-%Y %H:%M',1092941466,'unixepoch');

SELECT strftime('%d-%m-%Y %H:%M',timecol,'unixepoch');

Going thru this old post, noticed I have done something similar that might help:

public class FormatCursorAdapter extends SimpleCursorAdapter {

protected int[] mFormats;

public static final int FORMAT_TEXT=0;
public static final int FORMAT_CURRENCY=1;
public static final int FORMAT_DATE=2;

public FormatCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int[] formats, int flags) {
    super(context, layout, c, from, to, flags);
    mFormats = formats;
    ViewBinder viewBinder = new ViewBinder() {
        @Override
        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
            int formatType = mFormats[columnIndex-1];
            switch (formatType) {
                case FORMAT_CURRENCY:
                    NumberFormat nf = NumberFormat.getCurrencyInstance();
                    nf.setMaximumFractionDigits(2);
                    ((TextView)view).setText(nf.format(cursor.getDouble(columnIndex)));
                    return true;
                case FORMAT_DATE:
                    DateFormat df = SimpleDateFormat.getDateTimeInstance();
                    ((TextView)view).setText(df.format(new Date(cursor.getLong(columnIndex))));
                    return true;
            }
            return false;
        }
    };
    setViewBinder(viewBinder);
}

}

Usage:

    // For the cursor adapter, specify which columns go into which views with which format
    String[] fromColumns = {
            Table.COLUMN_TITLE,
            Table.COLUMN_AMOUNT,
            Table.COLUMN_DATE};
    int[] toViews = {
            R.id.tvTitle,
            R.id.tvAmount,
            R.id.tvDate};
    int[] formatViews = {
            FormatCursorAdapter.FORMAT_TEXT,
            FormatCursorAdapter.FORMAT_CURRENCY,
            FormatCursorAdapter.FORMAT_DATE};

    mAdapter=new FormatCursorAdapter(getContext(),R.layout.item_operation,cursor,
            fromOpsColumns,toOpsViews,formatViews,0);
    mListView.setAdapter(mOpsAdapter);

Hope this helps anyone out there !

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!