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.
감사합니다. 그것은 잘 작동합니다. 보기에 어댑터가있을 때 onCreateView와 onActivityCreated를 사용하는 것에 혼란 스러웠습니다. – Vicent
완벽한, 내 gridview 근무 – Manny265