【问题标题】:ImageView displaying the same image after GridView item has been clicked单击 GridView 项目后 ImageView 显示相同的图像
【发布时间】:2014-05-23 07:48:36
【问题描述】:

我遇到的问题可能比我想象的要简单得多。我有一个 GridView,它从以 JSON 格式发送的 URL 加载图像。然后将 Url 转换为位图,并将每个图像传递给 GridView 项。这一切都完美无缺。然后,当我单击图像时,我将图像 url 发送到另一个以全尺寸显示它的视图,我的问题是每次我单击 GridView 中的一个项目时,它总是将图像加载到该 GridView 的最后一个项目中,所以我假设当我将图像 url 发送到下一个视图时,我总是传递最后一张图像的 url。有人知道在列表视图中单击后我可以做些什么来显示正确的图像吗?任何帮助将不胜感激。

代码:

/**
 * Background AsyncTask to load profiles images by making HTTP Request
 */
class GetProfileImages extends AsyncTask<String, String, String> {

// Progress Dialog
private ProgressDialog pDialog; 
URL url = null;

/**
 * Before starting background thread Show Progress Dialog
 */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(ProfileImages.this);
        pDialog.setMessage("Loading images...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    /**
     * Gets all the notices from URL that correspond to the current user
     */
    protected String doInBackground(String... args) {
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("pid", pid));

        // Gets JSON string from URL
        JSONObject json = jsonParser.makeHttpRequest(url_profile_images, "GET", params);

        // Check your log cat for JSON response
        Log.d("Profile images: ", json.toString());

        try {
            // Checking for SUCCESS TAG
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // Image found
                // Gets Array of notices
                images = json.getJSONArray(TAG_IMAGES);

                // Loops through all images
                for (int i = 0; i < images.length(); i++) {
                    JSONObject image = images.getJSONObject(i);

                    // Storing each JSON item in variable
                    imagePath = ("http://gatoandroidapp.comeze.com/" + image.getString(TAG_PATH));

                    //Gets image path and passed the image in bitmap format
                        try {
                            url = new URL(imagePath);
                            bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
                        } catch (MalformedURLException e) {

                        }catch (IOException e) {

                        } 

                    // Creates new HashMap
                    HashMap<String, Object> map = new HashMap<String, Object>();

                    // Ads child nodes to HashMap
                    map.put(TAG_PATH, bmp);

                    // Ads HashList to ArrayList
                    imagesList.add(map);
                }

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
}

/**
 * After completing background task Dismiss the progress dialog
 */
protected void onPostExecute(String file_url) {

    //Dismiss the dialog after getting images
    pDialog.dismiss();

    //Updates UI from Background Thread
    runOnUiThread(new Runnable() {
        public void run() {

             //Updates parsed JSON data into ListView
             ListAdapter adapter = new ExtendedSimpleAdapter(
                    ProfileImages.this, imagesList,
                    R.layout.profile_images_custom_gridview, new String[] {TAG_PATH},
                    new int[] {R.id.profilesImages_customGridView});

            //Updates ListView
           gridview.setAdapter(adapter);
        }
    });

}

}

传递图片url的代码:

  gridview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                //Creates intent
                Intent i = new Intent(v.getContext(), PictureView.class);

                //Sends image path to next view
                i.putExtra(TAG_PATH,  imagePath);

                startActivityForResult(i, 0);

            }
        });

接收带有图像 url(路径)意图的代码

 // Get image path from intent
      imagePath = getIntent().getStringExtra(TAG_PATH);

        //Load image from server into ImageView 
        profilePicture = (ImageView) findViewById(R.id.pictureView_imageView);
        URL url = null;
        Bitmap bmp = null;
        try {
            url = new URL(imagePath);
            bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
        } catch (MalformedURLException e) {

        }catch (IOException e) {

        }
       profilePicture.setImageBitmap(bmp);

谢谢!

【问题讨论】:

  • 您是否记录了意图中发送的路径?
  • 你是如何在这一行获得 imagePath 的? //将图像路径发送到下一个视图 i.putExtra(TAG_PATH, imagePath);
  • 使用view.settag()将图片url设置为View,并在onItemClick方法中获取..post Adapter的getView()方法..
  • 嗨,我正在发送这个图像 pass imagePath = ("gatoandroidapp.comeze.com" + image.getString(TAG_PATH));,我应该发送位图图像而不是那个路径,我的路径'm pass 一定是从服务器接收到的最后一张图片的路径,对吗?

标签: android gridview


【解决方案1】:

我猜,你已经声明 imagePath 具有类级别范围。由于在 for 循环结束时 imagePath 已更新为最后一个项目的 URL,因此您始终传递最后一个图像的 url。

要解决此问题,请使用 View.setTag() 和 View.getTag() 方法传递 URL(或)使用 onItemClick() 中的 position 检索 JSONObject image = images.getJSONObject(position); 并构造 URL。

【讨论】:

  • 嗨,我已经这样做了一段时间了,我已经使用 setTag() 传递了 url 现在我正在尝试在单击项目时获取标签,但我有一些麻烦,第一次用setTag和getTag
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-17
  • 1970-01-01
  • 1970-01-01
  • 2021-08-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多