[英]Android is failing to load image with Picasso and ArrayAdapter into SwipeFlingAdapterView
[英]load image to custom ArrayAdapter
我使用arrayAdapter自定义将图像和文本加载到ListView,但是加载图像失败
这是我的arrayAdapter
public class CustomAdapter extends ArrayAdapter<Post> {
private ImageTask image;
public CustomAdapter(Context context, ArrayList<Post> posts) {
super(context, 0, posts);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
Post post = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.post_list, parent, false);
}
// Lookup view for data population
TextView tituloView = (TextView) convertView.findViewById(R.id.titulo);
TextView subtituloView = (TextView) convertView.findViewById(R.id.subTitulo);
ImageView fotoView = (ImageView) convertView.findViewById(R.id.imageView);
TextView textoView = (TextView) convertView.findViewById(R.id.texto);
// Populate the data into the template view using the data object
tituloView.setText(post.post_titulo);
subtituloView.setText(post.post_sub_titulo);
//this like idk if are the way corret for load the image into ViewImage specific
new ImageTask(fotoView).execute("http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png");
textoView.setText(post.post_texto);
// Return the completed view to render on screen
return convertView;
}
}
这是一个ImageTask图片
public class ImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public ImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
这里是MainActivity的电话
// Create the adapter to convert the array to views
CustomAdapter adapter = new CustomAdapter(this, arrayOfPost);
// Attach the adapter to a ListView
ListView listView = (ListView) findViewById(R.id.lvUsers);
listView.setAdapter(adapter);
arrayOfPost即将解析Jonson到ArrayList并将链接图像包含在其中,但是在此示例中,我使用了来自Google开发人员的图像
以及阵列适配器使用的xml
<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/titulo"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/subTitulo"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/texto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
任何人都知道我的错是什么,我应该如何加载图像? 我不正确,如果是在特定ViewImage中加载图像的方法正确//此行在第一个类中发布
new ImageTask(fotoView).execute("http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png");
在AsyncTask中加载图片的步骤
第1步:
ImageView fotoView = (ImageView) convertView.findViewById(R.id.imageView);
第2步:
String URL1 = "http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png";
第三步:
fotoView.setTag(URL1);
new DownloadImageTask.execute(fotoView);
第四步:
public class DownloadImagesTask extends AsyncTask<ImageView, Void, Bitmap> {
ImageView imageView = null;
@Override
protected Bitmap doInBackground(ImageView... imageViews) {
this.imageView = imageViews[0];
return download_Image((String)imageView.getTag());
}
@Override
protected void onPostExecute(Bitmap result) {
imageView.setImageBitmap(result);
}
private Bitmap download_Image(String url) {
Bitmap bmp =null;
try{
URL ulrn = new URL(url);
HttpURLConnection con = (HttpURLConnection)ulrn.openConnection();
InputStream is = con.getInputStream();
bmp = BitmapFactory.decodeStream(is);
if (null != bmp)
return bmp;
}catch(Exception e){}
return bmp;
} }
我看到您的代码中有几处可以改进的地方:
1.在自定义适配器中,应该使用ViewHolder
来提高ListView
性能,因为这些行: convertView.findViewById()
花费太多。
2.您使用AsyncTask
从Internet的GetView()
加载图像。 可以,但是每当您滚动ListView
,就会调用GetView()
并启动另一个AsyncTask
。 无法保证哪个Task
将首先完成,因此您在ListView
的图像位置可能不正确(现在在每行中加载相同的图像,这不会发生,但将来会发生)。
因此,我的建议是:在适配器中使用ViewHolder
,使用一些第三方Lib
有效地加载图像,例如Universal ImageLoader
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.