2010-07-02 2 views
1

데이터베이스에서 요소를 가져 와서 클릭 가능한 목록보기로 표시하는 주 활동이 있습니다. 데이터베이스 내에서 부울 필드를 액세스하는 것이 가능하다,SimpleCursorAdapter를 사용하여 데이터베이스에서 특정 요소를 굵게 표시하는 방법

private void fillData() { 
    // Get all of the notes from the database and create the item list 
    Cursor c = RequestManager.getRequests(getApplicationContext()); 
    startManagingCursor(c); 
    String[] from = new String[] { DataBase.KEY_TITLE, DataBase.KEY_BODY }; 
    int[] to = new int[] { R.id.text1, R.id.text2 }; 

    // Now create an array adapter and set it to display using our row 
    SimpleCursorAdapter notes = 
     new SimpleCursorAdapter(this, R.layout.notes_row, c, from, to); 
    setListAdapter(notes); 
} 

궁금하고, 굵은 특정 요소는 필드 읽지 않은 상태로 표시되어있다 : 나는 작업을 수행하기 위해이 방법을 사용합니까? 요소는 각각 텍스트 뷰에 있으며 목록 뷰에 배치됩니다. 감사합니다

편집 : 제안을 사용하여 CursorAdapter 클래스를 확장했지만 목록의 요소가 굵게 표시되면 첫 번째 요소도 굵게 표시됩니다. 모든 요소가 읽은 것으로 표시되면 첫 번째 요소는 다시 굵게 표시되지 않습니다. 어떤 아이디어?

public void bindView(View view, Context context, Cursor cursor) { 
    TextView textRequestNo = (TextView) view.findViewById(R.id.text1); 
    TextView textMessage = (TextView) view.findViewById(R.id.text2); 
    StringBuilder requestNo = new StringBuilder(cursor.getString(cursor 
      .getColumnIndex("requestNo"))); 
    StringBuilder message = new StringBuilder(cursor.getString(cursor 
      .getColumnIndex("Message"))); 

    textRequestNo.setText(requestNo); 
    textMessage.setText(message); 
    if (cursor.getString(cursor.getColumnIndex("Read")).equals("false")) 
    { 
     textRequestNo.setTypeface(null, Typeface.BOLD); 
     textMessage.setTypeface(null, Typeface.BOLD); 
    } 
} 
@Override 
public View newView(Context context, Cursor cursor, ViewGroup parent) { 
    final View view = mInflater.inflate(R.layout.notes_row, parent, false); 
    bindView(view, context, cursor); 
    return view; 
} 

답변

2

불행히도이 기능을 구현하려면 자체 CursorAdapter를 구현해야 할 것입니다. 그것은 그렇게 나쁘지 않습니다. ResourceCursorAdapter의 하위 클래스를 만들면 bindView()의 구현을 직접 작성해야합니다. 이 구현 내부에서 커서를 읽으면 행을 굵게 표시할지 여부를 결정할 수 있습니다.

+0

감사합니다. 작동시킬 수 있으면 업데이트하겠습니다. – mbauer14

+0

대부분이 부분이 있지만, 내 문제로 주요 질문을 업데이트했습니다 : 다른 요청이 있으면 첫 번째 요청은 항상 굵게 표시됩니다. 이 경험이 있습니까? 감사합니다 – mbauer14

+0

그 이유는 ListView가 디스플레이를 최적화하는 방법 때문입니다. 가능한 한 뷰를 재사용합니다. CursorAdapters에서이 문제를 해결하는 방법을 정확히 모르겠지만 기본적으로 몇 가지 다른 유형의 행을 표시하고 각 유형에 대해 적절한보기 만 사용해야합니다. –

관련 문제