从ListView中的URL插入图像

时间:2013-04-19 14:05:24

标签: android android-listview android-adapter

我想知道当URL存放在json中时是否可以直接从Web加载ListView中的图像。

编辑:已解决,可以使用AsyncTask

    public class ParseImage extends AsyncTask<Object, String, Drawable> {

        private ProgressBar pd;
        private ImageView imv;
        private int position;

        public ParseImage(int position, ImageView imv, ProgressBar pd2) {

            this.position = position;
            this.imv = imv;
            this.pd = pd2;

        }

        @Override
        public Drawable doInBackground(Object... params) {

            Drawable d = null;

            try {
                d = Drawable.createFromStream(
                        (InputStream) new URL(list.get(position).get("Cover"))
                                .getContent(), "src");
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return d;

        }

        public void onPostExecute(Drawable result) {

            if (result != null && imv != null) {
                pd.setVisibility(View.INVISIBLE);
                imv.setImageDrawable(result);
            }

        }
    }
}

2 个答案:

答案 0 :(得分:1)

您需要创建自己的自定义Listview以显示多个String.Here是您可以参考的一段代码:

MovieAdaptor.java:

public class MovieAdaptor extends ArrayAdapter<Movie> {
Context context;
int layoutResourceId;
ArrayList<Movie> movieArrayList = new ArrayList<Movie>();


public MovieAdaptor(Context context, int layoutResourceId, ArrayList<Movie> movies) {
    super(context, layoutResourceId, movies);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.movieArrayList = movies;
}

public View getView(final int position, View convertView, ViewGroup parent) {
    View row = convertView;
    ContactHolder holder = null;

    if (row == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new ContactHolder();
        holder.thumbnail = (ImageView) row.findViewById(R.id.imageThumbnail);
        holder.title = (TextView) row.findViewById(R.id.MovieTitle);
        holder.synopsis = (TextView) row.findViewById(R.id.synopsis);

        row.setTag(holder);
    } else {
        holder = (ContactHolder) row.getTag();
    }
    holder.title.setText(movieArrayList.get(position).title);
    holder.synopsis.setText(movieArrayList.get(position).synopsis);

       newAsyncDownloadImage(holder.thumbnail).execute(movieArrayList.get(position).posters.get("thumbnail"));


    return row;
}

static class ContactHolder {
    ImageView thumbnail;
    TextView title;
    TextView synopsis;
}


}

Activity.class:

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    moviesListView = (ListView) findViewById(R.id.list_movies);
    moviesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (position > HEADER) {
                Intent intent = new Intent(getBaseContext(), MovieActivity.class);
                intent.putExtra(MOVIE_OBJECT, movieArrayList.get(position - 1));
                startActivity(intent);
            }
        }
    });
    View header = (View) getLayoutInflater().inflate(R.layout.listview_row_header, null);
    moviesListView.addHeaderView(header);
    movieAdaptor = new MovieAdaptor(this, R.layout.listview_row_item, movieArrayList);
    moviesListView.setAdapter(movieAdaptor);

AsyncDownloadImage.java:

public class AsyncDownloadImage extends AsyncTask<Object, Object, Object> {
ImageView iv;
private HttpURLConnection connection;
private InputStream is;
private Bitmap bitmap;

public AsyncDownloadImage(ImageView mImageView) {
    iv = mImageView;
}

@Override
protected Object doInBackground(Object... params) {
    URL url;
    try {
        url = new URL((String) params[0]);
        connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        is = connection.getInputStream();
        bitmap = BitmapFactory.decodeStream(is);
        is.close();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (is != null) {
                is.close();
            }
            if (connection != null) {
                connection.disconnect();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return bitmap;
}

@Override
protected void onPostExecute(Object result) {
    super.onPostExecute(result);
    if (null != result) {
        iv.setImageBitmap((Bitmap) result);
    } else {
        iv.setBackgroundResource(R.drawable.ic_launcher);
    }
    }
}


}

答案 1 :(得分:0)

你能发送

的回报吗?
JSONfunction.getJSONfromURL("http://colmarmagazine.ctai.fr/issues.php");

拜托? 我认为这是一个糟糕的离别!因为你有:

Unable to decode stream: java.io.FileNotFoundException: /http:/colmarmagazine.ctai.fr/content/numerodemo/numerodemo.png: open failed: ENOENT (No such file or directory)
在您的封面网址中

和“/”太多了!