Pigeon android noobie在这里。 我正在尝试格式化DBcolumn上存储时间的数据库值。 我正在使用的代码根本没有格式化数据,只显示格式化的数据 如果我调试代码,正确的值将被传递到cosde的格式化piexe但它没有被传递到UI textview格式 我必须亲近:))
public void PopulateListViewFromDatabase() {
Cursor cursor = myDb.getAllRows();
startManagingCursor(cursor);
// Setup mapping from the cursor to view fields
String[] fromFieldNames = new String[] {
DBAdapter.KEY_START,
DBAdapter.KEY_FINISH,
DBAdapter.KEY_ACTIONHRCOUNT,
DBAdapter.KEY_BANGCOUNT,
DBAdapter.KEY_RANDOMOPT
};
int[] to=new int [] {
R.id.txtStartTime,
R.id.txtFinishTime,
R.id.txtActionCount,
R.id.txtBangCount,
R.id.checkRandom
};
//Create adaptor to map columns of the DB onto elements in the UI.
SimpleCursorAdapter myCursorAdapter =
new SimpleCursorAdapter(
this, // Context
R.layout.frm_item, // Row layout template
cursor, //cursor record infomation
fromFieldNames, //DB column names where the infomation is coming from
to // and where the infomation is being sent to
);
myCursorAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
int nCheckedIndex = cursor.getColumnIndexOrThrow(DBAdapter.KEY_START);
if (columnIndex == nCheckedIndex) {
long DBStart = cursor.getLong(DBAdapter.COL_START); //Value is being passed correctly into here!!!!
TextView txtStart= (TextView) view;
txtStart.setText(DateFormat.format("h:mm a",DBStart )); // Not being sent as formatted data correctly here?????????????
}
return false;
}
});
ListView myList = (ListView) findViewById(R.id.listviewactions);
myList.setAdapter(myCursorAdapter);
}
期待您的回复
Snowie
答案 0 :(得分:1)
排序。这有很多帮助 Changing values from Cursor using SimpleCursorAdapter
最终代码
public void PopulateListViewFromDatabase() {
Cursor cursor = myDb.getAllRows();
startManagingCursor(cursor);
// Setup mapping from the cursor to view fields
String[] fromFieldNames = new String[] {
DBAdapter.KEY_START,
DBAdapter.KEY_FINISH,
DBAdapter.KEY_ACTIONHRCOUNT,
DBAdapter.KEY_BANGCOUNT,
DBAdapter.KEY_RANDOMOPT
};
int[] to=new int [] {
R.id.txtStartTime,
R.id.txtFinishTime,
R.id.txtActionCount,
R.id.txtBangCount,
R.id.checkRandom
};
//Create adaptor to map columns of the DB onto elements in the UI.
SimpleCursorAdapter myCursorAdapter =
new SimpleCursorAdapter(
this, // Context
R.layout.frm_item, // Row layout template
cursor, //cursor record infomation
fromFieldNames, //DB column names where the infomation is coming from
to // and where the infomation is being sent to
);
myCursorAdapter.setViewBinder(new ViewBinder() {
public boolean setViewValue(View aView, Cursor aCursor, int aColumnIndex) {
if (aColumnIndex == 1) {
long createDate = aCursor.getLong(aColumnIndex);
TextView textView = (TextView) aView;
textView.setText(DateFormat.format("h:mm a", createDate));
return true;
}
if (aColumnIndex == 2) {
long createDate = aCursor.getLong(aColumnIndex);
TextView textView = (TextView) aView;
textView.setText(DateFormat.format("h:mm a", createDate));
return true;
}
return false;
}
});
ListView myList = (ListView) findViewById(R.id.listviewactions);
myList.setAdapter(myCursorAdapter);
}
在上面的链接中还有一个sqlLite语法注释,当数据适配器查询数据库时它会做同样的事情