【发布时间】:2015-06-25 01:25:00
【问题描述】:
我使用 arrayAdapter 自定义将图像和文本加载到 ListView 但加载图像失败
这是我的数组适配器
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 并链接图像包含在那里,但在此示例中,我使用来自谷歌开发人员的图像
这里还有数组适配器使用的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");
【问题讨论】:
-
图像不会出现在设备中,只是文本
-
你应该把这个添加到问题中。
-
好的,已经添加xml了
-
我的意思是你应该描述你在问题中遇到的失败/错误。