2016-10-04 3 views
0

私はbindView()メソッドを使用して、カスタムのCursorAdapterの実装を使用して、テキストビューをリストに動的に追加します。カーソルアダプターのビューを動的に追加して、それぞれの上にスタッキングする

各リストアイテムはlist itemの各インスタンスごとflow_layoutに追加されたテキスト・ビューの数は、カーソルで返される行の値の数を反映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レイアウトを含むlist_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); 
    } 

} 

再反復するために、私はlist_itemの各インスタンスごとに各flow_layout内のテキストビューの数は、カーソルによって返される行の値の数を反映します。

しかし、リストアイテムを再スクロールするたびに、特定のアイテムのテキストビューの数が2倍になり、さらにバインドされたデータがカーソルの位置とリストの位置の間に対称的に反映されることがあります項目。問題は古いテキストビューのリサイクルに関連していると思います。

新しいテキストビューが古いテキストビューに重なって表示されないようにするにはどうすればよいですか?特定の子ビューに対してカスタムカーソルアダプタのビューリサイクルをオーバーライドすることは可能ですか?ガーベージコレクションを強制的に解除して画面から外すことはできますか?ここで

は、カスタムカーソルアダプタのフルimplemtation

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); 
      } 
     } 
} 

答えて

0
if(flowLayout.getChildCount() > 0) 
     flowLayout.removeAllViews(); 
です
関連する問題