0

这是我的主要标签活动:

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 中。

但是,没有数据显示。没有一个列表。我哪里错了?

4

1 回答 1

1

您是否尝试过使用片段的setListAdapter()功能而不是视图的功能setAdapter()

那是...

setListAdapter(locationList);
// instead of ...
// locationListLayout.setAdapter(locationList);

哦,列表视图需要有 id@id/android:list才能工作。

编辑:

您的片段是否正在显示?尝试使用提供的 FT 而不是您自己的(缺少 .commit())

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) { //*** <-- renamed ft1
        // Check if the fragment is already initialized

        // You have no .commit() on this transaction but you should probably use the one passed in, which gets committed for you externally.
        //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);
        }
    }
于 2012-12-04T08:20:29.967 回答