在交错的网格视图中从资源而不是网址调用图像?

时间:2014-06-23 10:53:27

标签: android android-gridview custom-adapter

我所拥有的是开源交错网格视图适配器,它构建了一个不错的动态网格视图,这里是主要的活动代码:

public class MainActivity extends Activity {
private String urls[] = { 
            "http://farm7.staticflickr.com/6101/6853156632_6374976d38_c.jpg",
            "http://farm8.staticflickr.com/7232/6913504132_a0fce67a0e_c.jpg",
            "http://farm5.staticflickr.com/4133/5096108108_df62764fcc_b.jpg",
            "http://farm5.staticflickr.com/4074/4789681330_2e30dfcacb_b.jpg"};
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        StaggeredGridView gridView = (StaggeredGridView) this.findViewById(R.id.staggeredGridView1);

        int margin = getResources().getDimensionPixelSize(R.dimen.margin);

        gridView.setItemMargin(margin); // set the GridView margin

        gridView.setPadding(margin, 0, margin, 0); // have the margin on the sides as well 

        StaggeredAdapter adapter = new StaggeredAdapter(MainActivity.this, R.id.imageView1, urls);

        gridView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

这是适配器代码:

public class StaggeredAdapter extends ArrayAdapter<String> {

    private ImageLoader mLoader;

    public StaggeredAdapter(Context context, int textViewResourceId,
            String[] objects) {
        super(context, textViewResourceId, objects);
        mLoader = new ImageLoader(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;

        if (convertView == null) {
            LayoutInflater layoutInflator = LayoutInflater.from(getContext());
            convertView = layoutInflator.inflate(R.layout.row_staggered_demo,
                    null);
            holder = new ViewHolder();
            holder.imageView = (ScaleImageView) convertView .findViewById(R.id.imageView1);
            convertView.setTag(holder);
        }

        holder = (ViewHolder) convertView.getTag();

        mLoader.DisplayImage(getItem(position), holder.imageView);

        return convertView;
    }

    static class ViewHolder {
        ScaleImageView imageView;
    }
}

我想要做的是从资源调用图像而不是像这样的链接:

private int urls[] = { R.drawable.roller_blinds, R.drawable.zebra_blinds2,R.drawable.zebra_blinds,
            R.drawable.wooden_blinds,R.drawable.wooden2_blinds,R.drawable.office,R.drawable.motors,
            R.drawable.curtains_accessories
            };

我已将适配器中的构造函数更改为:

public StaggeredAdapter(Context context, int textViewResourceId,
            int[] objects) {

但我一直看到这行错误:

super(context, textViewResourceId,objects);

为什么会这样?如何从资源而不是链接调用我的图像?我做错了什么?

错误说:

The constructor ArrayAdapter<String>(Context, int, int[]) is undefined

1 个答案:

答案 0 :(得分:0)

您无权访问

ArrayAdapter(Context, int, int[])

因为您将T定义为String,所以您有权访问的实际构造函数是

ArrayAdapter(Context, int, String[])

我希望它有意义