使用来自 URL 的图像创建 GridView
Creating GridView With Images from URL
我一刻都不想离开这里,哈
我在使用 picasso
将图像从 url 加载到 gridview 时遇到了一些问题
我一直在关注这里的教程
http://developer.android.com/guide/topics/ui/layout/gridview.html#example
本教程很容易理解,但它不处理位图图像,因此我尝试使用自定义图像适配器中的 imageView 引用将图像从 url 应用到 ImageView
预期的结果是同一张图片在屏幕上重复了多次,但应用程序加载正常,但图片是空白的。物体在那里是因为触摸屏幕时可以看到它们。
主要ActivityClass
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Call JSON
// make call to json to get the information to display
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(MainActivity.this, "" + position,
Toast.LENGTH_SHORT).show();
}
});
}
}
ImageAdapter Class
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import com.squareup.picasso.Picasso;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(200, 200));
// imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
// imageView.setPadding(1, 1, 1, 1);
} else {
imageView = (ImageView) convertView;
}
// Picasso.with(this.mContext).load("http://www.bathchronicle.co.uk/user/lw-avatar/3541022/profileSmall1407142824456.png").into(imageView);
String url = "http://www.500kgiveaway.co.uk/upload/images/1430572021716.jpg";
Picasso.with(this.mContext).load(url)
.resize(100, 100).into(imageView);
// imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7
};
}
我运行你的代码。您可能忘记添加此权限,其余看起来没问题。
<uses-permission android:name="android.permission.INTERNET"/>
从 Internet 下载图像需要它。
您还可以启用日志记录以查看幕后发生的情况。
Picasso.with(this.mContext).setLoggingEnabled(true);
我一刻都不想离开这里,哈
我在使用 picasso
将图像从 url 加载到 gridview 时遇到了一些问题我一直在关注这里的教程
http://developer.android.com/guide/topics/ui/layout/gridview.html#example
本教程很容易理解,但它不处理位图图像,因此我尝试使用自定义图像适配器中的 imageView 引用将图像从 url 应用到 ImageView
预期的结果是同一张图片在屏幕上重复了多次,但应用程序加载正常,但图片是空白的。物体在那里是因为触摸屏幕时可以看到它们。
主要ActivityClass
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Call JSON
// make call to json to get the information to display
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(MainActivity.this, "" + position,
Toast.LENGTH_SHORT).show();
}
});
}
}
ImageAdapter Class
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import com.squareup.picasso.Picasso;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(200, 200));
// imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
// imageView.setPadding(1, 1, 1, 1);
} else {
imageView = (ImageView) convertView;
}
// Picasso.with(this.mContext).load("http://www.bathchronicle.co.uk/user/lw-avatar/3541022/profileSmall1407142824456.png").into(imageView);
String url = "http://www.500kgiveaway.co.uk/upload/images/1430572021716.jpg";
Picasso.with(this.mContext).load(url)
.resize(100, 100).into(imageView);
// imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7
};
}
我运行你的代码。您可能忘记添加此权限,其余看起来没问题。
<uses-permission android:name="android.permission.INTERNET"/>
从 Internet 下载图像需要它。
您还可以启用日志记录以查看幕后发生的情况。
Picasso.with(this.mContext).setLoggingEnabled(true);