Search code examples
androidandroid-cursoradapter

Android - Custom CursorAdapter keeps calling BindView when ViewFlipper flips


I've built a custom CursorAdapter that needs to populate a ListView with tweets... Just simple date and tweet will be shown... It works great, the only thing is that if I add a Log entry I notice that the BindView method is getting called continuously every 8 seconds for every item...Which corresponds with a ViewFlipper's flipDuration elsewhere on the screen...(Verified by changing the duration and seeing BindView being called with the new interval times)

The ViewFlipper does use the same layout xml as the ViewList and the same data (though I make 2 separate calls to the DB to load them in)

Is this normal behaviour?

Method that renders the List and Flipper:

public void loadTwitterFeed(){

    ListView twitterList = (ListView) findViewById(R.id.twitterList);

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    dbHelper dbh = new dbHelper(this);

    dbh.openRead();

    twitterDbAdapter adapt = new twitterDbAdapter(this, dbh.getTweets());

    twitterList.setAdapter(adapt);

    ViewFlipper flipper = (ViewFlipper) findViewById(R.id.twitterFlipper);

    ArrayList<twitterTweet> tweets = dbh.getTweetsAsList();

    for( twitterTweet obj : tweets){

        View tItem = (View) inflater.inflate(R.layout.twitter_item, flipper, false);

        TextView tv1 = (TextView) tItem.findViewById(R.id.textView1);
        TextView tv2 = (TextView) tItem.findViewById(R.id.textView2);

        tv1.setText(obj.getDate());
        tv2.setText(obj.getText());

        flipper.addView(tItem);

    }

    dbh.close();

    flipper.setFlipInterval(5000);
    flipper.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left));
    flipper.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.slide_out_right));
    flipper.startFlipping();

}

My custom adapter:

public class twitterDbAdapter extends CursorAdapter{
private LayoutInflater inflater;

public twitterDbAdapter(Context context, Cursor cursor){

    super(context, cursor);
    inflater = LayoutInflater.from(context);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    Log.i("extra","binding");
    TextView textView = (TextView) view.findViewById(R.id.textView1);
    TextView textView2 = (TextView) view.findViewById(R.id.textView2);

    textView.setText(cursor.getString(cursor.getColumnIndex("createdat")));
    textView2.setText(cursor.getString(cursor.getColumnIndex("tweet")));

}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    View view = inflater.inflate(R.layout.twitter_item, parent, false);
    return view;
}

}

Solution

  • It works great, the only thing is that if I add a Log entry I notice that the BindView method is getting called continuously every 8 seconds for every item

    I doubt that bindView is called for all the items in the ListView, most likely the method is called for the visible items(plus a few extra calls).

    As the ViewFillper flips through it's children Android needs to re-layout the current screen content, this is probably the cause for your bindView method being called at the ViewFlipper's switching interval. It shouldn't matter for you.