我想知道如何使用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 ..但是,它显示的是每列的日期,而不是特定的列。日期显示01-01-1970,而不是像12/11/2011 .. 我该怎么办?**
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;
}
});
答案 0 :(得分:0)
它是由此代码引起的
String date = cursor.getString(cursor.getColumnIndex("expired_date"));
你应该做这样的事
String date = cursor.getString(column);
接下来你必须检查列== cursor.getColumnIndex(“expired_date”) 并且只有在其他情况下你才能使用日期字符串
中的数据设置电视时才进行格式化答案 1 :(得分:0)
您的代码中的错误是您从未解析从数据库获取的日期。
尝试
public boolean setViewValue(View view, Cursor cursor, int column)
{
TextView tv = (TextView) view;
String date = cursor.getString(cursor.getColumnIndex("expired_date"));
tv.setText(date);
return true;
}
如果来自数据库的日期没有格式化您希望如何格式化,那么:
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;
}
答案 2 :(得分:0)
关于您可以在那里找到的日期类型: Android database date type setViewBinder