不为所有项调用适配器getView()

时间:2013-05-16 10:08:52

标签: android

我有一个由自定义适配器支持的列表视图。适配器在其项目列表中有17个项目,但仅显示前面的2.我已经跟踪getView()并确认它仅被调用位置0和1。

填充适配器并将其设置为父onCreate()的{​​{1}}。

Activity

适配器签名和FavouritesListAdapter adapter = new FavouritesListAdapter(favourites, this); ((ListView) findViewById(R.id.list)).setAdapter(adapter); adapter.notifyDataSetChanged(); // not needed but put here out of desperation! 方法:

getView()

logcat输出:

public class FavouritesListAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<Favourite> entries;

    public FavouritesListAdapter(ArrayList<Favourite> favourites, Context context) {
        this.entries = favourites;
        this.context = context;
    }

public View getView(int position, View convertView, ViewGroup parent) {

    RelativeLayout rl;

    Log.d(LOG_DEBUG_TAG, "Count:" + getCount() ", asking for pos " + position);

    if (convertView == null) {

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

        rl = (RelativeLayout) inflater.inflate(R.layout.favourite_tide_table, null);
        convertView = rl;

    } else { // convertView is not null (it's being recycled)

        Favourite thisFavourite = this.getItem(position);

        convertView = thisFavourite.getPortView();

    }

    return convertView;

}

活动XML:

Count:17, asking for pos 0
Count:17, asking for pos 0
Count:17, asking for pos 0
Count:17, asking for pos 1
Count:17, asking for pos 0
Count:17, asking for pos 0

前两项正确显示。

我错过了什么?谢谢!

1 个答案:

答案 0 :(得分:2)

ListView无法进入ScrollView

请参阅Romain Guy的this answer。来自Google I / O的this视频中的详细信息

如果你真的需要在ListView中添加ScrollView,你基本上需要ListView知道它的高度,这可以使用DougW回答提到的问题。