0

我想知道当 URL 存储在 json 中时,是否可以直接从网络加载 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);
            }

        }
    }
}
4

2 回答 2

1

您需要创建自己的自定义 Listview 来显示更多的字符串。这是一段您可以参考的代码:

电影适配器.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;
}


}

活动类:

 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);
    }
    }
}


}
于 2013-04-19T14:58:51.250 回答
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)

还有一个“/”在你的封面上太多了!

于 2013-04-19T14:35:44.117 回答