2011-12-31 3 views
1

이 주제와 관련하여 이전 주제 (Android: Issue with newView and bindView in custom SimpleCursorAdapter)를 보았지만 여전히 내 코드의 문제점을 이해할 수 없습니다. 나는 그것에 관한 책을 읽었고, 등등, 왜 내가 올바르게 못하고 있는지 알 수 없다.SimpleCursorAdapter bindView 및 재활용 된 뷰

문제는 내가 내 listView를 스크롤 할 때 잘못된 데이터로 행을 가져 오지만 행을 클릭하고 다음 활동으로 넘어 가면 해당 데이터에 해당합니다.

newView/bindview 메소드의 행에 바인드 할 날짜에 대한 viewHolder를 구현했습니다.

목록보기를 스크롤하기 시작할 때까지 모든 항목이 잘 표시됩니다. 행이 모두 섞일 때입니다. 그것은 견해의 재활용과 관련이 있습니다. 나는 그것을 고치는 법을 알고 있으며, 여전히 그것을 파악하려고합니다. 나는 어떤 도움을 원할 것이다!

내 SimplecursorAdapter의 코드 :

public class DropNotesAdapter extends SimpleCursorAdapter { 

private LayoutInflater layoutInflater; 
private Utils mUtils = new Utils(); 
private int layout; 
private int titleColIndex; 
private int modifiedColIndex; 
private int priorityColIndex; 

public DropNotesAdapter(Context context, int layout, Cursor c, String[] from, int[] to) { 

    super(context, layout, c, from, to); 
    this.layout = layout; 
    layoutInflater = LayoutInflater.from(context); 
    titleColIndex = c.getColumnIndex(DropNotesDBAdapter.KEY_TITLE); 
    modifiedColIndex = c.getColumnIndex(DropNotesDBAdapter.KEY_MODIFIED); 
    priorityColIndex =c.getColumnIndex(DropNotesDBAdapter.KEY_PRIORITY); 
} 

@Override 
public View newView(Context context, Cursor cursor, ViewGroup parent) { 

    View view = layoutInflater.inflate(layout, parent, false); 

    TextView titleText = (TextView) view.findViewById(R.id.title_line); 
    TextView modifiedText = (TextView) view.findViewById(R.id.date_line); 
    ImageView priorityTag = (ImageView) view.findViewById(R.id.item_priority); 

    NoteHolder holder = new NoteHolder(); 
    holder.titleView = titleText; 
    holder.modifiedView = modifiedText; 
    holder.priorityView = priorityTag; 
    holder.title = cursor.getString(titleColIndex); 
    holder.modified = mUtils.formatDate(mUtils.formatDateFromString (cursor.getString(modifiedColIndex), context, "dd-MM-yyyy")); 
    holder.priorityResId = mUtils.getPriorityResourceId(cursor.getInt(priorityColIndex)); 
    view.setTag(holder); 

    return view; 
} 

@Override 
public void bindView(View view, Context context, Cursor cursor) { 

    NoteHolder holder = (NoteHolder) view.getTag(); 

    holder.titleView.setText(holder.title); 
    holder.modifiedView.setText(holder.modified); 
    holder.priorityView.setImageResource(holder.priorityResId); 

    Typeface tf = Typeface.createFromAsset(context.getAssets(), "fonts/ARLRDBD.TTF"); 
    holder.titleView.setTypeface(tf); 
    holder.modifiedView.setTypeface(tf); 
} 

private static class NoteHolder { 
    TextView titleView; 
    TextView modifiedView; 
    ImageView priorityView; 
    String title; 
    String modified; 
    int priorityResId; 
} 

}

답변

2

NoteHolder에서 titlemodifiedpriorityResId을 제거하십시오. 홀더는 연관된 행에있는 위젯을 보유합니다. 홀더는 이 아니며 모델 데이터를 보유하지 않습니다. 모델 데이터는 Cursor에서 bindView()입니다.

+0

아주 간단했습니다. 필자는 열을 저장하는 예를 보았고 왜 데이터 자체를 저장하지 않을지 생각했습니다 ... 감사합니다. 많은 시간과 두통을 덜어 줬어! – greven

+0

태그 홀더 패턴은 어떻게됩니까? – Skynet

관련 문제