-1

我使用bindView()方法是自定義CursorAdapter實現動態添加文本視圖到列表。如何在自定義`CursorAdapter`中重新使用動態添加的視圖

每個列表項被list_item佈局其含有來自Android Flowlayout

<!--List Item Layout--> 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:background="#FFFFFF"> 

    <!--Flow Layout--> 
    <org.apmem.tools.layouts.FlowLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="@dimen/view_padding" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     android:id="@+id/hash_tag_layout" 
     > 
    </org.apmem.tools.layouts.FlowLayout> 

</LinearLayout> 

flow_layout佈局的加入flow_layoutlist item每個實例文本視圖的數量反映了光標返回的行值的數量來表示。

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

    // Flow layout wraps new views around the screen. 
    FlowLayout flowLayout = (FlowLayout) view.findViewById(R.id.flow_Layout); 

    // getRowValues() puts row values from cursor into an array list. 
    ArrayList<> rowValues = getRowValues(cursor); 

    // A new text view is created and inserted into Flow layout 
    // for each value in rowValues 
    TextView tv; 
    for value in rowValues { 
     tv = = new TextView(ctx); 
     tv.setText(value); 
     flowLayout.addView(tv); 
    } 

} 

再次重申,我希望每個flow_layoutlist_item每個實例內的文本視圖的數量,以反映光標返回行值的數量。然而,每當我重新滾動列表項目時,該特定項目中的文本視圖的數量加倍,並且另外,綁定數據有時會反映在光標的位置和列表的位置之間對稱地反映項目。我認爲這個問題與回收舊文本視圖有關。如何防止堆疊到舊文本視圖中的新文本視圖?

這是自定義光標適配器

public class DatabaseAdapter extends CursorAdapter { 

     Context ctx; 

     public DatabaseAdapter(Context context, Cursor cursor, int flags) { 

      super(context, cursor, 0); 
      ctx = context; 
     } 

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

      View v = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false); 
      return v; 
     } 

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

      // Flow layout wraps new views around the screen. 
      FlowLayout flowLayout = (FlowLayout) view.findViewById(R.id.flow_Layout); 

      // getRowValues() puts row values from cursor into an array list. 
      ArrayList<> rowValues = getRowValues(cursor); 

      // A new text view is created and inserted into Flow layout 
      // for each value in rowValues array list 
      TextView tv; 
      for value in rowValues { 
       tv = = new TextView(ctx); 
       tv.setText(value); 
       flowLayout.addView(tv); 
      } 
     } 
} 
+1

設置ID您'tv1',然後你可以調用'findViewById'檢查,如果該視圖已經添加或不添加,但實際上爲什麼不在'R.layout.list_item'中添加'TextView'? – pskink

+0

@pskink謝謝。這工作。實際上,我嘗試設置ID並使用'findViewById',但我沒有考慮在'createView'方法中使用'findViewById',而是在'bindView'中使用它'...有什麼區別? –

+0

爲什麼不在'R.layout.list_item'中添加'TextView'? – pskink

回答

0

完全implemtation在你LinearLayout您必須已經加入了TextView,id爲tv1說。隨後的

代替:

TextView tv1 = new TextView();

這樣做:

LinearLayout layout = (LinearLayout) view.findViewById(R.id.linear_layout); 
TextView tv1 = layout.findViewById(R.id.tv1); 
tv1.setText(Title); 
1
if(LinearLayout.getChildCount() > 0) 
     LinearLayout.removeAllViews(); 
+0

你也可以重寫getView()。 –

相關問題