2011-12-31 32 views
1

我已經看到以前的主題像這樣的主題:Android: Issue with newView and bindView in custom SimpleCursorAdapter仍然不明白我的代碼是什麼問題。我已經閱讀過關於它的書籍等等,但是爲什麼我沒有把它做好。SimpleCursorAdapter bindView和回收的視圖

問題是,當我滾動我的listView我得到錯誤的數據行,但如果我點擊行並進入下一個活動它對應於它應該有的數據。

我實現了viewHolder來綁定日期,以綁定到newView/bindview方法中的行。

一切都很好,直到我開始滾動列表視圖。這就是行開始混合起來的時候。這與回收意見有關,我知道,如何解決這個問題,我仍然試圖弄清楚。我會愛任何幫助!

我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 2011-12-31 22:04:24

+0

標籤持有人模式怎麼樣,我仍然得到重複! – Skynet 2013-12-19 09:04:27

相關問題