问题描述
我有一个问题,在那里我有一个日期时间值的字段,要显示在一个列表视图格式化值。
是否有人可以看看我的code和这一个帮助?
光标= db.getAllSms();
startManagingCursor(光标);
INT的mtime = cursor.getColumnIndex(DBAdapter.KEY_DATETIME); 的String [] =由新的String [cursor.getCount()];
INT []为= INT新[] {} R.id.label;
INT计数器= 0;
为(cursor.moveToFirst();!cursor.isAfterLast(); cursor.moveToNext()){
SimpleDateFormat的SDF =新的SimpleDateFormat(DD MMMM YYYY HH:MM); 日期resultdate =新的日期(cursor.getLong(Mtime时光网));
字符串mDateTime = sdf.format(resultdate);
从[计数器] = mDateTime;
反++;
} SimpleCursorAdapter用户=新SimpleCursorAdapter(这一点,R.layout.sms_row,光标,从,到);
setListAdapter(用户);
SimpleCursorAdapter是你想要做的太简单了。 发件人参数实际上是列名的数组,数据将直接从光标被映射到相应的TextView对游标中的每一行。
有人告诉我,一个正确的方法是延长的TextView理解数据,因为它是存储在您的光标,在内部处理的格式。但是,另一种,也许更少技术上正确的方法是如下:
扩展的CursorAdapter,并把上面的逻辑将bindView。例如:
类DateTimeCursorAdapter扩展的CursorAdapter {
LayoutInflater mInflater; 私人诠释的mtime;
SimpleDateFormat的SDF; DateTimeCursorAdapter(上下文的背景下,光标光标)
{
超(背景下,光标);
mInflater =(LayoutInflater)上下文
.getSystemService(Context.LAYOUT_INFLATER_SERVICE); Mtime时光网= cursor.getColumnIndex(DBAdapter.KEY_DATETIME);
SDF =新的SimpleDateFormat(DD MMMM YYYY HH:MM); } 公共查看NewView的(上下文的背景下,光标光标的ViewGroup父)
{
返回mInflater.inflate(R.layout.dispatchesrow,父母,假);
} 公共无效bindView(查看排,上下文的背景下,光标光标)
{
TextView的tvLabel =(TextView中)一行
.findViewById(R.id.label);
日期resultdate =新的日期(cursor.getLong(Mtime时光网));
字符串mDateTime = sdf.format(resultdate);
tvLabel.setText(mDateTime);
}}
然后:
光标C = mDB.getSms();
startManagingCursor(C);
DateTimeCursorAdapter适配器=新DateTimeCursorAdapter(这一点,光标);
setListAdapter(适配器);
i'm having a problem where I have a field with a datetime value and want to display a formatted value on a listview.Can someone please take a look at my code and help with this one?
cursor = db.getAllSms();
startManagingCursor(cursor);
int mTime= cursor.getColumnIndex(DBAdapter.KEY_DATETIME);
String[] from = new String[cursor.getCount()];
int[] to = new int[] {R.id.label};
int counter = 0;
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){
SimpleDateFormat sdf = new SimpleDateFormat("dd MMMM yyyy HH:mm");
Date resultdate = new Date(cursor.getLong(mTime));
String mDateTime = sdf.format(resultdate);
from[counter] = mDateTime;
counter++;
}
SimpleCursorAdapter users = new SimpleCursorAdapter(this, R.layout.sms_row, cursor, from, to);
setListAdapter(users);
SimpleCursorAdapter is too simple for what you're trying to do. The 'from' parameter is actually an array of column names, and the data will be mapped directly from the cursor to a corresponding TextView for each row in the cursor.
I've been told that a correct way would be to extend TextView to understand the data as it is stored in your cursor and internally handle the formatting. But, another, maybe less technically correct way is as follows:
Extend CursorAdapter and put the logic above into bindView. For example:
class DateTimeCursorAdapter extends CursorAdapter {
LayoutInflater mInflater;
private int mTime;
SimpleDateFormat sdf;
DateTimeCursorAdapter(Context context, Cursor cursor)
{
super(context, cursor);
mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mTime = cursor.getColumnIndex(DBAdapter.KEY_DATETIME);
sdf = new SimpleDateFormat("dd MMMM yyyy HH:mm");
}
public View newView(Context context, Cursor cursor, ViewGroup parent)
{
return mInflater.inflate(R.layout.dispatchesrow, parent, false);
}
public void bindView(View row, Context context, Cursor cursor)
{
TextView tvLabel = (TextView) row
.findViewById(R.id.label);
Date resultdate = new Date(cursor.getLong(mTime));
String mDateTime = sdf.format(resultdate);
tvLabel.setText(mDateTime);
}
}
Then:
Cursor c = mDB.getSms();
startManagingCursor(c);
DateTimeCursorAdapter adapter = new DateTimeCursorAdapter(this, cursor);
setListAdapter(adapter);
这篇关于从SQLite的转换日期和填充一个ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!