2012-02-15 5 views
0

저는 datetime 값이있는 필드가 있고 listview에 형식화 된 값을 표시하려고하는데 문제가 있습니다. 누군가 내 코드를보고이 코드를 도울 수 있습니까?SQLite에서 날짜를 변환하고 Listview를 채우십시오.

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

문제가 무엇 예를 들면? – confucius

답변

3

SimpleCursorAdapter는 수행하려는 작업에 너무 간단합니다. 'from'매개 변수는 실제로 열 이름의 배열이고 데이터는 커서에서 커서의 각 행에 해당하는 TextView로 직접 매핑됩니다.

올바른 방법은 TextView를 확장하여 커서에 저장된 데이터를 이해하고 내부적으로 서식을 처리하는 것입니다. 하지만 기술적으로 올바른 방법은 다음과 같습니다.

위의 논리를 bindView에 배치합니다.

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

} 

다음 :

Cursor c = mDB.getSms(); 
startManagingCursor(c); 
DateTimeCursorAdapter adapter = new DateTimeCursorAdapter(this, cursor); 
setListAdapter(adapter); 
+0

신난다 !! 고마워. – bond

+0

나는 조금 늦었지만 대답은 훌륭합니다! 위대한 작품과 감사합니다! 2 가지만 추가하면됩니다. newView()에서 배치 된 레이아웃은 목록보기의 항목 레이아웃이며 두 번째로 startManagingCursor 메소드는 사용되지 않으므로 조심스럽게 처리해야합니다. – ElaGorilaki

관련 문제