-2

I am implementing a grid view which is inside fragment with a custom adapter. But the getView() of custom adapter has never called. I set the grid view like below.

public class MainFragment extends Fragment {
ArrayList<ObjectPack> objects;
GridView gridView;
ThumbnailGridViewAdapter adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    objects = new ArrayList<ObjectPack>();
    objects.add(createMockObjectPack());
    gridView = (GridView)getActivity().getLayoutInflater().inflate(R.layout.fragment_story_main, null).findViewById(R.id.gridView_Main);
    adapter = new ThumbnailGridViewAdapter(this.getActivity(), R.layout.item_thumbnail, objects);
    gridView.setAdapter(adapter);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_story_main, container, false);

    return view;
}

private ObjectPack createMockObjectPack() {
    ObjectPack mock;
    int id = 1;
    String title = "test";
    String description = "TEST";
    int iconId = R.drawable.title;
    ArrayList<Integer> imoticonIds = null;

    mock = new ObjectPack(id, title, description, iconId, imoticonIds);

    return mock;
}
}

And next is the layout xml of the fragment. The text view is shown well but the grid view isn't.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView_storyMain"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/message_storyMain" />

<GridView
    android:id="@+id/gridView_storyMain_theme"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:horizontalSpacing="10dp"
    android:numColumns="4"
    android:paddingBottom="10dp"
    android:paddingTop="10dp"
    android:verticalSpacing="10dp" >
</GridView>

</LinearLayout>

And whenever I tried to debug, the break points in onCreate and onCreateView hit four times, why is it?

Please help me, any advice would be appreciated a lot!!!

================ added ======================

Below is my adapter code, by x90's advice, getView() is called once but screen is all black now. I'm very new to android please help me, Thanks!!!

public class ThumbnailGridViewAdapter extends BaseAdapter {
Context context;
int itemLayoutId;
List<ObjectPack> objects;
LayoutInflater inflater;

BitmapLoader loader;

public ThumbnailGridViewAdapter(Context context, int itemLayoutId, List<ObjectPack> objects) {
    super();

    this.context = context;
    this.itemLayoutId = itemLayoutId;
    this.objects= objects;

    this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    loader = new BitmapLoader(context.getResources(), R.drawable.loading);
}

@Override
public int getCount() {
    return objects.size();
}

@Override
public Object getItem(int index) {
    return objects.get(index);
}

@Override
public long getItemId(int index) {
    return objects.get(index).getId();
}

@Override
public View getView(int index, View convertView, ViewGroup parent) {
    if(convertView == null) {
        convertView = inflater.inflate(itemLayoutId, parent, false);
    }
    else {
        ImageView imageView = (ImageView)convertView.findViewById(R.id.imageView_item_thumbnail);
        imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
        imageView.setScaleType(ScaleType.FIT_XY);
        loader.LoadBitmap(objects.get(index).getIconId(), imageView);

        TextView text = (TextView)convertView.findViewById(R.id.textView_item_thumbnail);
        text.setText(objects.get(index).getTitle());
    }

    return convertView;
}

}

genki98
  • 680
  • 1
  • 11
  • 31

1 Answers1

2

in onCreateView:

GridView gridView = (GridView)view.findViewById(R.id.gridView_storyMain_theme);
gridView.setAdapter(adapter);
adapter.notifyDataSetChanged();


And remove GridView creation in onCreate method. If your adapter is fine - grid will appear.

x90
  • 2,140
  • 15
  • 25
  • Thanks!! It made my code one step foward I guess. But I'm stuck at another problem. getView() called once, but the screen turned to black. I guess something wrong in adapter. – genki98 May 26 '14 at 12:40