Android数据库日期类型setViewBinder
我想知道如何使用Android数据库日期类型setViewBinder,android,date,sqlite,Android,Date,Sqlite,我想知道如何使用setViewBinder。当我尝试这样做时,所有列都会更改相同的日期,即使它有数据。如何编辑与日期相关的每一列,而不是每一列? 我想我必须修改setViewBinder部分。 你能帮我吗 protected void onActivityResult(int requestCode, int resultCode,Intent intent) { super.onActivityResult(requestCode, resultCode, intent); fi
setViewBinder
。当我尝试这样做时,所有列都会更改相同的日期,即使它有数据。如何编辑与日期相关的每一列,而不是每一列?
我想我必须修改setViewBinder
部分。
你能帮我吗
protected void onActivityResult(int requestCode, int resultCode,Intent intent)
{
super.onActivityResult(requestCode, resultCode, intent);
fillData();
}
private void fillData()
{
cursor = dbHelper.fetchAllItems();
startManagingCursor(cursor);
String[] from = new String[] {FridgeDbAdapter.KEY_NAME,
FridgeDbAdapter.KEY_EXPIRED_DATE};
int [] to = new int[] {R.id.fridge_name_txt, R.id.fridge_expired_txt};
SimpleCursorAdapter data_row = new SimpleCursorAdapter(this, R.layout.fridge_row,
cursor, from, to);
//It makes crash the list which is making all same date..
data_row.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
//How can I bind the data with only specific date column?
public boolean setViewValue(View view, Cursor cursor, int column)
{
TextView tv = (TextView) view;
String date = cursor.getString(cursor.getColumnIndex("expired_date"));
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
Calendar cal = Calendar.getInstance();
date = df.format(cal.getTimeInMillis());
tv.setText(date);
return true;
}
});*/
setListAdapter(data_row);
}
*****我编辑了setViewBinder。。但是,它显示的是每列的日期,而不是具体的列。日期显示为1970年1月1日,与2011年11月12日不同。。
我该怎么办**
data_row.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int column)
{
if(column>=0)
{
TextView tv = (TextView) view;
long date=cursor.getLong(cursor.getColumnIndex("expired_date"));
Calendar cal = Calendar.getInstance();
cal.clear();
cal.setTimeInMillis(date);
String date_format = String.format("%1$td-%1$tm-%1$tY", cal);
tv.setText(date_format);
return true;
}
return false;
}
}))
这是由该代码引起的
String date=cursor.getString(cursor.getColumnIndex(“过期日期”)代码>
你应该这样做
String date=cursor.getString(列)代码>
接下来,您必须检查column==cursor.getColumnIndex(“过期日期”)
只有在正确的情况下才进行格式化。在另一种情况下,您应该使用日期字符串中的数据设置tv。代码中的错误是您从未解析从数据库中获取的日期
试一试
如果数据库中的日期未按您希望的格式格式化,则:
public boolean setViewValue(View view, Cursor cursor, int column)
{
TextView tv = (TextView) view;
String dbDate = cursor.getString(cursor.getColumnIndex("expired_date"));
SimpleDateFormat dbDateFormat = new SimpleDateFormat("****db date format***");
SimpleDateFormat outputFormat = new SimpleDateFormat("****output format***");
Date date = dbDateFormat.parse(dbDate);
tv.setText(outputFormat.format(date));
return true;
}
关于日期类型,您可以在此处找到:
当我使用您的编码时,这里发生了错误,Date-Date=dbDateFormat.parse(dbDate);。他们说使用try-catch,日期必须初始化。然后我将Date==null,并使用try-catch。但是,当我调试时,tv.setText(outputFormat.format(date));这又是一个错误。。我该怎么办?
public boolean setViewValue(View view, Cursor cursor, int column)
{
TextView tv = (TextView) view;
String dbDate = cursor.getString(cursor.getColumnIndex("expired_date"));
SimpleDateFormat dbDateFormat = new SimpleDateFormat("****db date format***");
SimpleDateFormat outputFormat = new SimpleDateFormat("****output format***");
Date date = dbDateFormat.parse(dbDate);
tv.setText(outputFormat.format(date));
return true;
}