2015-07-28 103 views
0

我試圖讓一個應用程序從一個在線服務器獲取不同的照片,取決於用戶點擊的標記。我的問題是,例如,如果用戶點擊marker1,他們會得到marker1的圖片,但當他們嘗試查看​​的照片時,我的gridview繼續顯示marker1的照片(沒有任何更改 - 刪除/添加新照片)。如果用戶以​​開頭,則相同。Android刪除gridview中的所有項目(刷新gridview)

因此,我不明白我在做什麼錯誤,因爲我將適配器設置爲null gridView1.setAdapter(null);,然後搜索點擊標記的圖像,然後在gridview中顯示它們。但是,即使將適配器設置爲null,gridview中的圖片也不會消失。

這是我的主要代碼:

public static ArrayList<String> image_urls = new ArrayList<>(); 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_poi_photos); 

      // Check for double values - clear the gridview. reload the images 
      //image_urls.clear(); 
      gridView1 = (GridView) findViewById(R.id.gridview); 
      if (gridView1.getChildCount() != 0 || image_urls != null) { 
       gridView1.setAdapter(null); 
      } 
      if (image_urls.isEmpty()) 
      { 
       getImages(); 
      } 

      final ImageAdapter adapter = new ImageAdapter(this); 
      adapter.notifyDataSetChanged(); 
      gridView1.setAdapter(adapter); 
    } 


    // GETTING PHOTOS FROM DATABASE 
    protected void showList() { 
     try { 
      JSONObject jsonObj = new JSONObject(myJSON); 
      photos = jsonObj.getJSONArray(TAG_RESULTS); 

      for (int i = 0; i < photos.length(); i++) { 
       JSONObject c = photos.getJSONObject(i); 
       String email_user = c.getString(TAG_EMAIL_USER); 
       String poi_name = c.getString(TAG_POI_NAME); 
       String photo_url = c.getString(TAG_PHOTO_URL); 

       if ((poi_name.substring(0, 4)).equals(map.markername) && photo_url!=null) { 
        // add to the picasso gallery 
        photo_url="http:/xxxxx/photos/"+photo_url; 
        image_urls.add(photo_url); 
       } 

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


    public void getImages() { 
     class GetDataJSON extends AsyncTask<String, Void, String> { 

      @Override 
      protected String doInBackground(String... params) { 
       DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams()); 
       HttpPost httppost = new HttpPost("http://xxxxx/table_photo_out.php"); 

       // Depends on your web service 
       httppost.setHeader("Content-type", "application/json"); 

       InputStream inputStream = null; 
       String result = null; 
       try { 
        HttpResponse response = httpclient.execute(httppost); 
        HttpEntity entity = response.getEntity(); 

        inputStream = entity.getContent(); 
        // json is UTF-8 by default 
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8); 
        StringBuilder sb = new StringBuilder(); 

        String line = null; 
        while ((line = reader.readLine()) != null) { 
         sb.append(line + "\n"); 
        } 
        result = sb.toString(); 
       } catch (Exception e) { 
        // Oops 
       } finally { 
        try { 
         if (inputStream != null) inputStream.close(); 
        } catch (Exception squish) { 
        } 
       } 
       return result; 
      } 

      @Override 
      protected void onPostExecute(String result) { 
       myJSON = result; 
       showList(); 
      } 
     } 
     GetDataJSON g = new GetDataJSON(); 
     g.execute(); 
    } 

任何想法可能是錯誤的,或者有什麼更好的辦法?

+0

你在getImages中設置適配器嗎? – Pavya

+0

@ user3676184不,我只是在getImages()調用 – Diana

+0

後,在onCreate中設置我的適配器,您將獲得列表或數組中的所有圖像。意味着如何迭代所有圖像? – Pavya

回答

1

這是因爲你第一次點擊marker1時獲得的內容會添加到image_urls中。當你點擊marker2時,那時所有的數據都可用。 爲此,您需要設置image_urls = new ArrayList <>();在每次點擊。

1

嘗試設置你的image_urls = null當你點擊。