0

我有下面的代碼,我SherlockListFragment,SherlockListFragment不更新時setListAdapter被稱爲

public class RoutesListFragment extends SherlockListFragment { 

    private ArrayList<Route> mItems; 
    private static final String TAG = "ROUTES LIST"; 

    RoutesAdapter mAdapter; 

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

     View view = inflater.inflate(R.layout.fragment_main_lists, container, 
       false); 

      // When the setListAdapter is called here, 
      // it works without any problems 

     return view; 
    } 

    public void updateAdapter() { 
     // populate the list 
     mItems = Route.getRouteList(); 
     Log.d(TAG, "Size of mItems : " + mItems.size()); 
     mAdapter = new RoutesAdapter(getActivity(), R.layout.list_item_routes, 
       mItems); 

     this.setListAdapter(mAdapter); 

    } 

    @Override 
    public void onListItemClick(ListView l, View v, int position, long id) { 
     Log.d(TAG, "Clicked item " + position); 
    } 

    private class RoutesAdapter extends ArrayAdapter<Route> { 

     private ArrayList<Route> mItems; 
     private Context mContext; 
     int mTextViewResource; 

     public RoutesAdapter(Context context, int textViewResource, 
       ArrayList<Route> items) { 
      super(context, textViewResource); 

      this.mItems = items; 
      this.mContext = context; 
      this.mTextViewResource = textViewResource; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      View view = convertView; 

      if (view == null) { 
       LayoutInflater inflater = (LayoutInflater) mContext 
         .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       view = inflater.inflate(mTextViewResource, null); 
      } 

      Route route = mItems.get(position); 

      ((TextView) view.findViewById(R.id.route_name)).setText(route 
        .getRouteName()); 
      ((TextView) view.findViewById(R.id.route_number)).setText(Integer 
        .toString(route.getRouteNumber())); 
      ((TextView) view.findViewById(R.id.direction)).setText(route 
        .getDirection().toString()); 
      ((TextView) view.findViewById(R.id.start_end)).setText(route 
        .getStartingStop() + " - " + route.getEndingStop()); 

      return view; 
     } 

    } 
} 

更多詳細信息

  • 我已經檢查,以確保該ArrayList<Route> mItems不爲空。

  • updateAdapter()函數從onPostExecute函數調用AsyncTask函數。

  • SherlockListFragment用於ViewPagerIndicator選項卡。

  • 它適用於setListAdapter() is called from the onCreateView`函數。

回答

3

你可以改變的數組,它是在適配器然後調用

adapter.notifyDataSetChanged();

而不是創建一個新的適配器

+0

謝謝!現在就去嘗試一下。 – yasith

1

我一定要添加到jiduvah的答案。

顯然ArrayAdapter有它自己的副本項目ArrayList<Route>。因此。在致電mAdapter.notifyAll()之前,我必須致電mAdapter.addAll(Route.getRouteList())

現在代碼爲updateAdapter函數是。

public void updateAdapter() { 
    mAdapter.addAll(Route.getRouteList()); 
    mAdapter.notifyDataSetChanged(); 
} 

參考

listview not updating with notifydatasetchanged() call

+0

這將數組添加到現有數組的底部https://developer.android.com/reference/android/widget/ArrayAdapter.html#addAll(java.util.Collection ) 調用.notifyDataSetChanged仍然正確();當你更新你的列表時,你添加到現有陣列或替換它的天氣 – jiduvah

+0

手動向mItem添加條目似乎沒有改變ArrayAdapter正在使用的內容。 – yasith

+1

不,它不會。正如我在我的回答中所說的,您需要更改適配器中的數組。你可以在改變數組的適配器中創建一個方法。 – jiduvah