2016-08-29 142 views
-2

我已經閱讀了有關該主題的各種SO線程,但它們中沒有一個似乎適用於我的代碼。自定義適配器的getView()方法永遠不會被調用

我想用我的自定義NearbyAdapter填充ListView的一個片段。但是,我的getView()方法永遠不會被調用(如我的日誌沒有顯示的那樣)。視圖本身似乎適當地附加到我的片段,如顯示在視圖中的按鈕所示,但不是ListView。

enter image description here

相關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> 

編輯:有沒有人想要實際包含downvote的原因?特別是當像Custom Adapter for List View這樣的東西有129個upvotes?

+0

嘗試在適配器上調用'invalidate()'。 –

回答

1

的問題是,ArrayAdapter不知道名單的地方: 使用此解決它:

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。

+0

這確實解決了這個問題,謝謝! – misaochan

0

添加下列適配器:

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

getItem這一點,你最有可能遇到的錯誤後,所以你需要重寫,以及從列表中返回的對象。

0

你必須覆蓋ArrayAdaptergetCount()方法來初始化列表視圖這樣的:

@Override 
    public int getCount() { 
     return placesList.size(); 
    } 
相關問題