使用网格中的网址加载图片查看

时间:2017-08-29 11:16:34

标签: android

我是android studio的新手。我想制作壁纸应用程序,其中使用gridView中的图像网址从互联网加载图像。为此,我创建了activity_common

activty_common.xml

    <GridView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:columnWidth="100dp"
        android:horizontalSpacing="10dp"
        android:verticalSpacing="10dp"
        android:numColumns="2"
        android:gravity="center"
        android:stretchMode="columnWidth"
        android:id="@+id/gridView" />

custom_layout

<ImageView
        android:id="@+id/sample"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@drawable/lion"/>

然后我创建了Gridadapter

public class Gridadapter  extends BaseAdapter {
private Context mContext;

public String mThumbIds[];

public Gridadapter(Context c) {
    mContext = c;
}
@Override
public int getCount() {
    return mThumbIds.length;
}
@Override
public String getItem(int position) {
    return mThumbIds[position];
}

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

public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(480, 480));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }
    String url = getItem(position);
    Picasso.with(mContext)
            .load(url)
            .centerCrop().into(imageView);
    return imageView;
}}

然后我创建名为Food

的java类
 public class Food extends Activity {

public String[] mThumbIds = {
       " https://www.pexels.com/photo/daylight-environment-fog-forest-539945/",
"https://www.pexels.com/photo/daylight-environment-fog-forest-539945/"
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activtiy_common);
    GridView gridView = (GridView) findViewById(R.id.gridView);
    //  Instance of ImageAdapter Class
    gridView.setAdapter(new Gridadapter(this, mThumbIds));


}

}

我认为gridadapter允许Integer []; 我怎么能传递String []; 我添加了互联网premissiona和毕加索图书馆

什么时候运行app它给了我 错误:(28,29)错误:构造函数Gridadapter类中的Gridadapter无法应用于给定类型; 必需:上下文 发现:Food,String []

原因:实际和正式的参数列表长度不同

提前致谢

3 个答案:

答案 0 :(得分:0)

收到错误,因为在你的适配器类中,Gridadapter中只有一个参数,你从活动中传递了两个参数

只需像这样更改你的Gridadapter构造函数

public Gridadapter(Context c,String[]Food) {
    mContext = c;
    mThumbIds=food;
}

答案 1 :(得分:0)

你的适配器构造函数应该是;

public Gridadapter(Context c,String[] mThumbIds) {
    mContext = c;
mThumbIds = mThumbIds;

}

答案 2 :(得分:0)

您已在 Gridadapter(this,mThumbIds)中传递了两个参数,但在您的适配器类中,您只定义了一个参数。 修改适配器类:

public Gridadapter(Context c,String mThumbIds[]) {
    mContext = c;
    this.mThumbIds[] = mThumbIds[];
}