2012-12-04 4 views
0

의 탭에서 조각에 배열 어댑터를 추가 할 수 없습니다 : 이것은 내 MyListFragment입니다이 내 주요 탭 활동 안드로이드

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 

     } 



    } 







} 

:

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; 

    } 
} 

예. 나는 배열의리스트를 tab에있는 조각의 listView에 추가하려고한다.

그러나 데이터가 표시되지 않습니다. 하나의 목록이 아닙니다. 내가 어디로 잘못 가고 있니?

답변

1

보기의 setAdapter() 대신 조각의 setListAdapter() 함수를 사용해 보셨습니까?

입니다

...

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); 
     } 
    } 
+0

여전히 동일한 문제입니다. 데이터가 표시되지 않습니다. – Hick

+0

어디서나 ft.commit()이 표시되지 않습니다. 그게 될 수 있을까요? 대신, 당신은 아마도 당신에게 전달 된 FragmentTransaction을 사용해야 할 것입니다. 그리고 커밋을 건너 뛰고, 문서로 판단하십시오. –

+0

(FragmentTransaction 가설이있는 수정 된 게시물) –

관련 문제