碎片中的GridView没有填充(使用毕加索)

时间:2015-09-06 00:27:03

标签: android android-fragments gridview android-adapter picasso

我正在尝试在官方的android教程网站上使用这个example,并将其转换为使用毕加索加载图像的片段内的gridview。

我正在查看我的代码,我无法弄清楚出了什么问题。图像根本无法加载,我得到的只是一个空白屏幕。我在这里缺少什么?

我的片段代码:

public class MainFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main, container, false);
        GridView gridView = (GridView) view.findViewById(R.id.gridview);
        gridView.setAdapter(new ImageAdapter(getActivity().getApplicationContext()));
        return view;
        }
    }

我的自定义适配器:

public class ImageAdapter extends BaseAdapter {

    private Context mContext;

    public ImageAdapter(Context c){
        mContext = c;
    }
    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ImageView imageView;
        if(view == null){
            imageView = new ImageView(mContext);

        }
        else{
            imageView = (ImageView) view;
        }
        Picasso.with(mContext).setLoggingEnabled(true);
        Picasso.with(mContext)
                .load(eatFoodyImages[i])
                .fit()
                .into(imageView);
        return imageView;
    }

    @Override
    public int getCount() {
        return 0;
    }

    public static String[] eatFoodyImages = {
            "http://i.imgur.com/rFLNqWI.jpg",
            "http://i.imgur.com/C9pBVt7.jpg",
            "http://i.imgur.com/rT5vXE1.jpg",
            "http://i.imgur.com/aIy5R2k.jpg",
            "http://i.imgur.com/MoJs9pT.jpg",
            "http://i.imgur.com/S963yEM.jpg",
            "http://i.imgur.com/rLR2cyc.jpg",
            "http://i.imgur.com/SEPdUIx.jpg",
            "http://i.imgur.com/aC9OjaM.jpg",
            "http://i.imgur.com/76Jfv9b.jpg",
            "http://i.imgur.com/fUX7EIB.jpg",
            "http://i.imgur.com/syELajx.jpg",
            "http://i.imgur.com/COzBnru.jpg",
            "http://i.imgur.com/Z3QjilA.jpg",
    };
}

我的活动:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if(savedInstanceState == null){
            getSupportFragmentManager()
                    .beginTransaction().add(R.id.mainfragment, new MainFragment())
                    .commit();
        }
    }
}

1 个答案:

答案 0 :(得分:2)

@Override public int getCount() { return eatFoodyImages.length; }