2012-09-19 8 views
0

Activity의 레이아웃이 2 개 Fragment으로되어 있습니다. 단편 레이아웃 중 하나에 GridView이 포함되어 있습니다. 이 단편은 그것이 GridView 용 어댑터의 인스턴스를 호출GridView 내의 조각 : getView가 호출되지 않습니다

public class PicturesGallery extends FragmentActivity { 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.gallery_frames); 

     Fragment galleryFragment = fm.findFragmentById(R.id.gallery_fragment); 
     if (galleryFragment == null) { 
      fm.beginTransaction().add(R.id.gallery_fragment, new GalleryFragment()).commit(); 
     } 
     ... 

GridView를 팽창 및 (AN ArrayList 15 항목에 공급되어 표시되는) 어댑터를 설정한다.

public class GalleryFragment extends Fragment { 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 

     // Initialize the layout Adapter 
     PicturesGallery pga = (PicturesGallery) getActivity(); 
     mAdapter = new ImageAdapter(pga.getApplication(), pga.mMemoryCache, 
       galleryDir); 
     mAdapter.mPictureNames = new ArrayList<String>(pictureNames); 

     // Inflate the layout and set the adapter 
     LayoutInflater inflater = (LayoutInflater) getActivity() 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     GridView gridView = (GridView) inflater.inflate(R.layout.pictures_gallery_fragment, null); 
     gridView.setAdapter(mAdapter); 
    } 
    .... 

는하지만 활동이 실행됩니다 아무것도합니다 (GridView에 표시되는 로그 캣하지만 아무것도에 오류)를 발생하지 않습니다. 어댑터 클래스에서 getView() 안에 Log.d()을 사용하면 메서드가 호출되지 않는다는 것을 나타냅니다. 그러나 getCount()은 적절한 수의 항목을 반환합니다. 관련 코드는 다음과 같습니다.

public class ImageAdapter extends BaseAdapter { 
    ... 
    public ArrayList<String> mPictureNames = new ArrayList<String>(); 
    public ImageAdapter(Context c, LruCache<String, Bitmap> mCache, File gallery) { 
     mMemoryCache = mCache; 
     mGalleryDir = gallery; 
     mInflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     Log.d("dupa", "getView"); 
     ViewHolder vh; 
     View cell = convertView; 
     if (cell == null) { 
      vh = new ViewHolder(); 
      cell = mInflater.inflate(R.layout.galleryitem, null); 
      // Populate the ViewHolder 
      vh.checkBox = (CheckBox) cell.findViewById(R.id.itemCheckBox); 
      ... 
      cell.setTag(vh); 
      ... 
     } else { 
      vh = (ViewHolder) cell.getTag(); 
     } 
     // Update the cell View state 
     vh.checkBox.setTag(position); 
     ... 
     return cell; 
    } 

게시물이 길기 때문에 xml 파일을 포함하지 않았습니다. 그들이 필요하다고 생각한다면 (또는 더 많은 코드가 필요함) 그냥 말해주십시오. 어떤 도움이라도 대단히 감사하겠습니다. TIA.

답변

1

당신은 (컨테이너에 팽창 된보기를 추가 나던 inflate 방법을 사용할 수 있습니다. http://developer.android.com/reference/android/app/Fragment.html#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle) 당신의 조각에 onCreateView()를 구현하고이 팽창 된 레이아웃을 반환해야

그냥이

/** 
* The Fragment's UI is just a simple text view showing its 
* instance number. 
*/ 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.hello_world, container, false); 
    View tv = v.findViewById(R.id.text); 
    ((TextView)tv).setText("Fragment #" + mNum); 
    tv.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.gallery_thumb)); 
    return v; 
} 

당신의 GridView에 인스턴스를 기억하고있는 onActivityCreated가 (또는 잠재적으로 onAttach()에 어댑터를 설정할 수있는 페이지의 상단에있는 예제에서 좋아한다.

+0

감사합니다. 그것은 잘 작동합니다. 보기에 어댑터가있을 때 onCreateView와 onActivityCreated를 사용하는 것에 혼란 스러웠습니다. – Vicent

+0

완벽한, 내 gridview 근무 – Manny265

관련 문제