2011-11-23 51 views
1

我在想使用setViewBinder。當我嘗試這樣做時,即使它有數據,所有列也會更改同一日期。我如何編輯每個列,這是相關的日期,而不是每一列? 我認爲我必須修改setViewBinder部分。 你能幫我嗎?Android數據庫日期類型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; 
    } 

})?;

Original List

Result after setViewBinder

回答

0

就會通過這個代碼

String date = cursor.getString(cursor.getColumnIndex("expired_date"));

你應該做的smthing這樣

String date = cursor.getString(column);

引起

接下來,您必須檢查是否列== cursor.getColumnIndex(「EXPIRED_DATE」) 並做格式化只有當它在其他情況下,真的,你要高度重視建立電視與日期字符串數據

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; 
    } 

如果從DB日期未格式化你想怎麼它被格式化,則:

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; 
    } 
+0

當我使用你的編碼時,在這裏發生錯誤,Date date = dbDateFormat.parse(dbDate);.他們說使用try catch,日期必須初始化。然後我把日期日期== null,我用try catch。但是當我調試時,tv.setText(outputFormat.format(date));這是錯誤再次..我該怎麼辦? – wholee1