这是我的主要标签活动:
public class LocationTabActivity extends RoboSherlockFragmentActivity{
private MapFragment mMapFragment;
private MyListFragment mMyListFragment;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Exchanger.mMapView = (MapView)findViewById(R.id.mapView1);
getSupportActionBar()
.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab newTab0 = getSupportActionBar().newTab();
newTab0.setText("Location");
newTab0.setTabListener(new TabListener<MyListFragment>(
LocationTabActivity.this, "listFragment", MyListFragment.class));
ActionBar.Tab newTab1 = getSupportActionBar().newTab();
newTab1.setText("Map");
getSupportActionBar().addTab(newTab0);
//getSupportActionBar().addTab(newTab1);
}
public static class TabListener<T extends Fragment> implements ActionBar.TabListener {
private Fragment mFragment;
private final Activity mActivity;
private final String mTag;
private final Class<T> mClass;
/** Constructor used each time a new tab is created.
* @param activity The host Activity, used to instantiate the fragment
* @param tag The identifier tag for the fragment
* @param clz The fragment's Class, used to instantiate the fragment
*/
public TabListener(Activity activity, String tag, Class<T> clz) {
mActivity = activity;
mTag = tag;
mClass = clz;
}
/* The following are each of the ActionBar.TabListener callbacks */
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft1) {
// Check if the fragment is already initialized
FragmentManager fragMgr = ((FragmentActivity) mActivity).getSupportFragmentManager();
FragmentTransaction ft = fragMgr.beginTransaction();
if (mFragment == null) {
// If not, instantiate and add it to the activity
mFragment = Fragment.instantiate(mActivity, mClass.getName());
ft.add(android.R.id.content, mFragment, mTag);
} else {
// If it exists, simply attach it in order to show it
ft.attach(mFragment);
}
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
}
这是我的 MyListFragment:
public class MyListFragment extends SherlockFragment {
public static final String TAG = "listFragment";
private final String[] mItems = { "item1","item2","item3" };
ArrayList<Location> locations = new ArrayList<Location>();
private View view;
public MyListFragment() {}
TextView address;
@Override
public void onCreate(Bundle arg0) {
super.onCreate(arg0);
setRetainInstance(true);
Location location = new Location();
location.address = "hello";
locations.add(location);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup vg, Bundle data) {
// Inflate the ListView layout file.
view = inflater.inflate(R.layout.location_list_fragment,vg, false);
address =(TextView) view.findViewById(R.id.address);
final ListView locationListLayout = (ListView) view.findViewById(R.id.locationListLayout);
LocationListAdapter locationList = new LocationListAdapter(getActivity(),R.layout.location_list_fragment,locations);
locationListLayout.setAdapter(locationList);
return view;
}
@Override
public void onViewCreated(View arg0, Bundle arg1) {
super.onViewCreated(arg0, arg1);
}
}
这是我的 LocationListAdapter:
public class LocationListAdapter extends ArrayAdapter<Location> {
public ArrayList<Location> locations;
public Context c;
public LocationListAdapter(Context context, int textViewResourceId,ArrayList<Location> objects) {
super(context, textViewResourceId,objects);
this.locations = objects;
c = context;
// TODO Auto-generated constructor stub
}
public View getView(int position,View convertView,ViewGroup parent){
View v = convertView;
if (v == null ){
LayoutInflater li = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.location_list_item,null);
}
Location location = locations.get(position);
if(location != null){
TextView locationAddress = (TextView)v.findViewById(R.id.address);
locationAddress.setText(location.address);
}
return v;
}
}
是的。我正在将一个位置数组列表添加到一个选项卡的片段中的 listView 中。
但是,没有数据显示。没有一个列表。我哪里错了?