永远不会调用自定义适配器的 getView() 方法

getView() method of custom adapter never gets called

我阅读了有关该主题的各种 SO 线程,但其中 none 似乎适用于我的代码。

我正在尝试使用带有自定义 NearbyAdapter 的 ListView 填充片段。但是,我的 getView() 方法从未被调用(正如我的日志未显示所证明的那样)。视图本身似乎适当地附加到我的片段,如显示的视图中的按钮所示,但不是 ListView。

相关NearbyListFragment.java代码:

public class NearbyListFragment extends ListFragment {

    private int mImageSize;
    private boolean mItemClicked;
    private NearbyAdapter mAdapter;

    private List<Place> places;
    private LatLng mLatestLocation;

    private static final String TAG = "NearbyListFragment";

    public NearbyListFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        Log.d(TAG, "NearbyListFragment created");

        View view = inflater.inflate(R.layout.fragment_nearby, container, false);
        return view;
    }

    //TODO: Do asynchronously?
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {

        //Load from data source (NearbyPlaces.java)

        mLatestLocation = ((NearbyActivity) getActivity()).getmLatestLocation();
        //FIXME: Hardcoding mLatestLocation to Michigan for testing
        //mLatestLocation = new LatLng(44.182205, -84.506836);

        places = loadAttractionsFromLocation(mLatestLocation);
        mAdapter = new NearbyAdapter(getActivity(), places);

        ListView listview = (ListView) view.findViewById(R.id.listview);
        //setListAdapter(mAdapter);
        listview.setAdapter(mAdapter);

        Log.d(TAG, "Adapter set to ListView");
        mAdapter.notifyDataSetChanged();
    }

    private class NearbyAdapter extends ArrayAdapter {

        public List<Place> placesList;
        private Context mContext;

        public NearbyAdapter(Context context, List<Place> places) {
            super(context, R.layout.item_place);
            mContext = context;
            placesList = places;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // Get the data item for this position
            Place place = (Place) getItem(position);
            //FIXME: This never gets called
            Log.d(TAG, "Place " + place.name);

            // Check if an existing view is being reused, otherwise inflate the view
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_place, parent, false);
            }

            // Lookup view for data population
            TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
            TextView tvDesc = (TextView) convertView.findViewById(R.id.tvDesc);

            // Populate the data into the template view using the data object
            tvName.setText(place.name);
            tvDesc.setText(place.description);

            // Return the completed view to render on screen
            return convertView;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

片段的布局文件,fragment_nearby.xml :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/btn_New">
    </ListView>
    <Button
        android:id="@+id/btn_New"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginBottom="20dp"
        android:text="Button"
        android:width="170dp"
        android:layout_alignParentBottom="true" />

</RelativeLayout>

以及项目的布局文件,item_place.xml :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <TextView
        android:id="@+id/tvName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Name" />
    <TextView
        android:id="@+id/tvDesc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Desc" />
</LinearLayout> 

编辑:有没有人想真正包含反对票的原因?特别是当像 Custom Adapter for List View 这样的东西有 129 票时?

将以下内容添加到您的适配器中:

@Override
public int getCount() {
    return placesList.size();
}

在此之后,您很可能会遇到 getItem 错误,因此您还需要将其覆盖到 return 列表中的对象。

您必须重写 ArrayAdapter 中的 getCount() 方法才能像这样初始化列表视图:

@Override
    public int getCount() {
        return placesList.size();
    }

问题是 ArrayAdapter 不知道 List 位置: 用这个来修复它:

private static class NearbyAdapter extends ArrayAdapter<Place> {
   public NearbyAdapter(Context context, List<Place> places) {
      super(context, R.layout.item_place, places);
      mContext = context;
      placesList = places;
   }
}

P/s:在这种情况下,我认为您需要更多控制才能将地点数据设置到您的视图中。考虑使用 BaseAdapter 而不是 ArrayAdapter。