2014-05-23 125 views
0

我遇到了一個問題,可能比我想象的要簡單得多。我有一個從JSON發送的URL加載圖像的GridView。 Url然後被轉換爲位圖,並且每個圖像都被傳遞給一個GridView項目。這一切都完美。然後當我點擊圖片時,我發送圖片url到另一個視圖,以全尺寸顯示它,我的問題是,每次我點擊GridView中的一個項目時,它總是將圖片加載到該GridView的最後一個項目中,所以我正在調整,可能當我將圖像url發送到下一個視圖時,我總是傳遞最後一個圖像的url。是否有人知道在列表視圖中單擊後可以如何顯示正確的圖像?任何幫助將不勝感激。ImageView在點擊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); 
     } 
    }); 

} 

}

代碼以通過圖像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); 

由於!

+0

你記錄了意圖中發送的路徑嗎? –

+0

你在這一行如何獲得imagePath? //將圖像路徑發送到下一個視圖 i.putExtra(TAG_PATH,imagePath); –

+0

使用view.settag()將圖像url設置爲視圖並在onItemClick方法中獲取它..適配器的getView()方法.. –

回答

0

我想你已經聲明imagePath具有類級作用域。由於在for循環結束時imagePath已更新爲最後一個項目的URL,因此您始終會傳遞最後一張圖片的網址。

要解決此問題,使用View.setTag()View.getTag()方法,通過URL(或)使用positiononItemClick()檢索 JSONObject image = images.getJSONObject(position);和構造URL。

+0

嗨,我一直這樣做了一段時間了,我已經通過URL使用setTag()現在我試圖獲取標籤時,該項目被點擊,但我有一些麻煩,第一次使用setTag和getTag –

+0

更新「ExtendedSimpleAdapter」的代碼 – blizzard

相關問題