单击GridView项后,ImageView显示相同的图像

时间:2014-05-23 07:48:36

标签: android gridview

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

代码:

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

}

}

传递图片网址的代码:

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

            }
        });

使用图片网址(路径)接收意图的代码

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

谢谢!

1 个答案:

答案 0 :(得分:0)

我猜,您已声明imagePath具有类级别范围。因为在for循环结束时imagePath更新了最后一个项目URL,所以你总是传递最后一个图像的url。

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