2013-05-05 77 views
0

我有一個由自定義CursorAdapter提供的ListView。 ListView行非常簡單 - 左側有一個CheckBox和兩個TextView。當「點擊」複選框時,將啓動操作模式,以便用戶可以從列表中刪除多個項目。在自定義CursorAdapter中無限調用bindView

雖然爲了處理複選框和OnItemClickedListeners並跟蹤哪些盒子被選中,我注意到bindView被無休止地調用。而且,我的確意味着無窮無盡:如果ListView有一個項目或100,那麼在片段生命週期的持續時間內,bindView將被多次調用一次。現在,ListView的行爲與我想要的完全一樣,但顯然,我的代碼或XML佈局非常錯誤。

在這個網站上搜索一個答案還沒有結果,所以我在這裏發佈我的第一個問題。

我爲ListView的每一行XML佈局看起來是這樣的:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:descendantFocusability="blocksDescendants"  
    > 

    <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" 
     android:focusable="false" 
     android:clickable="false" 
     android:id ="@+id/chkBox" /> 
    <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" 
     android:text="Some example text" 
     android:layout_weight=".5" 
     android:id="@+id/txtExercise" /> 
    <TextView 
     android:id="@+id/txtDuration" 
     android:layout_width="fill_parent" android:layout_height="wrap_content" 
     android:text="0:00" android:gravity="right" 
     android:layout_weight=".5" > 

    </TextView> 


    </LinearLayout> 

我不知道還有什麼其他的代碼顯示在這裏,所以我打算把我的NewView的和bindView方法淺析。希望有人能夠幫助我找出問題。

@Override 
public View newView(Context context, Cursor cursor, ViewGroup parent) {  
    final LayoutInflater inflater = LayoutInflater.from(context); 
    View v = inflater.inflate(layout, parent, false); 
    Log.i(TAG, "newView=" + parent.getId()); 

    final ViewHolder holder = new ViewHolder(); 
    holder.txtExercise = (TextView) v.findViewById(R.id.txtExercise); 
    holder.txtDuration = (TextView) v.findViewById(R.id.txtDuration); 
    holder.check = (CheckBox) v.findViewById(R.id.chkBox);     
    holder.check.setOnCheckedChangeListener(new OnCheckedChangeListener(){ 

     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean checked) { 
      checkedList.put((Integer) buttonView.getTag(),checked);    
     } 

    }); 
    v.setTag(holder);  
    return v; 
} 


@Override 
public void bindView(View v, Context arg1, Cursor c) { 
    String exercise = c.getString(c.getColumnIndex("_ExcerciseName")); 
    int sec = c.getInt(c.getColumnIndex(WorkoutLog.DURATION)); 
    String duration = returnDurationString(sec); 
    Log.i(TAG, "bindview called"); 
    ViewHolder holder = (ViewHolder) v.getTag(); 

    if(holder.txtExercise !=null) 
     holder.txtExercise.setText(exercise); 
    if(holder.txtDuration != null) 
     holder.txtDuration.setText(duration); 
    if(holder.check !=null) { 
     holder.check.setTag(c.getInt(c.getColumnIndex(WorkoutLog._ID))); 
     int x = (Integer) holder.check.getTag();    
     holder.check.setChecked(checkedList.get(x));    
    } 
} 

任何意見,將不勝感激!我對Android開發還很陌生,所以我可能會犯一些完全愚蠢的錯誤,但我沒有經驗和/或知識來嘗試解決這個問題。

[編輯] - 從我的ListView中移除標籤解決了問題!

+0

你可以發佈你有你的列表視圖的佈局嗎? – amp 2013-05-05 11:30:14

回答

3

我假設你的android:layout_heightlistview設置爲wrap_content。嘗試切換到fill_parent

檢查這個答案:Why does Wrap_Content fire BindView more than once

喜歡的東西:

<ListView 
     android:id="@+id/my_listView" 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" 
     //... 
</ListView> 

希望這有助於!

+0

安培,你的意見並沒有解決問題....但他們DID導致我找到問題的原因。在驗證了我正在使用fill_parent作爲android:layout_height設置後,我開始研究包含ListView本身的佈局。我發現listview被封裝在一個中。刪除,解決了這個問題。 – Workforce7 2013-05-05 15:54:24

+0

注意:我必須更改兩個才能包裝內容,因爲在我的項目中我們有一個具有網格的平板電腦佈局。 – StarWind0 2015-04-21 16:49:26

相關問題