the_prole
the_prole

Reputation: 8985

How to re-use dynamically added views in custom `CursorAdapter`

I use bindView() method is custom CursorAdapter implementation to dynamically add text views to a list.

Each list item is represented by list_item layout which contains flow_layout layout from 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>

The number of text views added to flow_layout per each instance of list item reflects the number of row values returned in the cursor.

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

}

To re-iterate, I want the number of text views inside each flow_layout per each instance of list_item to reflect the number of row values returned by the cursor.

However, every time I re-scroll over a list item, the number of text views in that particular item doubles, and additionaly, binded data sometimes does the reflect symetrically between the position of the cursor and the position of the list item. I think the problem is related to the recycling of old text views. How can I prevent new text views from stacking onto old textviews?

Here is full implemtation of custom cursor adapter

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

Upvotes: -1

Views: 40

Answers (2)

the_prole
the_prole

Reputation: 8985

if(LinearLayout.getChildCount() > 0)
        LinearLayout.removeAllViews();

Upvotes: 1

Ankit
Ankit

Reputation: 6622

In your LinearLayout you must have already added a TextView, say with id tv1.

Then instead of:

TextView tv1 = new TextView();

Do this:

LinearLayout layout = (LinearLayout) view.findViewById(R.id.linear_layout);
TextView tv1 = layout.findViewById(R.id.tv1);
tv1.setText(Title);

Upvotes: 0

Related Questions