-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()。 –

相关问题