我有一个由自定义适配器支持的列表视图。适配器的项目列表中有 17 个项目,但只显示前 2 个项目。我已经跟踪getView()
并确认它仅在位置 0 和 1 中被调用。
填充适配器并将其设置在onCreate()
parentActivity
中。
FavouritesListAdapter adapter = new FavouritesListAdapter(favourites, this);
((ListView) findViewById(R.id.list)).setAdapter(adapter);
adapter.notifyDataSetChanged(); // not needed but put here out of desperation!
适配器签名和getView()
方法:
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;
}
日志输出:
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
活动 XML:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/white"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/list"
android:background="@color/white"
android:cacheColorHint="#00000000"
android:divider="#FF0000"
android:dividerHeight="1px"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</ScrollView>
前两项已正确显示。
我错过了什么?谢谢!