对于我的 SherlockListFragment,我有以下代码,
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`` 函数时起作用。