2011-03-17 7 views
0

다음 코드 내 DB에서 내 데이터를 얻을 :데이터베이스에서 가져온 데이터를 어떻게 변경할 수 있습니까?

private void fillData() { 
     cursor = mDbAdapter.fetchAllSubjects(); 
     startManagingCursor(cursor); 

     String[] from = new String[] { DatabaseAdapter.KEY_TITLE, DatabaseAdapter.KEY_LECTURER, DatabaseAdapter.KEY_BEGIN }; 
     int[] to = new int[] { R.id.title, R.id.lecturer, R.id.time }; 

     // Now create an array adapter and set it to display using our row 
     SimpleCursorAdapter subjects = new SimpleCursorAdapter(this, R.layout.subject_row, cursor, from, to); 

     setListAdapter(subjects); 
    } 

이제 내 문제는 내가 내 DB에서 3 다른 열을 추가하려면 다음과 같은 얻고 싶은 것이 있습니다 :

  • "("+ DatabaseAdapter.KEY_TYPE + ")"+ DatabaseAdapter.KEY_TITLE
  • DatabaseAdapter.KEY_LECTURER
  • 새로운 날짜 DatabaseAdapt (DatabaseAdapter.KEY_BEGIN)
  • 새로운 날짜 (예 .KEY_END) ->이 두 가지는 dd.MM 방식으로 하나의 TextView에 있어야합니다. HH : mm (이 시작 출신) - HH : mm (이 END에서이다)

내가 그렇게 할 수 있어요 방법을 모른다 - 저를 도와주세요 :)

+0

질문을 좀 더 명확하게하면 더 좋아질 것 같습니다. 아무도 나머지 코드가 어떻게 보이는지 또는 고민하고 있는지 추측하기 위해 시간을 보내고 싶어하지 않습니다. :) – rogerkk

+0

나머지 코드는 무엇을 의미합니까? 나는 당신이 관심있는 코드를 확실히 모른다. –

+0

나는 그 문제가 정말로 무엇인지 잘 모르겠습니다. 커서에 모든 입력란이 포함되어 있지 않습니까? 그렇다면 fetchAllSubjects()에 대한 소스 코드를 보여줘야합니다. 문제가 커서에서 레이아웃으로 데이터를 가져 오는 것이므로 레이아웃을 표시해야합니다. – rogerkk

답변

0

확인 I을 마침내 당신이 정말로 원했던 것을 알아 냈습니다.

"SimpleCursorAdapter"를 직접 사용하는 대신 사용자가 원하는대로 데이터를 관리 할 수있는 자체 Cursor 어댑터를 만들 수 있습니다. 새 어댑터 "SubjectsAdapter.java"를 만듭니다. 이 어댑터에서는 "bindView"및 "newView"를 재정의합니다. 이렇게하면 커서에보기를 적용 할 수 있습니다. 그러나 그렇게하기 전에 커서에서 데이터를 변경할 수있는 기회를 제공합니다.

이렇게하면 무엇을해야하는지 알 수 있습니다.

private void fillData() 
    { 
     cursor = mDbAdapter.fetchAllSubjects(); 
     startManagingCursor(cursor); 


     SubjectsAdapter subjectsAdapter = new SubjectsAdapter(this, cursor); 
     setListAdapter(subjectsAdapter); 
    } 


//SubjectsAdapter.java - make changes to fix bugs/compilation errors. This is untested. 
public class SubjectsAdapter extends ResourceCursorAdapter 
{ 
    public SubjectsAdapter(Context context, Cursor cur) { 
     super(context, R.layout.subject_row, cur); 
    } 

    @Override 
    public View newView(Context context, Cursor cur, ViewGroup parent) 
    { 
     LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     return li.inflate(R.layout.subject_row, parent, false); 
    } 

    @Override 
    public void bindView(View view, Context context, Cursor cursor) 
    {   
     TextView titleText = (TextView)view.findViewById(R.id.title); 
      titleText.setText(cursor.getString(cursor.getColumnIndex(DatabaseAdapter.KEY_TITLE))); 

     //You can add code to retrieve other columns here. 

     //This is where you retrieve the date in long format from cursor, convert it to a required format, and then using it. 
     TextView beginTimeText = (TextView)view.findViewById(R.id.time);    
     Long lBeginDate = cursor.getLong(cursor.getColumnIndex(DatabaseAdapter.KEY_BEGIN)); 
     String sBeginDate = getFormattedDate(lBeginDate);   
     beginTimeText.setText(sBeginDate); 
    } 

    private String getFormattedDate(Long lDate) 
    { 
     SimpleDateFormat smdf = new SimpleDateFormat("MM/dd/yyyy h:mm:ss a");  
     String sDate = smdf.format(lDate)); 
     return sDate; 
    } 
} 
+0

Mmh ID, Type, Title, Lecutrer, Room, Begin (getTime()으로 변환하지 않음), End (thats) Begin에서와 동일). 그리고 3 개의 TextViews가있는 TwoLineListItem이 생겼습니다. 첫 번째 TextView는 첫 번째 점의 Text를 가져와야하고, 두 번째 점은 두 번째 점을 표시해야하고 마지막 TextView는 Date를 표시해야합니다. 내 마지막 지점에서 설명합니다 ... 많이 고마워요 :) –

+0

내 문제에 대한 해결책을 알고 계십니까? 개별 커서를 만들어야한다고 생각합니까? –

+0

@Prengepower 귀하의 의견을 바탕으로 답변을 변경했습니다. "Begindate를 오랫동안 사용하기 때문에 레이아웃에 넣기 전에 Integer를 날짜로 변환 할 수 있습니다." 위의 답변을 참조하십시오. –

관련 문제